You're adding components to the GUI after calling setVisible(true)
on the JFrame and that's backwards since you're rendering the GUI before anything has been added, and so it makes sense that things added later won't be displayed until all is repainted.
Instead, first add all your components, and only then render the GUI by calling setVisible(true)
on the JFrame.
Edit
Also, you'll want to avoid calling setSize(...)
on anything but instead let components size themselves using their preferredSizes and by calling pack()
on the JFrame prior to displaying it with setVisible(true)
.
Edit 2
For example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
public class StartingPoint {
private DrawingArea draw;
private JButton b1, b2;
private JPanel userInt;
private JSpinner gravitySpinner;
private JPanel mainPanel = new JPanel();
public StartingPoint() {
mainPanel.setLayout(new BorderLayout());
draw = new DrawingArea();
mainPanel.add(draw, BorderLayout.CENTER);
userInt = new JPanel();
mainPanel.add(userInt, BorderLayout.NORTH);
b1 = new JButton("Start");
b2 = new JButton("aaa");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
draw.setUp();
}
});
userInt.add(b1);
userInt.add(b2);
SpinnerNumberModel gravityModel = new SpinnerNumberModel(.9, .1, 2, .1);
gravitySpinner = new JSpinner(gravityModel);
userInt.add(gravitySpinner);
}
public JPanel getMainPanel() {
return mainPanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame window = new JFrame("Ball");
window.add(new StartingPoint().getMainPanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
});
}
}
class DrawingArea extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 400;
public void setUp() {
// TODO finish
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…