我想将一个 JList 中的动作侦听器添加到另一个 JList,并且 JList 如何在没有任何文本的情况下出现? [英] I want to add an action listener from one JList to another JList and how can a JList appear with out any text inside?

查看:28
本文介绍了我想将一个 JList 中的动作侦听器添加到另一个 JList,并且 JList 如何在没有任何文本的情况下出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关 Javan Swing 的 htis 问题的帮助.我的 GUI 中有三个 JList.其中一个列表包含餐厅的菜单.收银员应该点击第一个 JList 上的一道菜,他点击的任何内容都应该出现在第二个 JList 上.我怎样才能做到这一点?

还有一个问题是,我不能让 JList 出现,除非我给它一个数组对象来显示菜单,我希望 JList 在它也为空时出现,我该怎么做?

<预><代码>导入 java.awt.*;导入 java.awt.event.*;导入 javax.swing.*;公共类框架扩展 JFrame {私有 JList 菜单列表;私人 JList 订单列表;私人 JLabel countLabel;私有 DefaultListModel listModel;私有维度菜单ListDimension;公共框架(){JFrame frame = new JFrame();frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);构建UI();}私有无效buildUI(){BoxLayout mainLayout = new BoxLayout(getContentPane(), BoxLayout.X_AXIS);getContentPane().setLayout(mainLayout);getContentPane().add(Box.createHorizo​​ntalGlue());getContentPane().add(buildMenuPanel());getContentPane().add(Box.createHorizo​​ntalStrut(0));getContentPane().add(buildOrderPanel());getContentPane().add(Box.createHorizo​​ntalStrut(10));getContentPane().add(buildPayPanel());getContentPane().add(Box.createHorizo​​ntalStrut(50));//getContentPane().add(recieptPanel());getContentPane().add(Box.createHorizo​​ntalGlue());}私人 JPanel buildMenuPanel (){JPanel menuPanel = new JPanel();BoxLayout menuLayout = new BoxLayout(menuPanel, BoxLayout.Y_AXIS);menuPanel.setLayout(menuLayout);getContentPane().add(menuPanel);//menuList.addActionListener(//新的ActionListener(){//public void actionPerformed(ActionEvent e)//{//helloPressed();//}//}listModel = new DefaultListModel();listModel.addElement("ghjghj");listModel.addElement("约翰史密斯");listModel.addElement("Kathy Green");listModel.addElement("ghjghj");listModel.addElement("约翰史密斯");listModel.addElement("Kathy Green");listModel.addElement("ghjghj");listModel.addElement("约翰史密斯");listModel.addElement("Kathy Green");listModel.addElement("ghjghj");listModel.addElement("约翰史密斯");listModel.addElement("Kathy Green");menuList = new JList(listModel);//数据类型为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));返回菜单面板;}}私人 JPanel buildOrderPanel(){JPanel orderPanel = new JPanel();BoxLayout orderLayout = new BoxLayout(orderPanel, BoxLayout.Y_AXIS);orderPanel.setLayout(orderLayout);getContentPane().add(orderPanel);//menuList.addActionListener(//新的ActionListener()//{//public void actionPerformed(ActionEvent e)//{//helloPressed();//}orderList = new JList(listModel);//数据类型为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));退货订单面板;}私人 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("约翰史密斯");listModel.addElement("Kathy Green");listModel.addElement("ghjghj");listModel.addElement("约翰史密斯");listModel.addElement("Kathy Green");listModel.addElement("ghjghj");listModel.addElement("约翰史密斯");listModel.addElement("Kathy Green");listModel.addElement("ghjghj");listModel.addElement("约翰史密斯");listModel.addElement("Kathy Green");menuListDimension = 新维度 (10,10);menuList = new JList(listModel);//数据类型为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("取消");//menuList.addActionListener(//新的ActionListener()//{//public void actionPerformed(ActionEvent e)//{//helloPressed();//}payPanel.add(payButton);payPanel.add(cancelButton);返回payPanel;}}

解决方案

  1. Empty JList : JList l = new JList();

  2. 添加列表选择监听器

    firstJList.addSelectionListener(new ListSelectionListener() {public void valueChanged(ListSelectionEvent e){//将项目添加到您的其他JList} });

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?

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?



    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;


     }


    }

解决方案

  1. Empty JList : JList l = new JList();

  2. Add a list selection Listener

    firstJList.addSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //add items to your other JList } });

这篇关于我想将一个 JList 中的动作侦听器添加到另一个 JList,并且 JList 如何在没有任何文本的情况下出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆