【问题标题】:Adding JTextField yields empty JFrame添加 JTextField 会产生空的 JFrame
【发布时间】:2021-04-10 00:34:29
【问题描述】:

我对此很陌生,我正在学习一个教程,一切都很好。但是,当添加JTextField 时,JFrame 会变成纯灰色背景。我正在调用从 Main.java 生成 JFrame 的 GUI 类,所以我尝试只使用 Main.java 中的文本字段来生成 JFrame,但它有同样的问题。向文本字段添加和删除组件也没有做任何事情。将它放在面板中并没有帮助(面板与标签配合得很好)。

public class GUI extends JFrame {

    JButton button;
    
    GUI(){
        this.setTitle("Visualization");
        this.setSize(1200, 800);
        this.setResizable(false);
        this.setLayout(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ImageIcon image = new ImageIcon("Default/WhiteCatPFP.jpg");
        this.setIconImage(image.getImage());
        //this.getContentPane().setBackground(Color.BLACK);

        Border border = BorderFactory.createLineBorder(Color.green, 3);

        JLabel label = new JLabel();
        label.setForeground(Color.GREEN);
        label.setText("Bro, do you even code?");
        label.setBackground(Color.BLACK);
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setOpaque(true);
        label.setBorder(border);
        label.setFont(new Font(this.getFont().getName(), Font.PLAIN, 30));
        //label.setVerticalAlignment(JLabel.TOP);
        //label.setFont(new Font("MV Boli", Font.PLAIN, 20));

        button = new JButton();
        button.setFocusable(false);
        button.setForeground(Color.GREEN);
        button.setBackground(Color.BLACK);
        button.setOpaque(true);
        button.setBorder(border);
        button.setText("Baka");
        button.setFont(new Font(this.getFont().getName(), Font.PLAIN, 20));
        button.setBounds(50, 10, 100, 90);
        button.addActionListener(e -> System.out.println("Epic")); // Called lambda

        JTextField aTextField = new JTextField("Hello"); // <-------------------- Error inducing!
        aTextField.setBounds(0, 400, 400, 200);

        JPanel panelOne = new JPanel();
        panelOne.setBounds(0, 0, 400, 200);
        panelOne.setLayout(new BorderLayout());

        JPanel panelTwo = new JPanel();
        panelTwo.setBounds(0, 200, 400, 200);
        panelTwo.setLayout(new BorderLayout());

        JPanel panelThree = new JPanel();
        panelThree.setBounds(0, 400, 400, 200);
        panelThree.setLayout(new BorderLayout());

        panelOne.add(label);
        panelTwo.add(button);
        panelThree.add(aTextField);
        this.add(panelOne);
        this.add(panelTwo);
        this.add(panelThree);
    }
}

有很多东西可以做笔记,我宁愿为这个特定的项目使用空布局的东西(除非那是错误的根源),右边的空白是我稍后会添加的东西.

注意:没有文本字段,其他一切都很好。即使只是定义文本字段 JTextField aTextField = new JTextField();JFrame 设为灰色背景。也没有崩溃。

非常感谢任何帮助!

【问题讨论】:

  • 我宁愿为这个特定的项目使用空布局的东西我宁愿不帮助那些不听建议的人。
  • 嗯,我说是因为几节课后有布局,我还没有达到那个点,但似乎我错了!

标签: java swing jframe jtextfield


【解决方案1】:

我正在学习教程

如果教程使用空布局,则删除教程。没有理由使用空布局。 Swing 被设计为与布局管理器一起使用(这里列出的原因太多了)。

阅读Layout Managers 上的 Swing 教程以获取正确的演示代码。教程代码将向您展示如何正确创建 Swing 框架。从教程中的演示代码开始,然后进行更改。

但是,当添加 JTextField 时,JFrame 会变成纯灰色背景。

文本字段与您的问题无关。

问题是需要在框架可见之前将组件添加到框架中。 setVisible( true ) 语句应该是构造函数中的最后一条语句。

【讨论】:

猜你喜欢
  • 2015-12-18
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
相关资源
最近更新 更多