简体   繁体   English

JFrame没有在正确的尺寸上制作

[英]JFrame not being made at right dimensions

This code, when run, will make a window but not at the specified dimensions. 运行此代码时,将生成一个窗口但不是指定的维度。 What is wrong with it? 这有什么问题?

import javax.swing.*;
import java.awt.*;

public class Windowing {
    void JFrame(){
        JFrame frames = new JFrame("Total recall");
        frames.setSize(1000,8000);
        frames.setVisible(true);
        frames.pack();
        //Buttons push = new Buttons();
        //((Buttons) push).buttons();
        JTextField wager = new JTextField(1);
        wager.setSize(100,200);
        wager.setVisible(true);
        wager.setLocation(100, 200);
        frames.add(wager);
        //frames.add(push);
    }
}

You could remove the call to frames.pack() ; 你可以删除对frames.pack()的调用; it overrides the previously set frame size. 它会覆盖先前设置的帧大小。

However, what you really want to do is remove the frames.setSize(1000,8000) and move frames.pack() down to the bottom of the method; 但是,您真正想要做的是删除frames.setSize(1000,8000)并将frames.pack()向下移动到方法的底部; that will ensure that the frame is big enough to display its contents but not too big to fit on the screen. 这将确保框架足够大,可以显示其内容,但不能太大,无法放在屏幕上。

If you call pack before adding anything to the frame (like you are doing now), it will make the window extremely small; 如果你在向帧添加任何内容之前调用pack (就像你现在正在做的那样),它会使窗口非常小; it's probably appearing near the upper left of your screen, but you won't notice it unless you know where to look. 它可能出现在屏幕的左上角附近,但除非你知道在哪里看,否则你不会注意到它。

It looks like you have a number of "opportunity areas" here. 看起来你在这里有很多“机会区”。

  • To start, it seems like you set frame size to 1000x8000 because you didn't see any change right? 首先,您似乎将帧大小设置为1000x8000,因为您没有看到任何更改吗?

  • Secondly you call setVisible on the textField because you didn't see that either. 其次,你在textField上调用setVisible ,因为你也没有看到。

  • And finally you're setting the size of the textfield ( I guess because you're seeing it take the whole frame ) 最后你要设置文本字段的大小(我想因为你看到它占据了整个框架)

The problem here is that you have to invoke pack and setVisible at the end of the construction. 这里的问题是你必须在构造结束时调用packsetVisible Also, you have to learn how to use layout managers and frames. 此外,您还必须学习如何使用布局管理器和框架。

Swing, is very powerful, but it is a bit hard to grasp at the beginning. Swing,非常强大,但在开始时有点难以掌握。

These two links will be helpful: 这两个链接将有所帮助:

I've changed your code and the result looks like this: 我已经更改了你的代码,结果如下:

替代文字

Here's the modified source code. 这是修改后的源代码。

import javax.swing.*;
import java.awt.*;

public class Windowing {
    public static void main( String [] args ) {
        Windowing windowing = new Windowing();
        windowing.showFrame();
    }
    void showFrame(){
        JFrame frame = new JFrame("Total recall");
        JButton push = new JButton("Push");
        JTextField wager = new JTextField(15);

        // Panels do have "FlowLayout"
        JPanel panel = new JPanel();
        panel.add(wager);
        panel.add(push);

        frame.add( panel );
        frame.pack();
        frame.setVisible(true);

    }
}

尝试使用setPreferredSize(Dimension)代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM