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:
More general tips:
EachWordUpperCaseClass
, firstWordLowerCaseMethod()
, firstWordLowerCaseAttribute
unless it is an UPPER_CASE_CONSTANT
) and use it consistently. import
statements to be an MRE / SSCCE. MyPanel.add(b3, "East");
re East
BorderLayout.EAST
. always use the constants for compile time checking. 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. JFrame
. Keep the latter, ditch the former. 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. frame.getContentPane().add(..
can be shortened to frame.add(..
since Java 1.5. 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.