加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 为什么我的JFrame的内容无法正确显示?

发布时间:2020-12-15 05:01:13 所属栏目:Java 来源:网络整理
导读:为了减少代码,我正在取出与问题无关的代码,例如addActionListener();等等 我的主要课程是公共课TF2_Account_Chief 我有我的主Jframe f;及其内容: private static JFrame f = new JFrame("TF2 Account C.H.I.E.F.");private JLabel runnableTogetherLabel =
为了减少代码,我正在取出与问题无关的代码,例如addActionListener();等等

我的主要课程是公共课TF2_Account_Chief

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

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!");

我设置了所有内容的属性:

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没有正确显示内容:

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.

新框架如下所示:

关于为什么内容没有正确显示的任何想法?

(不正确地说,我的意思是JButton iB占据了整个帧,而帧是浅蓝色而不是普通的灰色.)

解决方法

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

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

iF.setLayout(null);

但这不是一个好习惯,我建议你学习how to use Layouts并将所有工作留给布局管理员

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读