【问题标题】:How to make a text area appear in a Java JFrame?如何使文本区域出现在 Java JFrame 中?
【发布时间】:2016-06-07 00:40:42
【问题描述】:

我已经在互联网上搜索了解决方案,但似乎没有找到适合我的解决方案...如果您想知道,我是 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));
    }
}

【问题讨论】:

  • 用实际代码和输出编辑了我的答案;现在它正在工作:)

标签: java swing jframe jbutton jtextarea


【解决方案1】:

移除框架.setVisible(true);来自 FrameCreation 类,并添加它 f.setVisible(true);在 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));
    //This new line
    f.setVisible(true);     
}

【讨论】:

    【解决方案2】:

    找到你真正的问题;请不要使用frame.setLayout(null);。 Here 是一篇解释为什么 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;
        }
    }
    

    这是输出:

    【讨论】:

      【解决方案3】:

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

      所以你需要添加:

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

      在 main() 方法的末尾。

      但是,这不是正确的解决方案,不应使用。你需要重新设计你的班级。

      我是 Swing 新手

      代码有太多问题,无法在此处列出。因此,我建议您先阅读 Swing Tutorial 了解 Swing 基础知识。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-22
        • 1970-01-01
        • 1970-01-01
        • 2016-02-04
        • 2013-04-21
        • 1970-01-01
        • 1970-01-01
        • 2022-12-16
        相关资源
        最近更新 更多