简体   繁体   中英

Why does my button disappear from my borderlayout when I add an actionListener to it?

I am writing the back end code for my java project which is implementing a fitness logger. For some reason when I put the actionListener function to a button in my Border Layout then the button disappears.

I tried setting the function in different places in the constructor.

public class Buttons extends JFrame implements ActionListener {

      JFrame frame = new JFrame("Menu");
      JPanel MyPanel= new JPanel();

      JButton b1= new JButton("Daily Logger");
      JButton b2= new JButton("View Weekly Logs");
      JButton b3= new JButton("Weight Calculator");
      JButton b4= new JButton("BMI Calculator");
      JButton b5= new JButton("Log Out");

      public Buttons(){
        MyPanel.setLayout(new BorderLayout());
        MyPanel.add(b1, "North");
        MyPanel.add(b2, "Center");
        MyPanel.add(b3, "East");
        MyPanel.add(b4, "West");
        MyPanel.add(b5, "South");

        b1.addActionListener(this);
        add(b1);

        frame.getContentPane().add(MyPanel, "North");
        frame.setSize(500,115);
        frame.setVisible(true);
      }

      public static void main(String[] args) {
        new Buttons();
      }


      @Override
      public void actionPerformed(ActionEvent e) {
          String command = e.getActionCommand();

          if(command.equals("Daily Logger"))
              myMethod();
      }

      public void myMethod() {
          JOptionPane.showMessageDialog(this,"Onto the next step");
      }
}

I expected the button to show up in the border layout when I add the actionListener function but it disappears. If the button did work as it should it should implement the myMethod() function. My main goal is to show my JTable that I created in another class to show up when the button is pressed.

add(b1);

Should be removed. The code has already added it via:

MyPanel.add(b1, "North");

A BorderLayout accommodates up to five components, each in a separate layout constraint. If a component is added twice, in different areas, it faces two problems:

  1. A component can only appear in one place.
  2. The component 'overwrites' the original component added to that area of the layout.

More general tips:

  1. Please learn common Java nomenclature (naming conventions - eg EachWordUpperCaseClass , firstWordLowerCaseMethod() , firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT ) and use it consistently.
  2. For better help sooner, add a minimal reproducible example or Short, Self Contained, Correct Example . Note: The posted code only needed appropriate import statements to be an MRE / SSCCE.
  3. MyPanel.add(b3, "East"); re East
    • There are constants for this. EG BorderLayout.EAST . always use the constants for compile time checking.
    • But BorderLayout.LINE_END is sensitive to the locale. It will appear on the RHS for left to right languages, and the left for right to left languages.
  4. All Swing & AWT GUIs should be created & updated on the EDT (Event Dispatch Thread).
  5. The code both extends and keeps a reference to, JFrame . Keep the latter, ditch the former.
  6. Be sure to add the java (language) and swing (GUI toolkit) tags to questions! The only reason I saw this was because I (extraordinarily) checked the question listing for the jframe tag! To the best of my recollection, it's the first time I've ever checked that tag's question listing. Even more ironic, it did not make the list of 5 tags I saw as being most relevant to this question.
  7. frame.getContentPane().add(.. can be shortened to frame.add(.. since Java 1.5.
  8. frame.setSize(500,115); should better be frame.pack(); , since 500 x 115 is no better than a guess, and will be 'wrong' for different OS' (the size of the content pane will change due to different frame decorations per system).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM