如何使文本区域出现在 Java JFrame 中?

How to make a text area appear in a Java JFrame?

我在 Internet 上搜索了解决方案,但似乎没有找到适合我的解决方案...如果您想知道,我是 Swing 新手。所以,事情是这样的,JButton 出现了,但是 JTextArea 没有出现。我不知道该怎么做才能解决这个问题...帮帮我...

public class FrameCreation
{
    public JFrame createFrame(int width, int height, String name) 
    {
        JFrame frame = new JFrame(name);
        frame.setVisible(true);
        frame.setSize(width, height);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        return frame;
    }

    public JButton createButton(int width, int height, int xPos, int yPos, String text) 
    {
        JButton button = new JButton(text);
        button.setBounds(xPos, yPos, width, height);
        button.setBackground(Color.GRAY);
        button.setForeground(Color.WHITE);
        return button;
    }

    public JTextArea createTextArea(int width, int height, int xPos, int yPos)
    {
        JTextArea txt = new JTextArea();
        txt.setVisible(true);
        txt.setBounds(xPos, yPos, width, height);
        txt.setText("Help this poor JTextArea to appear on the frame...");
        return txt;
    }
}

public class Main 
{
    public static void main(String[] args) 
    {
        FrameCreation mainFrame = new FrameCreation();
        JFrame f = new FrameCreation().createFrame(600, 600, "My Frame");
        f.add(mainFrame.createButton(100, 40, 10, 10, "Click me!"));
        f.add(mainFrame.createTextArea(200, 200, 390, 10));
    }
}

找到你真正的问题;请不要使用 frame.setLayout(null);Here 是一个 post 解释为什么 null 布局不好。相反,我使用了流式布局并且它按预期工作。这是代码(我把它们改成了两个类):

Main.java

import javax.swing.*;

public class Main
{
    public static void main(String[] args)
    {
        FrameCreation mainFrame = new FrameCreation();
        JFrame f = new FrameCreation().createFrame(600, 600, "My Frame");
        f.add(mainFrame.createButton(100, 40, 10, 10, "Click me!"));
        f.add(mainFrame.createTextArea(300, 300, 390, 10));
        f.setVisible(true);
    }
}

FrameCreation.java

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

public class FrameCreation
{
    public JFrame createFrame(int width, int height, String name)
    {
        JFrame frame = new JFrame(name);
        frame.setVisible(true);
        frame.setSize(width, height);
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        return frame;
    }

    public JButton createButton(int width, int height, int xPos, int yPos, String text)
    {
        JButton button = new JButton(text);
        button.setBounds(xPos, yPos, width, height);
        button.setBackground(Color.GRAY);
        button.setForeground(Color.WHITE);
        return button;
    }

    public JTextArea createTextArea(int width, int height, int xPos, int yPos)
    {
        JTextArea txt = new JTextArea();
        txt.setBounds(xPos, yPos, width, height);
        txt.setText("Help this poor JTextArea to appear on the frame...");
        return txt;
    }
}

这是输出:

删除frame.setVisible(true);来自 FrameCreation class,并添加它 f.setVisible(true);在主要方法的末尾。

public static void main(String[] args) {
    FrameCreation mainFrame = new FrameCreation();
    JFrame f = new FrameCreation().createFrame(600, 600, "My Frame");
    f.add(mainFrame.createButton(100, 40, 10, 10, "Click me!"));
    f.add(mainFrame.createTextArea(200, 200, 390, 10));
    //This new line
    f.setVisible(true);     
}

当您将组件添加到可见的 GUI 时,您需要告诉框架重新绘制自身。

所以你需要添加:

f.revalidate();
f.repaint();

在 main() 方法的末尾。

但是,这不是正确的解决方案,不应使用。您需要重新设计 class.

I'm new to Swing

这里列出的代码有太多错误。因此,我建议您先阅读 Swing Tutorial 了解 Swing 基础知识。

也许从 How to Use Text Areas 部分开始。 TextDemo 将向您展示如何更好地构建代码,以便您:

  1. 在事件调度线程上创建 Swing 组件。
  2. 创建大小合理的 JTextArea
  3. 使用布局管理器。
  4. pack() 框架并在所有组件都添加到框架后使用 setVisible( true )。