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

标签 java swing

我创建了一个匿名操作监听器类,当触发操作时,监听器会创建一个带有 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/

相关文章:

java - 为什么即使在设置 JPanel 的大小后,我的框布局也会更改大小

java - 在 Android 中调用 REST Web 服务并在字符串数据中获取 null

java - 将 GIF 图像转换为 PNG 格式,32 位深度

java - 如何解决 S3 错误 : org. jets3t.service.S3ServiceException : S3 GET failed? Java

java - Java Swing程序运行时显示不一致

java swing jlist数据传输

Java JFrame ComboBox情况

java - 如何在 Spring Boot 项目上禁用 activiti 自动部署

Java - 使用 POST 上传文件到 URL,然后检索响应

java - 有没有办法在 jbutton 之上设置 jbutton?