初次做swing,一个最简单的程序:public class TimingApp extends SingleFrameApplication {
    @Override protected void startup() {
        show(new TimingView(this));
    }
    @Override protected void configureWindow(java.awt.Window root) {
    }
    public static TimingApp getApplication() {
        return Application.getInstance(TimingApp.class);
    }
    public static void main(String[] args) {
        launch(TimingApp.class, args);
    }
}public class TimingView extends FrameView {
    public TimingView(SingleFrameApplication app) {
        super(app);
        initComponents();
    }    @SuppressWarnings("unchecked")
    private void initComponents() {
        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        mainPanel.setName("mainPanel"); 
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(timing.TimingApp.class).getContext().getResourceMap(TimingView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); 
        jLabel1.setName("jLabel1"); 
        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(168, 168, 168)
                .addComponent(jLabel1)
                .addContainerGap(190, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(125, 125, 125)
                .addComponent(jLabel1)
                .addContainerGap(160, Short.MAX_VALUE))
        );        setComponent(mainPanel);
    }                           private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel mainPanel;         }运行后程序窗口总是最大化了,如何控制窗口的大小?
在TimingView(SingleFrameApplication app)里的initComponents()后增加:
this.getComponent().setBounds(0, 0, 300, 400);
this.getFrame().setBounds(0, 0, 300, 400);
还是一样,...........

解决方案 »

  1.   

    代码不全,没有办法帮你验证以下的建议。
    TimingView继承FrameView,FrameView是Frame吧。
    如果是的话,就设置这个对象的setBounds(x, y, width, height)然后repaint。
    Good luck!
      

  2.   

    给frame设置 setPreferredSize(new Dimension(xxx, yyy));
      

  3.   

    JFrame.setResizable(false);是否这样设置了的哟?
    如果没有那样设置,下面提供三种方法改变JFrame的大小:
    1  JFrame.setSize(int width,int height);
    2  JFrame.setBounds(int   x,   int   y,   int   width,   int   height) 
    3  JFrame.setPreferredSize(new Dimension(int width,int height));
      

  4.   

    还有种忘记说了
    4  JFrame.setSize(new Dimension(int width,int height));
      

  5.   


    抱歉,需要的包没有引入。org.jdesktop.application不熟。
      

  6.   

    import org.jdesktop.application.Application;
    import org.jdesktop.application.SingleFrameApplication;
    import org.jdesktop.application.FrameView;总共用这3个包.上面的方法都不行。程序运行后,我用手动调整窗口的大小,然后关闭程序,再次运行,窗口的大小就是关闭的时候的尺寸,感觉象保存了什么设置在哪里,但肯定不是程序里.........
      

  7.   

    确实是有配置文件被保存了,在你关闭程序的时候。
    因为你继承了SingleFrameApplication类,而SingleFrameApplication在执行关闭方法的时候,会自动保存程序关闭前的会话状态(包括界面显示的大小),而再次启动的时候,首先会去查找是否有保存的会话配置文件,如存在,则加载,这样,你看到的现象就能解释了。
    附API说明:
    public abstract class SingleFrameApplicationextends ApplicationAn application base class for simple GUIs with one primary JFrame. This class takes care of component property injection, exit processing, and saving/restoring session state in a way that's appropriate for simple single-frame applications. The application's JFrame is created automatically, with a WindowListener that calls exit() when the window is closed. Session state is stored when the application shuts down, and restored when the GUI is shown. 
    另外,配置文件,是由另外一个类完成的(SessionStorage),API里有说明,不同的操作系统,默认的配置文件保存路径是不一样的:
     For example, on Windows XP, the full pathname for filename "session.xml" is typically:  ${userHome}\Application Data\${vendorId}\${applicationId}\session.xml
     Where the value of ${userHome} is the the value of the Java System property "user.home". On Solaris or Linux the file is: 
     ${userHome}/.${applicationId}/session.xml
     and on OSX: 
     ${userHome}/Library/Application Support/${applicationId}/session.xml
    另外,如果是Vista系统,路径又略有差异,这个要注意。