我需要關於Javan swing的htis問題的幫助。我的GUI中有三個JList。其中一個列表包含一家餐廳的菜單。收銀員應該點擊第一個JList上的一道菜,而他點擊的任何東西都會出現在第二個JList上。我怎樣才能做到這一點?我想從一個JList添加一個動作偵聽器到另一個JList,以及一個JList如何在內部出現任何文本?
另一個問題是,我不能顯示JList出現,除非我給它一個數組對象來顯示菜單,我希望JList出現時,它也是空的,我該怎麼做?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class frame extends JFrame {
private JList menuList ;
private JList orderList;
private JLabel countLabel;
private DefaultListModel listModel;
private Dimension menuListDimension;
public frame(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
buildUI();
}
private void buildUI() {
BoxLayout mainLayout = new BoxLayout(getContentPane(), BoxLayout.X_AXIS);
getContentPane().setLayout(mainLayout);
getContentPane().add(Box.createHorizontalGlue());
getContentPane().add(buildMenuPanel());
getContentPane().add(Box.createHorizontalStrut(0));
getContentPane().add(buildOrderPanel());
getContentPane().add(Box.createHorizontalStrut(10));
getContentPane().add(buildPayPanel());
getContentPane().add(Box.createHorizontalStrut(50));
//getContentPane().add(recieptPanel());
getContentPane().add(Box.createHorizontalGlue());
}
private JPanel buildMenuPanel(){
JPanel menuPanel = new JPanel();
BoxLayout menuLayout = new BoxLayout(menuPanel, BoxLayout.Y_AXIS);
menuPanel.setLayout(menuLayout);
getContentPane().add(menuPanel);
//menuList.addActionListener(
//new ActionListener()
{
//public void actionPerformed(ActionEvent e)
//{
//helloPressed();
//}
//}
listModel = new DefaultListModel();
listModel.addElement("ghjghj");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
listModel.addElement("ghjghj");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
listModel.addElement("ghjghj");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
listModel.addElement("ghjghj");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
menuList = new JList(listModel); //data has type Object[]
menuList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
menuList.setLayoutOrientation(JList.VERTICAL);
menuList.setVisibleRowCount(-1);
menuList.setFixedCellWidth(200);
JScrollPane listScroller = new JScrollPane(menuList);
listScroller.setPreferredSize(new Dimension(7, 250));
menuPanel.add(menuList);
menuPanel.add(Box.createVerticalStrut(5));
return menuPanel;
}
}
private JPanel buildOrderPanel(){
JPanel orderPanel = new JPanel();
BoxLayout orderLayout = new BoxLayout(orderPanel, BoxLayout.Y_AXIS);
orderPanel.setLayout(orderLayout);
getContentPane().add(orderPanel);
//menuList.addActionListener(
//new ActionListener()
//{
//public void actionPerformed(ActionEvent e)
//{
//helloPressed();
//}
orderList = new JList(listModel); //data has type Object[]
orderList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
orderList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
orderList.setVisibleRowCount(-1);
orderList.setFixedCellWidth(200);
JScrollPane listScroller = new JScrollPane(orderList);
listScroller.setPreferredSize(new Dimension(250, 80));
//orderList.setVisible(true);
orderPanel.add(orderList);
//orderPanel.setVisible(true);
orderPanel.add(Box.createVerticalStrut(5));
return orderPanel;
}
private JPanel buildPayPanel(){
JPanel payPanel = new JPanel();
BoxLayout doneLayout = new BoxLayout(payPanel, BoxLayout.Y_AXIS);
payPanel.setLayout(doneLayout);
getContentPane().add(payPanel);
payPanel.add(Box.createVerticalStrut(5));
listModel = new DefaultListModel();
listModel.addElement("ghjghj");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
listModel.addElement("ghjghj");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
listModel.addElement("ghjghj");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
listModel.addElement("ghjghj");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
menuListDimension = new Dimension (10,10);
menuList = new JList(listModel); //data has type Object[]
menuList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
menuList.setLayoutOrientation(JList.VERTICAL);
menuList.setVisibleRowCount(50);
menuList.setFixedCellWidth(300);
menuList.setDragEnabled(true);
menuList.setMinimumSize(menuListDimension);
JScrollPane listScroller = new JScrollPane(menuList);
listScroller.setPreferredSize(new Dimension(80, 250));
payPanel.add(menuList);
payPanel.add(Box.createVerticalStrut(5));
getContentPane().add(payPanel);
JButton payButton = new JButton ("Pay");
JButton cancelButton = new JButton ("Cancel");
//menuList.addActionListener(
//new ActionListener()
//{
//public void actionPerformed(ActionEvent e)
//{
//helloPressed();
//}
payPanel.add(payButton);
payPanel.add(cancelButton);
return payPanel;
}
}
如果您還在代碼中包含主要方法,以便代碼可以運行並準確呈現所需的問題,那麼只需對未來進行說明即可。 我假設你正在以這種方式發起它(如果是的話,我會建議你把它編輯成你的問題代碼)。 'public static void main(String [] args) \t { \t \t JFrame f = new frame(); \t \t f.setSize(800,600); \t \t f.setVisible(true); \t}' – Boro 2011-04-07 23:21:50