目前我有一个JFrame'窗口',其布局设置为BorderLayout。我有一个面板用于西部和中部,两个交替的面板用于南部。在西部我有5个标签。在中心部分,我有5个JTextField组件。在南部区域,我想在两个面板(都是FlowLayout)和两个面板之间交替(每个面板都有两个独立的按钮)。
现在我初始化并且南部区域有正确的两个按钮,但是当我调用我的事件动作侦听器时,只有一半的方法被正确调用。在幕后我将菜单项连接到监听器,它似乎正常工作。明文方法有效,但更改南面板不会
这里是正在初始化的面板和框架(它们在类构造函数中初始化):
myWindow = new JFrame();
myWindow.setLayout(new BorderLayout());
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sPanel1.add(prevButton);
sPanel1.add(nextButton);
sPanel2.add(okButton);
sPanel2.add(cancelButton);
myWindow.add(sPanel1, BorderLayout.SOUTH);
myWindow.add(wPanel, BorderLayout.WEST);
myWindow.add(cPanel, BorderLayout.CENTER);
//so here, at this line, i have tried the adding sPanel2 at the south position,
//after i have added sPanel1. The result is that sPanel2 overrides sPanel1 (like i
//assumed it would). However, the same doesnt work in the editAddEvent method
//below. both lines are called but only the cleartextfields method works.
//why is this?
public class myEvent implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
myWindow.add(sPanel2, BorderLayout.SOUTH);
clearTextFields();
myWindow.revalidate();
myWindow.repaint();
Container content = myWindow.getContentPane();
content.revalidate();
content.repaint();
}
}
public void clearTextFields(){
fNameTxt.setText("");
mNameTxt.setText("");
lNameTxt.setText("");
}
clear textfields方法工作正常,它清除了我的JTextFields