簡體   English   中英

我想將動作偵聽器從一個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?

我需要有關Javan swing的htis問題的幫助。 我的GUI中有三個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;


     }


    }

  1. 空的JList : JList l = new JList() ;

  2. 添加列表選擇監聽器

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

我已經注釋掉了很多代碼。 我必須說這句話,請不要誤會我的意思,我不想冒犯您的任何技能或任何東西,但是您的這段代碼是...的一個丑陋兒子……我在很長一段時間里都看到過這樣的代碼:)

@Swaranga Sarma的建議很有用。 並且如該用戶所述,即使為空,JList也會顯示。 您幾乎是正確的,只是錯誤地添加了列表。 您以為是對的,您曾經使用過JScrollPane,但是您應該將它們添加到面板中而不是列表中。

我決定在這里使用鼠標偵聽器,而不是@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;
    }
}


請下次再清理代碼:)我真的很想知道是什么促使我完成了。 也許是因為很久以前的一天,我遇到了類似的問題,並且很容易再次解決它? :)

祝你好運,博羅。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM