java不能是用jframe_java – 无法动态添加JPanel到JFrame

我要观点:

> MainWindowView(扩展JFrame)

> ScanOptimisationView(扩展JPanel)

所以,我在MainWindowView类中有组合框.然后我创建ActionListener并将其绑定到这个组合框.此ActionListener的actionPerfomed()方法尝试将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()中显示代码,但是当我使用下面的代码时会显示它?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值