java - 如何在从 JComboBox 创建的按钮上放置标签?

标签 java swing jbutton jcombobox

我正在创建一个电梯,并从 JComboBox 中制作了一些按钮,但是我似乎无法在它们上获得标签。最多创建 8 个按钮,并且按钮必须从下到上命名。所以最后添加的按钮应该是一楼。

如何在从 JComboBox 创建的按钮上制作标签?

[-------floor N-------]
[-------floor 3-------]
[-------floor 2-------]
[-------floor 1-------]

这是我的一些代码...

//The main class
public class Elevator_Simulation extends JFrame implements ActionListener {

public JLabel state; //The current state of the elevator being displayed
public ButtonPanel control; //The button control panel
private Elevator elevator; //The elevator area
String[] floorStrings = {"Select one", "1", "2", "3", "4", "5", "6", "7", "8"};    
JComboBox floorList = new JComboBox(floorStrings); //The combo box
JButton go = new JButton();
public JPanel buttons;
//private int counter;

//constructor
public Elevator_Simulation() {        

    //Setting up layout and content pane
    this.setSize(500, 500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocation(100, 100);
    this.getContentPane().setLayout(new BorderLayout(1, 1));

    buttons = new JPanel(new GridLayout(8, 1));
    add(buttons);

    //Panel creation
    JPanel centerpanel = new JPanel();
    centerpanel.setLayout(new FlowLayout());

    //Adds the button panel to the BorderLayout
    this.getContentPane().add(buttons, BorderLayout.EAST);

    // adds the title to the top of p3
    p3.add(title, BorderLayout.NORTH);
    // adds floorlist to the top right of p3
    p3.add(floorList, BorderLayout.NORTH);
    // adds the start button to the panel
    p3.add(go, BorderLayout.NORTH);
    go.setText("Start");
    go.addActionListener(this);
    // adds p2 to the right of the container
    this.getContentPane().add(p3, BorderLayout.NORTH);

//Main method
public static void main(String[] args) {
    Elevator_Simulation eSim = new Elevator_Simulation();
    eSim.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    eSim.setVisible(true);
}

//start of the actionPerformed
@Override
public void actionPerformed(ActionEvent e) {
    int count = floorList.getSelectedIndex();
    //buttons.removeAll();
    for (int index = 0; index < count; index++) {
        buttons.add(new JButton("F" + String.valueOf(index)));
    }
    buttons.revalidate();

    elevator = new Elevator(this);
    this.getContentPane().add(elevator, BorderLayout.CENTER);

}
//end of the actionPerformed

最佳答案

更改 floorStrings 的顺序以便楼层按照您期望的顺序显示。

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
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) {
                }

                JComboBox cb = new JComboBox(new String[]{"Select one", "8", "7", "6", "5", "4", "3", "2", "1"});

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(cb);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

更新为能够简单地反转选择索引...

现在我们颠倒了顺序,因此我们颠倒了选择索引(项目 8 不在位置 1 )。

我认为解决此问题的最简单方法是使用 Arrays.asList(floorsList).indexOf(...)这将返回 floorsList 中所选值的位置数组...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestComboBox08 {

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

    private String[] floorsList = new String[]{"Select one", "8", "7", "6", "5", "4", "3", "2", "1"};

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

                final JComboBox cb = new JComboBox(floorsList);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(cb);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                cb.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String value = (String)cb.getSelectedItem();
                        int index = Arrays.asList(floorsList).indexOf(value);
                        System.out.println("Item at " + index + " = " + floorsList[index]);
                    }
                });
            }
        });
    }

}

关于java - 如何在从 JComboBox 创建的按钮上放置标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16006526/

相关文章:

java - 用Java创建 "paint"应用程序

java - 在 actionPerformed 完成之前禁用单击按钮 java

java - 在 Java 中,我将如何创建一个本质上是字符串子类但设置允许长度范围的类?

java - 我们如何测试一个类是否实现了很多接口(interface)?

java - Flex 和 Java 应用程序使用什么架构

java - 从另一个类禁用 JButton

java - 透明JButton占用了其他的背景,如何解决?

java - 跨多个域的 Tomcat 集成 Windows 身份验证

java - 基于控制台的应用程序到 Java 中基于 GUI 的应用程序

java - 设计时显示图像