通过匿名操作侦听器类

时间:2017-10-14 01:03:01

标签: java swing

我创建了一个匿名的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);   
}

1 个答案:

答案 0 :(得分:1)

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

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

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