-1
当我尝试运行这个类后,我搜索它(在actionPerformed
)它不更新JFrame
,我试了一大堆不同的东西(repaint()
,revalidate()
等),但没有他们的工作,如果你能发现问题,将帮助很多。JFrame不会更新
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.setVisible(false)替换f.dospose()。 – selalerer
不知道你想要做什么,但我看到删除组件和添加组件。在我看来,你的情况更适合'CardLayout'。从[**这个问题**](http://stackoverflow.com/questions/21592492/how-can-i-switch-between-jpanels)和[**这个答案**](http ://stackoverflow.com/a/21460065/2587435)和[**如何使用CardLayout **](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) –
它doesn不更新JFrame意味着JFrame不显示或JFrame显示,但JPanel不显示? – user12458