【问题标题】:JFrame won't updateJFrame 不会更新
【发布时间】:2014-02-15 05:51:23
【问题描述】:

当我在搜索后尝试运行这个类时(在actionPerformed 中)它不会更新JFrame,我尝试了一大堆不同的东西(repaint()、revalidate() 等。 ) 但如果您能发现对您有很大帮助的问题,它们都不起作用。

public class Scroll extends JPanel implements ActionListener {

    private static final int N = 16;
    private JTextArea last;
    private int index;
    JButton sort, Quit, search;
    arraySorter array;
    JFrame f;
    String[]sValue;
    double[]dValue;
    boolean checks;
    public Scroll(String[]sValues, double[]dValues, boolean check, String compare) {
        sValue=sValues;
        dValue=dValues;
        checks=check;
        this.setLayout(new GridLayout(0, 1, 3, 3));
        System.out.println(compare);
        if(check){   
            for (int i = 0; i < sValues.length; i++) {
                    if(sValues[i].contains(""+compare+""))this.add(create(i, sValues[i], 0, true));
                }
        }else{
            for (int i = 0; i < dValues.length; i++) {
                this.add(create(i, null, dValues[i], false));
            }
        }
    }
    private JTextArea create(int i, String j, double k, boolean check) {
        if(check)last =  new JTextArea(i+1+". "+j+"");
        else last =  new JTextArea(i+1+". "+k+"");
        last.setEditable(false);
        return last;
    }

public void display() {

        f = new JFrame("This is your sorted list!!!!");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(this));
        f.invalidate();
        f.validate();
        f.repaint();
        f.setSize(300, 300);

        JPanel panel=new JPanel();
        f.add(panel, BorderLayout.NORTH);
        Quit=new JButton("Quit");//sets instruction button for frame
        //adds an actionlistener
        Quit.addActionListener(this);
        panel.add(Quit);


        search=new JButton("search");//sets instruction button for frame
        //adds an actionlistener
        search.addActionListener(this);
        panel.add(search);
        f.invalidate();
        f.validate();
        f.repaint();
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==Quit){
            f.dispose();
            array=new arraySorter();
        }

        if(e.getSource()==search){
            f.dispose();
            String compare=JOptionPane.showInputDialog("What do you want to search for?");
            f.removeAll();
            new Scroll(sValue, dValue, checks, compare);
            display();
        }
    }

}

【问题讨论】:

  • 尝试将 f.dospose() 替换为 f.setVisible(false)。
  • 不确定您到底要做什么,但我看到删除组件和添加组件。在我看来,您的情况更适合CardLayout。查看this question 的示例和this answer 和How to use CardLayout 的可运行示例
  • 不更新JFrame是指JFrame不显示还是JFrame显示但JPanel不显示?

标签: java swing search jframe


【解决方案1】:

从外观上看,您正在关闭当前的 JFrame,并在每次搜索时创建一个全新的 JFrame。为什么不简单地创建一个更新器方法来填充当前 JFrame 中的所有内容?您使用的方法似乎效率极低,而且很可能是您的问题的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2016-04-24
    • 2014-09-10
    相关资源
    最近更新 更多