【发布时间】: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);
}
【问题讨论】: