只有在调整窗口大小后才会显示Java项目

时间:2012-01-07 15:07:29

标签: java swing jpanel

我在一个帧中有2个JPanel。第一个面板包含按钮等java项目。我添加的两个按钮出现,但JSpinner在我调整窗口大小后出现。我想这也将与我将添加的其他项目一起发生。我怎么能解决这个问题?

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;

public class StartingPoint {

static JFrame window;
static DrawingArea draw;
static JButton b1, b2;
static JPanel userInt;
static JSpinner gravitySpinner;

public static void main(String[] args) {
    window = new JFrame("Ball");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(600, 400);
    window.setLayout(new BorderLayout());
    window.setVisible(true);

    draw = new DrawingArea();
    window.add(draw, BorderLayout.CENTER);

    userInt = new JPanel();
    window.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);
}
}

2 个答案:

答案 0 :(得分:30)

在在JFrame上调用setVisible(true)之后,您正在向GUI 添加组件,这是因为您在添加任何内容之前渲染GUI,因此它是有意义的之后添加的内容在重新绘制之前不会显示。

相反,首先添加所有组件,然后只有然后通过调用JFrame上的setVisible(true)来呈现GUI。

修改
此外,您还要避​​免在任何内容上调用setSize(...),而是让组件使用其preferredSizes调整大小,并在使用pack()显示之前调用JFrame上的setVisible(true)

编辑2
例如:

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);
   }

}

答案 1 :(得分:4)

在最后添加setVisible(true),它可以正常工作。