0

I'm trying to add some buttons in a JPanel from a comboBox. The comboBox has an array of 8 ints and when one of them is selected I want to be able to press a go button which will then display the number of buttons selected from the comboBox into a JPanel.

The JPanel is initially empty and the go button is disabled until something is selected.

I have created the JPanel, comboBox and the go button, but I'm now lost as to how to get and create the buttons.

The comboBox filled with Strings -

String[] floorStrings = {"Select one", "1", "2", "3", "4", "5", "6", "7", "8"};
//Create the combo box
JComboBox floorList = new JComboBox(floorStrings);

The actionPerformed code -

        public void actionPerformed(ActionEvent e) {

        floorList.getSelectedIndex();
        //int i = Integer.valueOf((String) floorList);

    if (e.getSource() == go) {
        go.setText("Stop");
        System.out.print("Clicked " + floorList);
        p3.add(go, BorderLayout.NORTH);
    } 

}
4

1 回答 1

1

将一个附加ActionListener到“开始”按钮。在actionPerformed您需要从 中获取值的方法中JComboBox,只需使用getSelectedValue. 这会返回一个Object. 检查对象是否不是null并尝试将其转换为int(即(int)value)。

如果转换成功,只需根据组合框中的值创建一个for-next循环n次数的循环并创建按钮,将它们添加到面板中。

查看如何编写动作侦听器以及如何使用组合框for 语句以获取更多详细信息

更新示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.management.StringValueExp;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestComboBox08 {

    public static void main(String[] args) {
        new TestComboBox08();
    }

    public TestComboBox08() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JComboBox floorList;
        private JPanel buttons;

        public TestPane() {
            setLayout(new BorderLayout());
            String[] floorStrings = {"Select one", "1", "2", "3", "4", "5", "6", "7", "8"};
            floorList = new JComboBox(floorStrings);
            JButton go = new JButton("Go");
            go.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int count = floorList.getSelectedIndex();
                    buttons.removeAll();
                    for (int index = 0; index < count; index++) {
                        buttons.add(new JButton(String.valueOf(index)));
                    }
                    buttons.revalidate();
                }
            });
            JPanel top = new JPanel(new FlowLayout(FlowLayout.CENTER));
            top.add(floorList);
            top.add(go);

            buttons = new JPanel(new GridLayout(0, 4));
            buttons.setPreferredSize(new Dimension(200, 200));

            add(top, BorderLayout.NORTH);
            add(buttons);
        }
    }
}
于 2013-04-11T00:34:05.783 回答