Components not appearing (need help!)
Good day! I'm a complete noob in GUI programming so I wanted to know whats wrong with my code. I can compile the program without a problem but when the GUI appears none of my components appear. Any help would be appreciated. thanks!
Code :
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MenuList {
private JFrame page1;
private JPanel panel;
private JButton burger;
private JButton rice;
private JLabel label;
public MenuList() {
gui();
}
public void gui() {
page1 = new JFrame("FAST FOOD");
page1.setVisible(true);
page1.setSize(600,400);
page1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new BorderLayout());
//BUTTONS
burger = new JButton("Burger");
burger.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "BURGER"); }
});
rice = new JButton("Rice");
label = new JLabel("ORDER HERE");
//END (BUTTONS)
//LAYOUT
GroupLayout layout = new GroupLayout(panel);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(burger)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
);
//END (LAYOUT)
}
public static void main(String[] args) {
new MenuList();
}
}
Re: Components not appearing (need help!)
When do you add your components to the JPanel? When do you add your JPanel to the JFrame?
Re: Components not appearing (need help!)
I don't think I've added my components to the Jpanel yet. Same goes for my Jpanel to Jframe.
Re: Components not appearing (need help!)
Quote:
Originally Posted by
hawkeye01
I don't think I've added my components to the Jpanel yet. Same goes for my Jpanel to Jframe.
That's your problem then. Components aren't visible until you add them to something.
Re: Components not appearing (need help!)
When I write GUI code, I follow a specific layout of how I arrange my code. There are different ways of placing the event-listener code in the GUI, so feel free to use a different way if you wish. Keep in mind, the JFrame is what ultimately contains all of your JComponents but computers aren't smart enough to automatically put the JComponents onto the JFrame. Likewise, if you make a JPanel, you have to add whichever JComponents to it, then add it to the JFrame.
Code Java:
// import statements
public class myGui extends JFrame {
// GUI components that are used by the ActionListener (or whichever other event listener)
public class myEventListener implements ActionListener (or whichever other event listener) {
// event-listener code
}
public myGui() {
// code for creating the GUI and its components
// at the end, register the event listeners.
}
}
--- Update ---
When I write GUI code, I follow a specific layout of how I arrange my code. There are different ways of placing the event-listener code in the GUI, so feel free to use a different way if you wish. Keep in mind, the JFrame is what ultimately contains all of your JComponents but computers aren't smart enough to automatically put the JComponents onto the JFrame. Likewise, if you make a JPanel, you have to add whichever JComponents to it, then add it to the JFrame.
Code Java:
// import statements
public class myGui extends JFrame {
// GUI components that are used by the ActionListener (or whichever other event listener)
public class myEventListener implements ActionListener (or whichever other event listener) {
// event-listener code
}
public myGui() {
// code for creating the GUI and its components
// at the end, register the event listeners.
}
}
Re: Components not appearing (need help!)
Quote:
Originally Posted by
SunshineInABag
When I write GUI code, I follow a specific layout of how I arrange my code. There are different ways of placing the event-listener code in the GUI, so feel free to use a different way if you wish. Keep in mind, the JFrame is what ultimately contains all of your JComponents but computers aren't smart enough to automatically put the JComponents onto the JFrame. Likewise, if you make a JPanel, you have to add whichever JComponents to it, then add it to the JFrame.
Code Java:
// import statements
public class myGui extends JFrame {
// GUI components that are used by the ActionListener (or whichever other event listener)
public class myEventListener implements ActionListener (or whichever other event listener) {
// event-listener code
}
public myGui() {
// code for creating the GUI and its components
// at the end, register the event listeners.
}
}
Some nitpicking- you almost never have a reason to extend JFrame, and your classes should start with upper-case letters.
Re: Components not appearing (need help!)
I was able to solve my problem by adding my components to the panel and adding my panel to my frame. thanks for the help guys!