JTabbedPane 随机显示自身

发布于 2024-10-04 23:12:07 字数 1716 浏览 5 评论 0原文

我遇到的问题对我来说很奇怪,因为我正在一步一步正确地完成所有事情(在我看来),最后当我可以说我完成了程序的一部分时,它似乎取笑了我。实际的问题是,在我创建的 GUI 中,我使用了 JPanel,然后将其放入 JTabbedPane 中,最后将其放入 JFrame 中。一切都很好,除了有时无法正常工作之外。我知道这听起来很奇怪,但是在运行程序后,一旦我得到了我想要的东西(带有选项卡式窗格的框架,其中包含带有一些内容的面板),然后当我再次运行它时,它要么再次显示正确的内容,要么只是空框架。最糟糕的是,它是如此随机,我不知道什么可能是错误的,我什至不知道我到底应该谷歌什么来找到它。代码是:

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

public class GUI extends JFrame {

JFrame frame = new JFrame("WakeOnLan script generator");
JPanel panel1 = new JPanel(null);
JTextArea text; //= new JTextArea("test");
JScrollPane scroll = new JScrollPane();
JButton but = new JButton("test");
JTabbedPane tab = new JTabbedPane();

public GUI() {

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int w = frame.getSize().width;
    int h = frame.getSize().height;
    int x = (dim.width-w)/3;
    int y = (dim.height-h)/4;

    frame.setSize(500,500);
    frame.setLocation(x,y);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLayout(null);

    createTab1();

    tab.addTab("Tab 1", panel1);
    tab.setVisible(true);
    tab.setBounds(0, 0, 500, 500);

    frame.add(tab);

}

public void createTab1(){

    text = new JTextArea("test");
    text.setVisible(true);

    scroll.setViewportView(text);
    scroll.setBounds(10,10,465,300);

    panel1.setLayout(null);
    panel1.add(scroll);
    panel1.setVisible(true);
    panel1.setSize(500,500);
    //panel.setBackground(Color.blue);
    }

   }

然后我只是在其他类的主方法中运行它:

public class GUIStarter {

public static void main(String[] args) {

    GUI start = new GUI();

}

}

那么有人能给我一个答案或只是一个提示吗? 谢谢。

the problem I encountered is weird for me, because I was doing everything step by step, correctly (in my opinion) and finally when I could say I finished one part of my program it appeared to make a fun of me. The actual problem is that in GUI I created I used a JPanel, then I've put it into a JTabbedPane which I've finally put into a JFrame. Everything is fine and works apart from times when it doesn't. I know it sounds strange, but after running program once I get what I wanted (Frame with tabbed pane containing panel with some stuff in it) and then when I run it again it either show the correct thing again or just empty frame. The worst thing is that it's so random, I haven't got a clue what can be wrong, I don't even know what exactly should I google to find it out. The code is:

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

public class GUI extends JFrame {

JFrame frame = new JFrame("WakeOnLan script generator");
JPanel panel1 = new JPanel(null);
JTextArea text; //= new JTextArea("test");
JScrollPane scroll = new JScrollPane();
JButton but = new JButton("test");
JTabbedPane tab = new JTabbedPane();

public GUI() {

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int w = frame.getSize().width;
    int h = frame.getSize().height;
    int x = (dim.width-w)/3;
    int y = (dim.height-h)/4;

    frame.setSize(500,500);
    frame.setLocation(x,y);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLayout(null);

    createTab1();

    tab.addTab("Tab 1", panel1);
    tab.setVisible(true);
    tab.setBounds(0, 0, 500, 500);

    frame.add(tab);

}

public void createTab1(){

    text = new JTextArea("test");
    text.setVisible(true);

    scroll.setViewportView(text);
    scroll.setBounds(10,10,465,300);

    panel1.setLayout(null);
    panel1.add(scroll);
    panel1.setVisible(true);
    panel1.setSize(500,500);
    //panel.setBackground(Color.blue);
    }

   }

And then I just run it in the main method in other class:

public class GUIStarter {

public static void main(String[] args) {

    GUI start = new GUI();

}

}

So could anyone give me an answer or just a hint?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗷兮 2024-10-11 23:12:07

您应该在将所有组件添加到 JFrame 后调用frame.setVisible(true)。因此,请尝试将其移至构造函数的末尾。

或者,您可以在添加所有组件后调用frame.validate()

You should call frame.setVisible(true) after adding all your components to your JFrame. So try moving it to the end of your constructor.

Alternatively, you can call frame.validate() after all the components have been added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文