0

我的代码有什么问题。

我创建了一个 JList,添加了项目并将其推到左侧(BorderLayout.WEST)。每次单击列表项时,我都希望在列表右侧显示一个面板。But the problem is when a list item is selected and the listener is run, reaches to the if selection where according to the index selected a panel related to it from another class (with inner classes) should be displayed(but it does not!) . . .

        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
   {

   }
}
4

1 回答 1

1

您的 ListListener 类不应扩展 JFrame。

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

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

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

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

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

于 2013-10-19T15:34:14.867 回答