gpt4 book ai didi

java - 通过匿名操作监听器类将 JPanel 添加到 JFrame

转载 作者:行者123 更新时间:2023-12-02 12:08:09 28 4
gpt4 key购买 nike

我创建了一个匿名操作监听器类,当触发操作时,监听器会创建一个带有 5 个 JTextField 的 JPanel 供用户输入信息。

代码运行时没有编译器错误,问题是由操作监听器创建的 JPanel 未显示在 GUI 上。

匿名操作监听器代码:

private class addListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JPanel addPanel = new JPanel();
GridLayout gl = new GridLayout(5,2);
addPanel.setLayout(gl);

JLabel genreLabel = new JLabel("Genre: ");
JTextField txbGenre = new JTextField();
addPanel.add(genreLabel);
addPanel.add(txbGenre);

JLabel titleLabel = new JLabel("Title: ");
JTextField txbTitle = new JTextField();
addPanel.add(titleLabel);
addPanel.add(txbTitle);

JLabel ratingLabel = new JLabel("Rating: ");
JTextField txbRating = new JTextField();
addPanel.add(ratingLabel);
addPanel.add(txbRating);

JLabel directorLabel = new JLabel("Director: ");
JTextField txbDirector = new JTextField();
addPanel.add(directorLabel);
addPanel.add(txbDirector);

JLabel castLabel = new JLabel("Cast: ");
JTextField txbCast = new JTextField();
addPanel.add(castLabel);
addPanel.add(txbCast);

addPanel.setVisible(true);
MovieListPanel.this.add(addPanel);

}
}

JFrame 的构造函数:

public MovieListPanel()
{
super("Movie Database");
setSize(WIDTH, HEIGHT);
setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

JMenu movieMenu = new JMenu("Movie menu options");

//------------------------------------------------------------
//Sub Menu creation
JMenu searchMenu = new JMenu("Search for a movie");

JMenuItem titleChoice = new JMenuItem("By title: ");
titleChoice.addActionListener(new titleListener());
searchMenu.add(titleChoice);

JMenuItem ratingChoice = new JMenuItem("By rating: ");
ratingChoice.addActionListener(new ratingListener());
searchMenu.add(ratingChoice);

JMenuItem genreChoice = new JMenuItem("By genre: ");
genreChoice.addActionListener(new genreListener());
searchMenu.add(genreChoice);

JMenuItem castChoice = new JMenuItem("By cast: ");
castChoice.addActionListener(new castListener());
searchMenu.add(castChoice);

JMenuItem directorChoice = new JMenuItem("By director: ");
directorChoice.addActionListener(new directorListener());
searchMenu.add(directorChoice);

JMenuItem multiChoice = new JMenuItem("By cast/year/rating:");
multiChoice.addActionListener(new multiSearchListener());
searchMenu.add(multiChoice);

movieMenu.add(searchMenu);
//----------------------------------------------------
//Main menu choices creation
JMenuItem addChoice = new JMenuItem("Add a Movie");
addChoice.addActionListener(new addListener());
movieMenu.add(addChoice);

JMenuItem removeChoice = new JMenuItem("Remove a movie");
removeChoice.addActionListener(new removeListener());
movieMenu.add(removeChoice);

JMenuItem displayChoice = new JMenuItem("Display all movies");
displayChoice.addActionListener(new displayListener());
movieMenu.add(displayChoice);

JMenuBar bar = new JMenuBar();
bar.add(movieMenu);
setJMenuBar(bar);


}

感谢任何帮助。

谢谢

编辑:

更新的代码:

扩展 JPanel 的私有(private) ActionListener 类

private class addListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JPanel addPanel = new JPanel();
GridLayout gl = new GridLayout(5,2);
addPanel.setLayout(gl);

JLabel genreLabel = new JLabel("Genre: ");
JTextField txbGenre = new JTextField();
addPanel.add(genreLabel);
addPanel.add(txbGenre);

JLabel titleLabel = new JLabel("Title: ");
JTextField txbTitle = new JTextField();
addPanel.add(titleLabel);
addPanel.add(txbTitle);

JLabel ratingLabel = new JLabel("Rating: ");
JTextField txbRating = new JTextField();
addPanel.add(ratingLabel);
addPanel.add(txbRating);

JLabel directorLabel = new JLabel("Director: ");
JTextField txbDirector = new JTextField();
addPanel.add(directorLabel);
addPanel.add(txbDirector);

JLabel castLabel = new JLabel("Cast: ");
JTextField txbCast = new JTextField();
addPanel.add(castLabel);
addPanel.add(txbCast);


addPanel.setVisible(true);
add(addPanel);
addPanel.revalidate();
addPanel.repaint();

}
}

JFrame类

public class MovieApp extends JFrame{

private MovieListPanel view = new MovieListPanel();

public MovieApp()
{
super("Movie Database");
setSize(WIDTH, HEIGHT);
setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

JMenu movieMenu = new JMenu("Movie menu options");

//------------------------------------------------------------
//Sub Menu creation
JMenu searchMenu = new JMenu("Search for a movie");

JMenuItem titleChoice = new JMenuItem("By title: ");
titleChoice.addActionListener(new titleListener());
searchMenu.add(titleChoice);

JMenuItem ratingChoice = new JMenuItem("By rating: ");
ratingChoice.addActionListener(new ratingListener());
searchMenu.add(ratingChoice);

JMenuItem genreChoice = new JMenuItem("By genre: ");
genreChoice.addActionListener(new genreListener());
searchMenu.add(genreChoice);

JMenuItem castChoice = new JMenuItem("By cast: ");
castChoice.addActionListener(new castListener());
searchMenu.add(castChoice);

JMenuItem directorChoice = new JMenuItem("By director: ");
directorChoice.addActionListener(new directorListener());
searchMenu.add(directorChoice);

JMenuItem multiChoice = new JMenuItem("By cast/year/rating:");
multiChoice.addActionListener(new multiSearchListener());
searchMenu.add(multiChoice);

movieMenu.add(searchMenu);
//----------------------------------------------------
//Main menu choices creation
JMenuItem addChoice = new JMenuItem("Add a Movie");
addChoice.addActionListener(new addListener());
movieMenu.add(addChoice);

JMenuItem removeChoice = new JMenuItem("Remove a movie");
removeChoice.addActionListener(new removeListener());
movieMenu.add(removeChoice);

JMenuItem displayChoice = new JMenuItem("Display all movies");
displayChoice.addActionListener(new displayListener());
movieMenu.add(displayChoice);

JMenuBar bar = new JMenuBar();
bar.add(movieMenu);
setJMenuBar(bar);
}

最佳答案

当您从可见 GUI 添加(或删除)组件时,基本逻辑是:

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

默认情况下,所有组件的大小均为 (0, 0),因此您需要 revalidate() 来调用布局管理器,该布局管理器将根据布局管理器的规则为每个组件提供大小/位置。

关于java - 通过匿名操作监听器类将 JPanel 添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46739831/

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