java - 添加新的组合框 Java
问题描述
我在 java 上的 Swing 接口有问题。说明:我有一个包含 1、2、3、4、5 项的组合框。When an exact item is selected I need to create some more comboboxes the number of which depends on the selected item. 因此,如果选择了数字 5,则框架中必须再出现 5 个组合框。我使用了 ActionListener 但它不能正常工作。但是,相同的代码但在 Actionlistener 之外运行良好。这会是什么问题?
public class FrameClass extends JFrame {
JPanel panel;
JComboBox box;
String[] s = {"1", "2", "3", "4", "5"};
String[] s1 = {"0", "1", "2", "3", "4", "5"};
public FrameClass() {
panel = new JPanel();
box = new JComboBox(s);
JComboBox adults = new JComboBox(s);
JComboBox children = new JComboBox(s1);
panel.add(box, BorderLayout.CENTER);
box.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i <= box.getSelectedIndex(); i++) {
panel.add(adults, BorderLayout.WEST);
panel.add(children, BorderLayout.WEST);
}
}
});
add(panel);
}
}
public class MainClass {
public static void main(String[] args) {
JFrame frame = new FrameClass();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.getContentPane().setBackground(Color.WHITE);
frame.setVisible(true);
}
}
解决方案
您没有通知布局管理器有关面板中的新元素的问题。
这是您的动作侦听器的正确变体:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class FrameClass extends JFrame {
JPanel panel;
JComboBox<String> box;
String[] s = {"1", "2", "3", "4", "5"};
String[] s1 = {"0", "1", "2", "3", "4", "5"};
public FrameClass() {
panel = new JPanel();
box = new JComboBox(s);
JComboBox[] adults = new JComboBox[5];
JComboBox[] children = new JComboBox[5];
for (int i = 0; i < 5; i++) {
adults[i] = new JComboBox<>(s);
children[i] = new JComboBox<>(s1);
}
panel.add(box, BorderLayout.CENTER);
JPanel nested = new JPanel();
add(nested, BorderLayout.EAST);
box.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
nested.removeAll();
nested.setLayout(new GridLayout(box.getSelectedIndex() + 1, 2));
for (int i = 0; i <= box.getSelectedIndex(); i++) {
nested.add(adults[i]);
nested.add(children[i]);
}
getContentPane().revalidate();
getContentPane().repaint();
pack();
}
});
add(panel);
}
public static void main(String[] args) {
JFrame frame = new FrameClass();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.getContentPane().setBackground(Color.WHITE);
frame.setLocationRelativeTo(null); // center the window
frame.setVisible(true);
}
}
推荐阅读
- facebook - 从 LogicApp 到 Facebook 的 OAuthException - LogicApp 设计
- javascript - 为什么javascript函数返回未定义
- angular - 在 angular 和 openlayers 中,将地图点的名称设置为动态的
- javascript - 无法从函数中获取值
- android - 房间数据库迁移
- c++ - 函数模板推导中的类型转换
- c# - 异常 Uknown 标识符:在 Oreo Android 上启动服务
- c# - 从 WebAPI 控制器返回 DTO
- wpf - 如何创建动画路径数据
- xml - 为什么我使用 System.Xml.XmlReader 的代码没有检测到无效的 XML 文件?