我在向JComboBox中添加ArrayList时遇到问题。有人能就如何进行提出一些建议吗?我似乎无法使ArrayList进入组合框。使用arrayLists填充JCombobox有哪些不同的方法?
酒店系统。Java
package hotelManage;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class HotelSystem extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1840835913045151061L;
private JFrame mainFrame;
private JPanel mainPanel;
private JPanel btnPanel;
private JButton btnRoom;
private JButton btnCustomer;
private JButton btnOrder;
private JButton btnSearch;
private CardLayout cLayout;
private JLabel lblUpdate;
//
//
OrderSystem order = new OrderSystem();
JPanel orderPanel = order.getOrderPanel();
//
RoomSystem room = new RoomSystem();
JPanel roomPanel = room.getRoomPanel();
ArrayList<String> AvailableRooms = room.getAvailableRooms();
//
private JButton btnBackfromRoom = room.getBtnBack();
private JButton btnBackfromOrder = order.getBtnBack();
//other
private JButton btnUserInputEdit = room.getBtnEdit();
public HotelSystem(){
showGUI();
registerListeners();
}
private void showGUI(){
//create JFrame(mainFrame)
mainFrame = new JFrame("Hotel Management System");
mainFrame.setSize(500,300);
mainFrame.setLayout(new GridLayout(2,0));
//create buttons
btnRoom = new JButton("Room Editor");
btnCustomer = new JButton("Customer Editor");
btnOrder = new JButton("Order");
btnSearch = new JButton("Search");
//create Labels
lblUpdate = new JLabel("Instructions/details will go here.");
//create main panel
mainPanel = new JPanel();
cLayout = new CardLayout();
mainPanel.setLayout(cLayout);
//create panel that holds the buttons
btnPanel = new JPanel();
//add buttons to panel
btnPanel.add(btnRoom);
btnPanel.add(btnCustomer);
btnPanel.add(btnOrder);
btnPanel.add(btnSearch);
btnPanel.add(lblUpdate);
//Button & Room Panel
mainPanel.add(btnPanel, "Buttons");
mainPanel.add(roomPanel, "Rooms");
mainPanel.add(orderPanel, "Orders");
//other stuff
mainFrame.add(mainPanel);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void registerListeners(){
//register all buttons to self
btnRoom.addActionListener(this);
btnCustomer.addActionListener(this);
btnOrder.addActionListener(this);
btnSearch.addActionListener(this);
btnBackfromRoom.addActionListener(this);
btnBackfromOrder.addActionListener(this);
btnUserInputEdit.addActionListener(this);
} // end registerListeners
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
//check all button presses and send
//control to appropriate methods
if (e.getSource() == btnRoom){
cLayout.show(mainPanel, "Rooms");
} else if (e.getSource() == btnCustomer){
} else if (e.getSource() == btnOrder){
cLayout.show(mainPanel, "Orders");
} else if (e.getSource() == btnSearch){
} else if (e.getSource() == btnBackfromRoom){
cLayout.show(mainPanel, "Buttons");
} else if (e.getSource() == btnBackfromOrder){
cLayout.show(mainPanel, "Buttons");
} else if (e.getSource() == btnUserInputEdit){
//retrieve contents from ComboBox.
JComboBox<String> test = room.getRoomType();
String selectedItem = (String) test.getSelectedItem();
//adds user input into array
AvailableRooms.add(selectedItem);
//testing functionality of.. something.
for(String item: AvailableRooms){
System.out.println("retrieved element: " + item);
lblUpdate.setText("A new room has been created! It has been stored under the " + item + " category.");
}
cLayout.show(mainPanel, "Buttons");
} else {
//lblOutput.setText("something went wrong");
} // end if
} // end actionPerformed
public static void main(String[] args) {
new HotelSystem();
}
}
RoomSystem。Java
package hotelManage;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
public class RoomSystem {
private JButton btnEdit;
private JButton btnBack;
private JPanel roomPanel;
private JComboBox<String> roomType;
GridBagLayout gridBag;
//RoomPent, RoomLarge, RoomSmall
String[] roomArray = { "Penthouse", "Large Room", "Small Room" };
ArrayList<String> AvailableRooms = new ArrayList<String>();
public RoomSystem(){
setBtnEdit(new JButton("Create"));
setBtnBack(new JButton("Back to Main Menu"));
Label lblRoom= new Label("Room Type: ");
setRoomType(new JComboBox<>(roomArray));
roomPanel = new JPanel();
roomPanel.setLayout(new GridBagLayout());
GridBagConstraints gridConst = new GridBagConstraints();
//Panel Dimensions
gridConst.gridx = 0;
gridConst.gridy = 0;
roomPanel.add(lblRoom, gridConst);
gridConst.gridx = 1;
gridConst.gridy = 0;
roomPanel.add(roomType, gridConst);
gridConst.gridx = 0;
gridConst.gridy = 2;
roomPanel.add(getBtnEdit(), gridConst);
gridConst.gridx = 1;
gridConst.gridy = 2;
roomPanel.add(getBtnBack(), gridConst);
}
public JPanel getRoomPanel() {
return roomPanel;
}
public void setRoomPanel(JPanel roomPanel) {
this.roomPanel = roomPanel;
}
public JButton getBtnBack() {
return btnBack;
}
public void setBtnBack(JButton btnBack) {
this.btnBack = btnBack;
}
public JButton getBtnEdit() {
return btnEdit;
}
public void setBtnEdit(JButton btnEdit) {
this.btnEdit = btnEdit;
}
public ArrayList<String> getAvailableRooms(){
return AvailableRooms;
}
public void setAvailableRooms(ArrayList<String> AvailableRooms){
this.AvailableRooms = AvailableRooms;
}
public JComboBox<String> getRoomType() {
return roomType;
}
public void setRoomType(JComboBox<String> roomType) {
this.roomType = roomType;
}
}
订单系统。Java
package hotelManage;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import javax.swing.*;
public class OrderSystem {
private JButton btnOrder;
private JButton btnBack;
private JPanel orderPanel;
private JTextField firstName;
private JTextField lastName;
//private JComboBox<String> roomList;
String[] arraynamehere = { "Temp", "Temp1", "Temp2" };
RoomSystem room = new RoomSystem();
ArrayList<String> aRooms = room.getAvailableRooms();
JComboBox<String> roomList;
//DefaultComboBoxModel<String> Modes = new DefaultComboBoxModel<String>();
public OrderSystem(){
btnOrder = new JButton("Process Order");
setBtnBack(new JButton("Back to Main Menu"));
JLabel lblfName = new JLabel("First Name: ");
JLabel lbllName = new JLabel("Last Name: ");
JLabel lblfEmptyRooms = new JLabel("Available Rooms: ");
firstName = new JTextField("", 20);
lastName = new JTextField("", 20);
//ComboBox that should hold the elements of aRooms (Array)
roomList = new JComboBox<String>();
orderPanel = new JPanel();
orderPanel.setLayout(new GridBagLayout());
GridBagConstraints gridConst = new GridBagConstraints();
//Panel Dimensions
gridConst.gridx = 0;
gridConst.gridy = 0;
orderPanel.add(lblfName, gridConst);
gridConst.gridx = 1;
gridConst.gridy = 0;
orderPanel.add(firstName, gridConst);
gridConst.gridx = 0;
gridConst.gridy = 1;
orderPanel.add(lbllName, gridConst);
gridConst.gridx = 1;
gridConst.gridy = 1;
orderPanel.add(lastName, gridConst);
gridConst.gridx = 0;
gridConst.gridy = 2;
orderPanel.add(lblfEmptyRooms, gridConst);
gridConst.gridx = 1;
gridConst.gridy = 2;
orderPanel.add(roomList, gridConst);
gridConst.gridx = 0;
gridConst.gridy = 3;
orderPanel.add(btnOrder, gridConst);
gridConst.gridx = 1;
gridConst.gridy = 3;
orderPanel.add(getBtnBack(), gridConst);
}
public JPanel getOrderPanel() {
return orderPanel;
}
public void setOrderPanel(JPanel orderPanel) {
this.orderPanel = orderPanel;
}
public JButton getBtnBack() {
return btnBack;
}
public void setBtnBack(JButton btnBack) {
this.btnBack = btnBack;
}
public static void main(String[] args) {
}
}
您需要在comobox obect上调用setModel()。
comboBox.setModel(new DefaultComboBoxModel<>(aRooms.toArray(new String[aRooms.size()])));
请参阅此处的文档
JComboBox
应与ComboBoxModel
一起使用。
roomList = new JComboBox<>(new DefaultComboBoxModel<String>(aRooms.toArray(new String[aRooms.size()));
此代码将创建并填充组合框的模型。