我正在创建电梯,并从 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