Note that there are some explanatory texts on larger screens.

plurals
  1. POJava swing questions in ActionListener
    primarykey
    data
    text
    <pre><code>//GUI.java public class GUI extends JFrame implements ActionListener { private static final long serialVersionUID = 870343916997182570L; private JPanel btmPanel; public GUI(String arg0) throws HeadlessException { super(arg0); createGUI(); } private void createGUI() { setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); //ResultPanel rslt = new ResultPanel(); //this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER); btmPanel = new JPanel(); btmPanel.setBackground(Color.LIGHT_GRAY); btmPanel.setLayout(new FlowLayout()); JButton blueSearch = new JButton("Search"); blueSearch.setBackground(Color.WHITE); blueSearch.addActionListener(this); btmPanel.add(blueSearch); JButton blackChart = new JButton("Chart"); blackChart.setBackground(Color.WHITE); blackChart.addActionListener(this); btmPanel.add(blackChart); this.getContentPane().add(btmPanel, BorderLayout.SOUTH); } @Override public void actionPerformed(ActionEvent e) { String buttonString = e.getActionCommand(); if (buttonString.equals("Search")) { ResultPanel rslt = new ResultPanel(); this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER); } } } //ResultPanel.java public class ResultPanel extends JPanel implements ActionListener { private static final long serialVersionUID = -7851502165390304971L; private JPanel textPanel; private JTextArea textDisplay; public ResultPanel() { textPanel = new JPanel(); textDisplay = new JTextArea("Text Area:"); } public JPanel createPanel() { textDisplay.setEditable(true); textPanel.setBackground(Color.LIGHT_GRAY); textPanel.setLayout(new BorderLayout()); textPanel.add(textDisplay,BorderLayout.CENTER); return textPanel; } @Override public void actionPerformed(ActionEvent e) { } } </code></pre> <p>I have two buttons on the main frame, and I hope to change the panel when I press the button.</p> <p>The question is that the code in "actionPerformed" doesn't work,</p> <p>but it works well if I put them in the creatGUI()....(see the marked section).</p> <p>Is that anything wrong ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload