【问题标题】:Adding a JPanel to JFrame through an anonymous action listener class通过匿名操作侦听器类将 JPanel 添加到 JFrame
【发布时间】:2017-10-14 01:03:01
【问题描述】:

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

代码运行没有编译器错误,问题是由 Action Listener 创建的 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 的私有 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);   
}

【问题讨论】:

    标签: java swing


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢camickr,我回家试试这个。
    • 我现在已经在代码中添加了重新验证和重新绘制,但是这样做并没有改变结果,GUI 仍然没有显示 JPanel。我已经在更新中包含了代码的详细信息,你认为我在其他地方犯了错误吗? (不在显示的两个类中)。
    • I have now added both revalidate and repaint into the code, - 这不是我的建议。看看我的代码结构。您需要将子组件添加到面板BEFORE 您重新验证面板。如果组件尚未添加到面板中,布局管理器如何为组件指定大小???
    • 我错误地用 JFrame 扩展了这两个类,现在已更改为包括 JFrame 和 JPanel 的扩展。是否可以将私有类 ActionListeners 添加为 JMenu 创建中的 actionlistener?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    相关资源
    最近更新 更多