为什么我的JFrame的内容无法正确显示? [英] Why are the contents of my JFrame not displaying correctly?

查看:90
本文介绍了为什么我的JFrame的内容无法正确显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了减少代码量,我删除了与该问题无关的代码,例如addActionListener();等.

For the sake of less code, I am taking out code that is unrelevant to the problem, such as addActionListener(); etc.

我的主要课程是public class TF2_Account_Chief

我有我的主要Jframe f;及其内容:

I have my main Jframe f; and its contents:

private static JFrame f = new JFrame("TF2 Account C.H.I.E.F.");
private JLabel runnableTogetherLabel = new JLabel("How many idlers would you like to run at a time?");
private static JTextField runnableTogetherInput = new JTextField();
private JButton runButton = new JButton("Run!");
private JButton stopButton = new JButton("Stop!");
private JButton resetButton = new JButton("Reset!");
private JButton exitButton = new JButton("Exit!");

然后设置所有内容的属性:

and I set the properties of all the contents:

    public void launchFrame() {
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
        f.add(runnableTogetherInput);
        f.add(runnableTogetherLabel);
        f.add(runButton);
        f.add(stopButton);
        f.add(resetButton);
        f.add(exitButton);
        f.setSize(625, 500);
        runnableTogetherInput.setSize(35, 20);
        runnableTogetherLabel.setSize(275, 25);
        runButton.setSize(60, 25);
        stopButton.setSize(65, 25);
        resetButton.setSize(70, 25);
        exitButton.setSize(60, 25);
        f.setLocation(0, 0);
        runnableTogetherInput.setLocation(285, 3);
        runnableTogetherLabel.setLocation(5, 0);
        runButton.setLocation(330, 0);
        stopButton.setLocation(395, 0);
        resetButton.setLocation(465, 0);
        exitButton.setLocation(540, 0);
    }

然后我有我的main()方法:

public static void main(String[] args) throws IOException {
    TF2_Account_Chief gui = new TF2_Account_Chief();
    gui.launchFrame();
    Container contentPane = f.getContentPane();
    contentPane.add(new TF2_Account_Chief());
}

然后我的第二个JFrame iF不能正确显示内容:

And then I have my second JFrame iF which is not displaying the contents correctly:

private void invalidInput() {
    JFrame iF = new JFrame("Invalid Input!");
    JLabel iL = new JLabel("The input you have entered is invalid!");
    JButton iB = new JButton("Exit!");
    Container contentPane2 = iF.getContentPane();
    contentPane2.add(new TF2_Account_Chief());
    iF.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    iF.pack();
    iF.setVisible(true);
    iF.add(iL);
    iF.add(iB);
    iF.setSize(500, 300);
    iL.setSize(125, 25);
    iB.setSize(60, 25);
    iF.setLocation(0, 0);
    iL.setLocation(0, 15);
    iB.setLocation(0, 45);
}

现在,在调用invalidInput()方法时启动了JFrame iF,但是您看不到它,因为那部分代码与问题无关.重要的是JFrame iF已启动.

Now, JFrame iF is launched when the invalidInput() method is called, but you can't see that because that part of the code is unrelevant to the problem. What does matter is that the JFrame iF is launched.

新框架如下所示:

关于内容为何无法正确显示的任何想法?

Any ideas on why the contents are not displaying properly?

(通过不正确的说法,我是说JButton iB占据了整个框架,并且框架是浅蓝色而不是正常的灰色.)

(By improperly, I mean the JButton iB takes up the whole frame and the frame is a light blue instead of the normal grey.)

推荐答案

您使用的是绝对位置而不使用null布局,这就是为什么您看到一个大按钮的原因.

You are using absolute positions without using a null layout, that's why you see a large button.

要查看每个组件,您必须使用

To see every component, you have to use

iF.setLayout(null);

但是这不是一个好习惯,我建议您学习

but it's not a good practice, I'd suggest you to learn how to use Layouts and leave all the work to the layout manager

这篇关于为什么我的JFrame的内容无法正确显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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