Reputation: 133
I'm in the middle of working on a program. I created a JFrame with a bunch of panels, buttons, labels and textfields that are supposed to be inside it. For some reason the JFrame apears, but with nothing inside it. Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.Event.*;
public class GUI extends JFrame {
JButton rect,oval,tri,free,addPoint;
JLabel xLabel,yLabel;
JTextField xTextField,yTextField;
JPanel leftPanel,rightPanel,optionsPanel,pointsPanel;
public GUI(){
initUI();
}
private void initUI(){
setLayout(new GridLayout(1,2,5,5));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Graphics Generator");
setSize(500,500);
setVisible(true);
add(leftPanel);
add(rightPanel);
leftPanel.setLayout(new GridLayout(2,1,5,5));
leftPanel.add(optionsPanel);
optionsPanel.setLayout(new GridLayout(1,4,2,2));
rect = new JButton("Rectangle");
oval = new JButton("Oval");
tri = new JButton("Triangle");
free = new JButton("Free Shape");
optionsPanel.add(rect);
optionsPanel.add(oval);
optionsPanel.add(tri);
optionsPanel.add(free);
leftPanel.add(pointsPanel);
pointsPanel.setLayout(new GridLayout(1,5,2,2));
pointsPanel.add(xLabel);
pointsPanel.add(xTextField);
pointsPanel.add(yLabel);
pointsPanel.add(yTextField);
pointsPanel.add(addPoint);
}
public static void main(String[] args) {
GUI gui = new GUI();
}
}
Upvotes: 0
Views: 83
Reputation: 208944
None of your panels, textfields, or labels have been initialized. I'm getting NullPointerException
The code below will get your program running. But you really need to look into some LayoutManagers
private void initUI(){
leftPanel = new JPanel();
rightPanel = new JPanel();
optionsPanel = new JPanel();
pointsPanel = new JPanel();
yLabel = new JLabel();
xLabel = new JLabel();
xTextField = new JTextField();
yTextField = new JTextField();
add(leftPanel);
add(rightPanel);
leftPanel.setLayout(new GridLayout(2,1,5,5));
leftPanel.add(optionsPanel);
optionsPanel.setLayout(new GridLayout(1,4,2,2));
rect = new JButton("Rectangle");
oval = new JButton("Oval");
tri = new JButton("Triangle");
free = new JButton("Free Shape");
addPoint = new JButton("Add Point");
optionsPanel.add(rect);
optionsPanel.add(oval);
optionsPanel.add(tri);
optionsPanel.add(free);
leftPanel.add(pointsPanel);
pointsPanel.setLayout(new GridLayout(1,5,2,2));
pointsPanel.add(xLabel);
pointsPanel.add(xTextField);
pointsPanel.add(yLabel);
pointsPanel.add(yTextField);
pointsPanel.add(addPoint);
setLayout(new GridLayout(1,2,5,5));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Graphics Generator");
setSize(500,500);
setVisible(true);
}
Upvotes: 1
Reputation: 109815
your JComponents
aren't initialized,
you added JComponents
to already visible JFrame
,
you have to move code line setVisible(true);
to the end of constructor,
Swing GUI should be crated on Initial Thread
Upvotes: 5
Reputation: 1615
You should invoke setVisible(true)
after you're done adding your components. They won't render otherwise until there's a call to revalidate()
;
You also need to initialise your components before trying to use them.
Example:
leftPanel = new JPanel();
rightPanel = new JPanel();
add(leftPanel);
add(rightPanel);
Repeat for the other components.
Upvotes: 1