在一个JFrame中包含了另一个Jpanel,现在更改了包含Jpanel中的Label,但不知应如何刷新,望指点;package Gui;import java.awt.BorderLayout;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class MainFrame extends JFrame { private JPanel jpanel;
private otherPanel otherpanel;
public MainFrame(){
init();
}
public void init(){
this.setLayout(new BorderLayout());
jpanel=new JPanel();
jpanel.add(new JLabel("MainFrame"));
otherpanel=new otherPanel();
this.add(jpanel,BorderLayout.NORTH);
this.add(otherpanel,BorderLayout.CENTER);
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
new otherPanel().setLabel(new JLabel("change Label"));
}
}package Gui;import java.awt.BorderLayout;import javax.swing.JLabel;
import javax.swing.JPanel;public class otherPanel extends JPanel { private JLabel label;
public otherPanel(){
this.setLayout(new BorderLayout());
label=new JLabel("otherPanel");
this.add(label,BorderLayout.CENTER);
} public JLabel getLabel() {
return label;
} public void setLabel(JLabel label) {
this.label = label;
}
}
import javax.swing.JLabel;
import javax.swing.JPanel;public class MainFrame extends JFrame { private JPanel jpanel;
private otherPanel otherpanel;
public MainFrame(){
init();
}
public void init(){
this.setLayout(new BorderLayout());
jpanel=new JPanel();
jpanel.add(new JLabel("MainFrame"));
otherpanel=new otherPanel();
this.add(jpanel,BorderLayout.NORTH);
this.add(otherpanel,BorderLayout.CENTER);
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
new otherPanel().setLabel(new JLabel("change Label"));
}
}package Gui;import java.awt.BorderLayout;import javax.swing.JLabel;
import javax.swing.JPanel;public class otherPanel extends JPanel { private JLabel label;
public otherPanel(){
this.setLayout(new BorderLayout());
label=new JLabel("otherPanel");
this.add(label,BorderLayout.CENTER);
} public JLabel getLabel() {
return label;
} public void setLabel(JLabel label) {
this.label = label;
}
}
找不到代码了....
你setLabel了但是并没有放到面板上啊,只是放到
otherPanel对象里面而已,同时你的otherPanel 对象又new了两次,需要补习基础。 public static void main(String[] args) {
MainFrame mf=new MainFrame();
// mf.getOtherpanel().setLabel(new JLabel("change Label"));
mf.getOtherpanel().getLabel().setText("aa");
} public otherPanel getOtherpanel() {
return otherpanel;
}