Reputation: 199
This is my code
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
public class Test extends JFrame{
private JButton by;
private JButton bn;
JLabel startQuestion;
public Test() {
JPanel p = new JPanel();
by = new JButton("Yes");
bn = new JButton("No");
startQuestion = new JLabel("Would you like to start?");
p.setLayout(null);
by.setBounds(180, 250, 70, 45);
bn.setBounds(250, 250, 70, 45);
startQuestion.setLocation(195, 240);
p.add(by);
p.add(bn);
add(startQuestion);
add(p);
setDefaultCloseOperation(3);
setSize(500, 500);
setVisible(true);
}
public static void main(String...args) {
new Test();
}
}
There are no syntax errors, however my Jlabel is not added (the buttons work fine). I do not want to change the layout from null, as I want to be able to define the positions of the buttons and labels. Does anyone know why this isn't working?
Upvotes: 2
Views: 71
Reputation: 1956
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
public class Test extends JFrame{
private JButton by;
private JButton bn;
JLabel startQuestion;
public Test() {
JPanel p = new JPanel();
by = new JButton("Yes");
bn = new JButton("No");
startQuestion = new JLabel("Would you like to start?");
//p.setLayout(null);
by.setBounds(180, 250, 70, 45);
bn.setBounds(250, 250, 70, 45);
startQuestion.setLocation(195, 240);
p.add(by);
p.add(bn);
add(startQuestion);
add(p);
setDefaultCloseOperation(3);
setSize(500, 500);
setVisible(true);
}
public static void main(String...args) {
new Test();
}
}
you need to comment setlayout(null);
Upvotes: 0
Reputation: 1399
This question is similar to JLabel won't show with JPanel.setLayout(null). Why?
To answer it here, add a line like this:
startQuestion.setBounds(150, 150, 200, 20);
Upvotes: 2