只有在调整窗口大小后才会显示Java项目 [英] Java items appear only after the window is resize

查看:190
本文介绍了只有在调整窗口大小后才会显示Java项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 2 JPanels in a frame. The first panel contains java items like buttons etc. The two buttons I added appears but the JSpinner appear just after I resize the window. I guess this will happen also with other items I will add. How could I solve this problem?

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


推荐答案

你在在JFrame上调用 setVisible(true)之后,将组件添加到GUI ,这是因为您在任何事情发生之前渲染GUI添加,所以有意义的是,在重新绘制所有内容之后,之后添加的内容将不会显示。

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.

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

Instead, first add all your components, and only then render the GUI by calling setVisible(true) on the JFrame.

修改


此外,您还要避​​免调用 setSize(...)关于任何事情,而是让组件使用他们的preferredSizes自行调整大小,并在JFrame上调用 pack()在显示它之前用 setVisible(true)

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).

编辑2

例如:

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

}

这篇关于只有在调整窗口大小后才会显示Java项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆