栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java项目仅在调整窗口大小之后出现

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java项目仅在调整窗口大小之后出现

您需要 调用

setVisible(true)
Jframe 之后
向GUI添加组件,这是向后的,因为在添加任何东西之前渲染GUI都是有意义的,因此以后添加的所有东西都需要重新粉刷才显示。

取而代之的是,首先添加所有组件,并且只有 然后 通过调用渲染GUI

setVisible(true)
上的Jframe。

编辑
此外,你要避免调用

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496268.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号