我有两个视图:
- MainWindowView(扩展JFrame)
- ScanOptimisationView(扩展JPanel)
所以,我在MainWindowView类中有一个下拉框。我创建了ActionListener并将其绑定到此下拉框上。这个ActionListener的actionPerformed()方法尝试将ScanOptimisationView面板添加到主窗口框架中。以下是代码:
package ru.belaventcev.view;
import java.awt.Container;
public class MainWindowView extends JFrame{
private int frmHeight = 525;
private int frmWidth = 650;
public Container frmContainer;
public static JButton btnCalc;
public static JComboBox cbMethods;
public MainWindowView(){
setPreferredSize(new Dimension(frmWidth, frmHeight));
setSize(frmWidth, frmHeight);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
frmContainer = getContentPane();
frmContainer.setLayout(new MigLayout("", "[grow,center]", "[::30px,grow,center][grow,center][::500px,grow,center][::25px,grow,center]"));
cbMethods = new JComboBox();
cbMethods.setModel(new DefaultComboBoxModel(new JPanel[] {new ScanOptimisationView()}));
cbMethods.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel temp = (JPanel) cbMethods.getSelectedItem();
frmContainer.add(temp, "cell 0 1,span");
}
});
/*
* If I uncomment this, panel is shown!
JPanel temp = (JPanel) cbMethods.getSelectedItem();
frmContainer.add(temp, "cell 0 1");
*/
frmContainer.add(cbMethods, "cell 0 0,growx");
btnCalc = new JButton("Расчитать");
frmContainer.add(btnCalc, "cell 0 3,alignx right");
}
}
您能帮我理解一下吗——为什么在actionPerformed()中使用代码时,面板没有显示出来,但是当我使用下面的代码时,面板会显示出来?