我想与另一位在一个JFrame,我已经物色我的代码,但没有任何的发生,这是我的代码来代替一个JPanel:
public class Frame extends JFrame {
private Container contain;
private JPanel reChange,reChange2;
private JButton reChangeButton;
public Frame() {
super("Change a panel");
setSize(350, 350);
setLayout(null);
setLocationRelativeTo(null);
setResizable(false);
reChange = new JPanel(null);
reChange.setBackground(Color.red);
reChange.setSize(240, 225);
reChange.setBounds(50, 50, 240, 225);
add(reChange);
reChangeButton = new JButton("Change It");
reChangeButton.setBounds(20, 20, 100, 20);
add(reChangeButton);
reChangeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println("in");
contain = getContentPane();
contain.removeAll();
//System.out.println("in2");
reChange2 = new JPanel(null);
reChange2.setBackground(Color.white);
reChange2.setSize(240, 225);
reChange2.setBounds(50, 50, 240, 225);
//System.out.println("in3");
contain.add(reChange2);
validate();
//System.out.println("in4");
setVisible(true);
//System.out.println("in5");
}
});
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
有人能帮我吗 ? 非常感谢
你必须调用validate()
然后repaint()
含面板上你做的删除后,并添加操作。
contain.validate();
contain.repaint();
你需要做的是这样的:
public void actionPerformed(ActionEvent e) {
//System.out.println("in");
contain = getContentPane();
contain.removeAll();
//System.out.println("in2");
reChange2 = new JPanel(null);
reChange2.setBackground(Color.white);
reChange2.setSize(240, 225);
reChange2.setBounds(50, 50, 240, 225);
//System.out.println("in3");
contain.add(reChange2);
validate();
repaint();
//System.out.println("in4");
setVisible(true);
//System.out.println("in5");
}
});
几个问题与您的代码。 这是固定的版本:
public class Frame extends JFrame {
private Container contain;
private JPanel reChange,reChange2;
private JButton reChangeButton;
public Frame() {
super("Change a panel");
setSize(350, 350);
getContentPane().setLayout(null); // Changed here
setLocationRelativeTo(null);
setResizable(false);
reChange = new JPanel(null);
reChange.setBackground(Color.red);
reChange.setSize(240, 225);
reChange.setBounds(50, 50, 240, 225);
getContentPane().add(reChange); // Changed here
reChangeButton = new JButton("Change It");
reChangeButton.setBounds(20, 20, 100, 20);
getContentPane().add(reChangeButton); // Changed here
reChangeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contain = getContentPane();
contain.removeAll();
reChange2 = new JPanel(null);
reChange2.setBackground(Color.white);
reChange2.setSize(240, 225);
reChange2.setBounds(50, 50, 240, 225);
contain.add(reChange2);
invalidate(); // Changed here
repaint(); // Changed here
}
});
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
假设你有一个函数,generatePanel()返回您在类型JPanel的一个实例变量保存一个JPanel:
private JPanel panelWithDynamicContent;
假定您已经放置在JPanel中放入容器中,或许在其他容器内的具体指标,并希望保留该容器内的组件的顺序。 而不是使用的removeAll()毁灭一切的,我更喜欢一个更精确的办法,仅替换需要更换组件:
private void replacePanel(){
Container parent = this.panelWithDynamicContent.getParent();
int index = parent.getComponentZOrder(this.panelWithDynamicContent);
// remove the old edition of the panel
parent.remove(this.panelWithDynamicContent);
// generate the replacement panel
this.panelWithDynamicContent = generatePanel();
// place the replacement panel in the same relative location as the one it is replacing
parent.add(this.panelWithDynamicContent, index);
// must call both of these, in the correct order
parent.validate();
parent.repaint();
}
试试这个,下面的代码将加载第二的JPanel(NewOrder)以第一的JPanel(jpMain)
NewOrder no = new NewOrder(); //This is the object of Second JPanel
// jpMain - This is the First JPanel
jpMain.setLayout(new java.awt.BorderLayout());
jpMain.removeAll();
jpMain.add(no);
jpMain.revalidate();