I need help with htis issue I have with Javan swing. I have three JLists in my GUI. One of the list contains a menu for a restaurant. The cashier is supposed to click on the a dish on the first JList and whatever he clicked should appear on the second JList. How can i do this?
我需要帮助解决我与Javan swing的htis问题。我的GUI中有三个JLists。其中一个包含餐厅菜单。收银员应该点击第一个JList上的菜肴,他点击的任何东西都应该出现在第二个JList上。我怎样才能做到这一点?
Also another problem is that I cant hae the JList appear UNLESS i am giving it an array object to display the menu, I want the JList to appear while its empty as well, how can i do that?
还有一个问题是我不能让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;
}
}
2 个解决方案
#1
0
I have commented out lots of the code. I have to say it, please do not get me wrong I do not want to offend any of your skills or anything but this code of yours is one ugly son of ... a code I have seen in a very long time :)
我已经注释掉了很多代码。我必须说出来,请不要误会我的意思我不想冒犯你的任何技能或任何东西,但是你的这个代码是一个丑陋的儿子...我在很长一段时间内见过的代码:)
@Swaranga Sarma suggestions are useful. And as stated by this user the JList would show even if empty. You almost had it right, you just added the list wrongly. You were on the right track thought, you have used JScrollPane's but then you should add these to the panel and not the list.
@Swaranga Sarma建议很有用。并且如该用户所述,JList即使是空的也会显示。你几乎把它弄好了,你只是错误地添加了列表。你是在正确的轨道上思考,你已经使用过JScrollPane,但是你应该将它们添加到面板而不是列表中。
I have decided to use a mouse listener here instead of the suggested by @Swaranga Sarma selection listener because doing so you will have no problem when clicking twice on the same item and adding it only once as it would be if you used the selection listener as it works only on item change.
我已经决定在这里使用鼠标监听器而不是@Swaranga Sarma选择监听器的建议,因为这样做在单击两次同一项目并且只添加一次时就没有问题,因为如果你使用选择监听器那样它仅适用于项目更改。
package test.components;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class frame extends JFrame
{
private JList menuList;
private JList menuList2;
private JList orderList;
private JLabel countLabel;
// private DefaultListModel listModel;
private Dimension menuListDimension;
public static void main(String[] args)
{
JFrame f = new frame();
f.setSize(800, 600);
f.setVisible(true);
}
public frame()
{
//JFrame frame = new JFrame();
//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
buildUI();
}
private void buildUI()
{
//BoxLayout mainLayout = new BoxLayout(getContentPane(), BoxLayout.X_AXIS);
GridLayout mainLayout = new GridLayout(1, 5);
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);
final DefaultListModel 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(50);
menuList.setFixedCellWidth(150);
menuList.setBackground(Color.GREEN);
menuList.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
Object selected = menuList.getSelectedValue();
System.out.println("menuList.addMouseListener.mousePressed selected=" + selected);
DefaultListModel dm = (DefaultListModel) orderList.getModel();
dm.add(orderList.getModel().getSize(), selected);
}
});
JScrollPane listScroller = new JScrollPane(menuList);
listScroller.setPreferredSize(new Dimension(100, 250));
menuPanel.add(listScroller);
// 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);
orderList = new JList(new DefaultListModel());//(listModel); //data has type Object[]
orderList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
orderList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
orderList.setVisibleRowCount(50);
orderList.setFixedCellWidth(150);
orderList.setBackground(Color.RED);
JScrollPane listScroller = new JScrollPane(orderList);
listScroller.setPreferredSize(new Dimension(100, 250));
//orderList.setVisible(true);
orderPanel.add(listScroller);
//orderPanel.setVisible(true);
// getContentPane().add(orderPanel);
// 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));
DefaultListModel 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);
menuList2 = new JList(listModel); //data has type Object[]
menuList2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
menuList2.setLayoutOrientation(JList.VERTICAL);
menuList2.setVisibleRowCount(50);
menuList2.setFixedCellWidth(150);
menuList2.setDragEnabled(true);
menuList2.setBackground(Color.BLUE);
// menuList2.setMinimumSize(menuListDimension);
JScrollPane listScroller = new JScrollPane(menuList2);
listScroller.setPreferredSize(new Dimension(100, 250));
payPanel.add(listScroller);
// payPanel.add(Box.createVerticalStrut(5));
// getContentPane().add(payPanel);
JButton payButton = new JButton("Pay");
JButton cancelButton = new JButton("Cancel");
payPanel.add(payButton);
payPanel.add(cancelButton);
return payPanel;
}
}
Please next time clean the code more :) I really wonder what got me through it. Maybe because I had a similar problem one day, a long time ago, and it was easy to deal with it again? :)
请下次再清理代码:)我真的很想知道是什么让我通过它。也许是因为我很久以前就有类似的问题了,很容易再次处理它? :)
Good luck, Boro.
祝你好运,博罗。
#2
0
-
Empty
JList : JList l = new JList()
;空JList:JList l = new JList();
-
Add a list selection Listener
添加列表选择侦听器
firstJList.addSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //add items to your other JList } });
firstJList.addSelectionListener(new ListSelectionListener(){public void valueChanged(ListSelectionEvent e){//将项添加到其他JList}});
#1
0
I have commented out lots of the code. I have to say it, please do not get me wrong I do not want to offend any of your skills or anything but this code of yours is one ugly son of ... a code I have seen in a very long time :)
我已经注释掉了很多代码。我必须说出来,请不要误会我的意思我不想冒犯你的任何技能或任何东西,但是你的这个代码是一个丑陋的儿子...我在很长一段时间内见过的代码:)
@Swaranga Sarma suggestions are useful. And as stated by this user the JList would show even if empty. You almost had it right, you just added the list wrongly. You were on the right track thought, you have used JScrollPane's but then you should add these to the panel and not the list.
@Swaranga Sarma建议很有用。并且如该用户所述,JList即使是空的也会显示。你几乎把它弄好了,你只是错误地添加了列表。你是在正确的轨道上思考,你已经使用过JScrollPane,但是你应该将它们添加到面板而不是列表中。
I have decided to use a mouse listener here instead of the suggested by @Swaranga Sarma selection listener because doing so you will have no problem when clicking twice on the same item and adding it only once as it would be if you used the selection listener as it works only on item change.
我已经决定在这里使用鼠标监听器而不是@Swaranga Sarma选择监听器的建议,因为这样做在单击两次同一项目并且只添加一次时就没有问题,因为如果你使用选择监听器那样它仅适用于项目更改。
package test.components;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class frame extends JFrame
{
private JList menuList;
private JList menuList2;
private JList orderList;
private JLabel countLabel;
// private DefaultListModel listModel;
private Dimension menuListDimension;
public static void main(String[] args)
{
JFrame f = new frame();
f.setSize(800, 600);
f.setVisible(true);
}
public frame()
{
//JFrame frame = new JFrame();
//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
buildUI();
}
private void buildUI()
{
//BoxLayout mainLayout = new BoxLayout(getContentPane(), BoxLayout.X_AXIS);
GridLayout mainLayout = new GridLayout(1, 5);
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);
final DefaultListModel 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(50);
menuList.setFixedCellWidth(150);
menuList.setBackground(Color.GREEN);
menuList.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
Object selected = menuList.getSelectedValue();
System.out.println("menuList.addMouseListener.mousePressed selected=" + selected);
DefaultListModel dm = (DefaultListModel) orderList.getModel();
dm.add(orderList.getModel().getSize(), selected);
}
});
JScrollPane listScroller = new JScrollPane(menuList);
listScroller.setPreferredSize(new Dimension(100, 250));
menuPanel.add(listScroller);
// 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);
orderList = new JList(new DefaultListModel());//(listModel); //data has type Object[]
orderList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
orderList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
orderList.setVisibleRowCount(50);
orderList.setFixedCellWidth(150);
orderList.setBackground(Color.RED);
JScrollPane listScroller = new JScrollPane(orderList);
listScroller.setPreferredSize(new Dimension(100, 250));
//orderList.setVisible(true);
orderPanel.add(listScroller);
//orderPanel.setVisible(true);
// getContentPane().add(orderPanel);
// 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));
DefaultListModel 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);
menuList2 = new JList(listModel); //data has type Object[]
menuList2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
menuList2.setLayoutOrientation(JList.VERTICAL);
menuList2.setVisibleRowCount(50);
menuList2.setFixedCellWidth(150);
menuList2.setDragEnabled(true);
menuList2.setBackground(Color.BLUE);
// menuList2.setMinimumSize(menuListDimension);
JScrollPane listScroller = new JScrollPane(menuList2);
listScroller.setPreferredSize(new Dimension(100, 250));
payPanel.add(listScroller);
// payPanel.add(Box.createVerticalStrut(5));
// getContentPane().add(payPanel);
JButton payButton = new JButton("Pay");
JButton cancelButton = new JButton("Cancel");
payPanel.add(payButton);
payPanel.add(cancelButton);
return payPanel;
}
}
Please next time clean the code more :) I really wonder what got me through it. Maybe because I had a similar problem one day, a long time ago, and it was easy to deal with it again? :)
请下次再清理代码:)我真的很想知道是什么让我通过它。也许是因为我很久以前就有类似的问题了,很容易再次处理它? :)
Good luck, Boro.
祝你好运,博罗。
#2
0
-
Empty
JList : JList l = new JList()
;空JList:JList l = new JList();
-
Add a list selection Listener
添加列表选择侦听器
firstJList.addSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //add items to your other JList } });
firstJList.addSelectionListener(new ListSelectionListener(){public void valueChanged(ListSelectionEvent e){//将项添加到其他JList}});