我正在创建一个电梯,并从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
答案 0 :(得分:1)
更改floorStrings
的顺序,以便按照您期望的顺序显示地板。
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]);
}
});
}
});
}
}
答案 1 :(得分:0)
使用ListCellRenderer
来操纵项目在组合框中的显示方式。
http://docs.oracle.com/javase/6/docs/api/javax/swing/ListCellRenderer.html
编辑:
实施ListCellRenderer
是更好的说法