Reputation:
When I run the program the window and all it's properties are right, but the buttons won't show up, any idea of what I've done wrong?
I have two classes window and TimeTable0:
Here's window:
package timetable0;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class window extends JFrame {
JButton bt1,bt2,bt3,bt4 = new JButton();
JPanel panel = new JPanel();
public void ventana() {
setResizable(false);
setTitle("Time Table");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,550);
setVisible(true);
bt1.setText("Show Grades");
bt2.setText("Show Time Table");
bt3.setText("");
bt4.setText("");
panel.add(bt1);
panel.add(bt2);
panel.add(bt3);
panel.add(bt4);
}
public void actions (){
bt1.addActionListener((ActionEvent e) -> {
System.out.println("");
});
bt2.addActionListener((ActionEvent e) -> {
System.out.println("");
});
bt3.addActionListener((ActionEvent e) -> {
System.out.println("");
});
bt4.addActionListener((ActionEvent e) -> {
System.out.println("");
});
}
}
And here's TimeTable0:
package timetable0;
public class TimeTable0 {
public static void main(String[] args) {
window menu = new window();
menu.ventana();
menu.actions();
}
}
Upvotes: 1
Views: 88
Reputation: 1622
Initialize buttons one by one:
JButton bt1 = new JButton();
JButton bt2 = new JButton();
JButton bt3 = new JButton();
JButton bt4 = new JButton();
Add panel to your frame after you add buttons to your panel:
panel.add(bt1);
panel.add(bt2);
panel.add(bt3);
panel.add(bt4);
setContentPane(panel);
Upvotes: 0
Reputation: 17474
You didn't show much codes there, but you should at least try to do this instead:
Note that JButton bt1,bt2,bt3,bt4 = new JButton();
is different from writing it as below:
JButton bt1 = new JButton();
JButton bt2 = new JButton();
JButton bt3 = new JButton();
JButton bt4 = new JButton();
Doing JButton bt1,bt2,bt3,bt4 = new JButton();
, you are only creating a JButton object for bt4
and not the rest.
You have also forgotten to add your JPanel into your JFrame.
add(panel); //Add panel to frame
Upvotes: 0
Reputation: 1480
You will need to initialize the buttons and add panel
to the JFrame. Also, try moving the setVisible(true)
statement to the end of the ventana()
method so that it is displayed after the components are added.
public void Ventana() {
bt1 = new JButton("Show Grades");
bt2 = new JButton("Show Time Table");
bt3 = new JButton();
bt4 = new JButton();
...
panel.add(bt4);
add(panel);
setVisible(true); //moved from top
}
Upvotes: 0
Reputation: 17474
I would have done it this way if you really want to extends your class to a JFrame.
public class Window extends JFrame {
JButton bt1,bt2,bt3,bt4; //Do initialization in the constructor, not here
JPanel panel;
public Window() {
createComponents();
addComponents();
initFrame();
}
private void createComponents(){
panel = new JPanel();
panel.setPreferredSize(new Dimension(800, 600));
bt1 = new JButton("Show Grades");
bt2 = new JButton("Show Time Table");
bt3 = new JButton("btn 3");
bt4 = new JButton("btn 4");
}
private void addComponents(){
panel.add(bt1);
panel.add(bt2);
panel.add(bt3);
panel.add(bt4);
add(panel);
}
private void initFrame(){
setResizable(false);
setTitle("Time Table");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
}
Usually I would prefer to extends a class to JPanel instead of extending it to JFrame. After that create a JFrame in the main() and add your customized JPanel into it.
Upvotes: 1
Reputation: 32343
You aren't adding the JPanel
to the frame.
panel.add(bt1);
panel.add(bt2);
panel.add(bt3);
panel.add(bt4);
setContentPane(panel);
You aren't running the program on the Event Dispatch Thread:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
window menu = new window();
menu.ventana();
menu.actions();
}
});
}
You aren't creating all four buttons:
JButton bt1 = new JButton();
JButton bt2 = new JButton();
JButton bt3 = new JButton();
JButton bt4 = new JButton();
Upvotes: 4