因为在Jcombobox中,我认为不允许多选,但可以使用jlist。
addActionListener中的错误
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo() {
prepareGUI();
}
public static void main(String[] args) {
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showListDemo();
}
private void prepareGUI() {
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);
statusLabel.setSize(350, 100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showListDemo() {
headerLabel.setText("JList");
//final DefaultListModel
mainMenu = new DefaultListModel();
String[] choices = {
"Redeem",
"Prepaid",
"FRC",
"Offices",
"Individual Bags",
"Gift Subscription",
"E-Gift Card",
"My Account",
"Wp-Admin",
"Shipstaion",
"Any date changes"
};
final JList mainList = new JList(choices);
mainList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
mainList.setSelectedIndex(0);
mainList.setVisibleRowCount(5);
JScrollPane mainListScrollPane = new JScrollPane(mainList);
mainList.setBounds(125, 110, 173, 30);
mainList.setSelectedIndex(0);
mainList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// final DefaultListModel
subMenu = new DefaultListModel();
String[] choices1 = {
"Redeem Remaining & Redeem Gift Card",
"Redeem of Tasting Kit Sub",
"US & NON-US Redeem",
"Manually Created Vouchers"
};
final JList subList1 = new JList(choices1);
subList1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
subList1.setSelectedIndex(0);
subList1.setVisibleRowCount(5);
JScrollPane subListScrollPane1 = new JScrollPane(subList1);
controlPanel.add(subListScrollPane1);
}
});
// String[] choices2 = {"Prepaid US","Prepaid Non-US","Prepaid Auto Renewal", "Prepaid Don't Renew","Prepaid Auto Renewal Failed Payment","With Enable Dates"};
// final JList subList2 = new JList(choices2);
// subList2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// subList2.setSelectedIndex(0);
// subList2.setVisibleRowCount(5);
// JScrollPane subListScrollPane2 = new JScrollPane(subList2);
//controlPanel.add(subListScrollPane2);
JButton showButton = new JButton("Show main menu");
JButton showButton1 = new JButton("Show sub menu");
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "";
if (mainList.getSelectedIndex() != -1) {
data = "Main List Selected: " + mainList.getSelectedValue();
statusLabel.setText(data);
}
statusLabel.setText(data);
}
});
showButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "";
if (subList1.getSelectedIndex() != -1) {
data += " Sub List selected: ";
for (Object sub: subList1.getSelectedValues()) {
data += sub + " ";
}
}
statusLabel.setText(data);
}
});
controlPanel.add(mainListScrollPane);
controlPanel.add(showButton);
controlPanel.add(showButton1);
mainFrame.setVisible(true);
}
}