我编写了一个具有按钮和JList的JFrame,当我单击按钮时,将显示JList列表。相反,除非我单击 maximaze 或刷新框架,否则它不会显示任何内容。 按钮监听器类
class b0listener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
MessageList.removeAll();
Messages = new JList(lireRepertoire("C:/Documents and Settings/Java/eclipse data file"));
Pane =new JScrollPane(Messages);
Pane.setPreferredSize(new Dimension(400,400));
//Messages.setMaximumSize(MessageList.getPreferredSize()) ;
MessageList.add( Pane);
}}
类构造函数代码
Fframe.setTitle("Boite Message");
Fframe.setSize(800,300);
Fframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Fframe.setVisible(true);
// panels Layout
ButtonMenu.setLayout(new BoxLayout(ButtonMenu, BoxLayout.Y_AXIS));//Jpanel
MessageList.setLayout(new FlowLayout());//JPanel
ButtonMenu.setBackground(Color.LIGHT_GRAY);
MessageList.setBackground(Color.orange);
MessageList.setPreferredSize(new Dimension(400, 400));
Fframe.add(ButtonMenu,BorderLayout.WEST);
Fframe.add(MessageList,BorderLayout.CENTER);
ButtonMenu.add(b0);
b0.addActionListener(new b0listener());
Pane =new JScrollPane(Messages);
Messages.setPreferredSize(new Dimension(800,250));
//Pane.setMaximumSize(MessageList.getSize()) ;
MessageList.add( Pane);
我已经将 Jpanels 和 Jframe 、按钮声明为类成员
最佳答案
首先,学习并使用正确的 Java 命名约定。变量不应以大写字符开头。
当从面板中动态添加/删除组件时,您需要重新验证()面板:
messageList.add( pane);
messageList.revalidate();
messageList.repaint();
关于java - JButton Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7364345/