Reputation: 1
When I try to add a JButton and a JLabel into a JFrame, they both conflict each other in which all the JButtons would disappear and only the JLabel would be visible. The JLabel for some reason would go to the left most side of the JFrame instead of the desired location I set it to go. I'm new to GUI related material and I'm willing to learn from these mistakes.
Here is my code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Windowb extends JFrame{
static String title = "This is a JFrame";
static int width = 500;
static int height = 400;
private static final int BUTTON_LOCATION_X = 46;
private static final int BUTTON_LOCATION_Y = 80;
public static void main(String[]args){
Windowb simple = new Windowb(title, width, height);
JPanel p = new JPanel();
p.setLayout(null);
JLabel c1 = new JLabel("Name: ");
JButton b1 = new JButton("Name:");
JButton b2 = new JButton("Grade:");
JButton b3 = new JButton("GPA");
b1.setBounds(BUTTON_LOCATION_X, BUTTON_LOCATION_Y, 90, 20);
b2.setBounds(50, 170, 90, 20);
b3.setBounds(50, 240, 90, 20);
c1.setLocation(100, 250);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "ActionListener is working!");
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "The second one works too!");
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "Surprise!");
}
});
p.add(b1);
p.add(b2);
p.add(b3);
simple.add(p);
simple.add(c1);
}
public Windowb(String t, int w, int h){
setVisible(true);
setResizable(true);
setSize(w, h);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(500, 100);
setTitle(t);
}
}
Upvotes: 0
Views: 103
Reputation: 1405
You probably should use a LayoutManager. See the layout manager tutorial: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
setBounds() is used by the layout manager to position components. You can set the LayoutManager to null and position the components yourself but stuff like window resizing isn't handled for you that way (ie. components and space aren't scaled accordingly). If you want to maintain your sanity then don't use the build in GridBag layout! It will drive you insane! For more complex layouts use http://www.miglayout.com/ or http://www.jgoodies.com/freeware/libraries/forms/ . For simple layouts use layoutmanagers like BorderLayout.
If you really don't want to use a layoutmanager then use a JPanel. A JFrame can only hold a single component so thats your problem. Put a JPanel inside the JFrame and put your components in the JPanel.
Upvotes: 2