有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing Java JList侦听器

我的代码有什么问题

我已经创建了一个JList,添加了项目并将其推到左侧(BorderLayout.WEST)。每次单击列表项时,我都希望在列表右侧显示一个面板。但是问题是,当一个列表项被选中并且监听器运行时,到达if选择,根据所选的索引,应该显示另一个类(具有内部类)中与其相关的面板(但它没有!)

        import java.awt.BorderLayout;
        import java.awt.CardLayout;
        import java.awt.Color;
        import java.awt.Font;

        import javax.swing.JFrame;
        import javax.swing.JList;
        import javax.swing.JPanel;
        import javax.swing.ListSelectionModel;
        import javax.swing.event.ListSelectionEvent;
        import javax.swing.event.ListSelectionListener;

    public class MainGUI extends JFrame{

    ListListeners listListeners = new ListListeners();

    JList list = new JList(
            new String[]{"Create Account","Borrow Book","Return Book","Add Book","Delete Book","Display Details"}
            );

    JPanel panel1 = new JPanel();

    public MainGUI()
    {
        CardLayout cardLayout = new CardLayout();
        JPanel panel = new JPanel();
        list.setForeground(Color.RED);
        list.setBackground(Color.WHITE);
        list.setSelectionForeground(Color.GREEN);
        list.setSelectionBackground(Color.LIGHT_GRAY);
        list.setFixedCellWidth(150);
        list.setFixedCellHeight(50);
        list.setFont(new Font("Serif",Font.BOLD,16));
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //add(listListeners.new  CreateAccount(panel1));
        //ListListeners.CreateAccount createAccount = listListeners.new  CreateAccount(panel1);
        //add(createAccount.createAccountPanel());          
        //registering the JList listener
        list.addListSelectionListener(new ListListener());
        panel.add(list);
        add(panel,BorderLayout.WEST);
    }

    class ListListener  extends JFrame implements ListSelectionListener
    {
        public void valueChanged(ListSelectionEvent e)
        {
            int index = list.getSelectedIndex();
            if(e.getValueIsAdjusting() == false) {

                if (list.getSelectedIndex() == -1) {

                    System.out.println("No list item was selected");

                } else {


                    if(index == 0)
                    {

                        ListListeners.CreateAccount createAccount = listListeners.new  CreateAccount(panel1);
                        add(createAccount.createAccountPanel());

                        System.out.println(index);
                    }
                    else if(index == 1)
                    {
                        System.out.println(index);
                    }
                    else if(index == 2)
                    {
                        System.out.println(index);
                    }
                    else if(index == 3)
                    {
                        System.out.println(index);
                    }
                    else if(index == 4)
                    {
                        System.out.println(index);
                    }
                    else if(index == 5)
                    {
                        System.out.println(index);
                    }
                }

            }
        }
    }

    public static void main(String[] args) {
        MainGUI frame = new MainGUI();

        frame.setSize(500, 350);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }    
}

这是另一个具有内部类的类

    import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ListListeners extends JFrame {

   class CreateAccount extends JPanel
   {           
       JLabel label = new JLabel("Enter new members' name : ");
       JTextField textField = new JTextField("This is a text field");          
       JRadioButton radioButton1 = new JRadioButton();
       JRadioButton radioButton2 = new JRadioButton();

       public CreateAccount(JPanel panel)
       {
           panel.add(label);
           panel.add(textField);
           panel.add(radioButton1,radioButton2);               
           add(panel,BorderLayout.CENTER);
       }

       public JPanel createAccountPanel()
       {
           JPanel panel = new JPanel();;
           panel.add(label);
           panel.add(textField);
           panel.add(radioButton1);            
           return panel;               
       }
   }

   class BorrowBook extends JPanel
   {
       public BorrowBook(JPanel panel)
       {
           JLabel label = new JLabel("Just borrow the book and go : ");
           JTextField textField = new JTextField("This is a second text field");   
           JRadioButton radioButton1 = new JRadioButton();
           JRadioButton radioButton2 = new JRadioButton();             
           panel.add(label);
           panel.add(textField);
           panel.add(radioButton1);
           panel.add(radioButton2);            
           add(panel,BorderLayout.CENTER);
       }
   }

   class ReturnBook extends JPanel
   {

   }

   class AddBook extends JPanel
   {

   }

   class DeleteBook extends JPanel
   {

   }

   class DisplayDetails extends JPanel
   {

   }
}

共 (1) 个答案

  1. # 1 楼答案

    ListListener类不应扩展JFrame

    所以不能像编码一样使用add()方法。您需要对MainGui类的引用,因为是JList可见的框架。因此,可能需要将帧作为参数传递给ListListener类

    其次,将组件添加到可见GUI时,基本代码结构是:

    panel.add();
    panel.revalidate();
    panel.repaint();
    

    因为您需要告诉布局管理器已经添加了一个组件

    我建议您阅读How to Use Lists上的Swing教程,了解一个使用ListSelectionListener的工作示例。它还将为您的程序提供更好的设计