gpt4 book ai didi

Java JList 监听器

转载 作者:行者123 更新时间:2023-12-01 13:58:48 27 4
gpt4 key购买 nike

我的代码有什么问题。

我创建了一个 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
{

}
}

最佳答案

您的 ListListener 类不应扩展 JFrame。

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

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

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

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

我建议您阅读关于如何使用列表的 Swing 教程,了解使用 ListSelectionListener 的工作示例。它还将为您提供更好的程序设计。

关于Java JList 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19467663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com