Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaJFrame中显示文本区域?_Java_Swing_Jframe_Jbutton_Jtextarea - Fatal编程技术网

如何在JavaJFrame中显示文本区域?

如何在JavaJFrame中显示文本区域?,java,swing,jframe,jbutton,jtextarea,Java,Swing,Jframe,Jbutton,Jtextarea,我在互联网上搜索解决方案,但似乎没有找到适合我的解决方案。。。如果你想知道的话,我是个新手。所以,这里有一件事,JButton出现了,但是JTextArea没有。我不知道该怎么解决这个问题。。。帮帮我,伙计们 public class FrameCreation { public JFrame createFrame(int width, int height, String name) { JFrame frame = new JFrame(name);

我在互联网上搜索解决方案,但似乎没有找到适合我的解决方案。。。如果你想知道的话,我是个新手。所以,这里有一件事,
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)。这篇文章解释了为什么空布局不好。取而代之的是,我使用了flow布局,它按照预期的方式工作。以下是代码(我已将它们更改为两个类):

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;
    }
}
以下是输出:


移除框架。设置可见(true);从FrameCreation类,并将其添加到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()方法的末尾

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

我不太会荡秋千

代码有太多错误,无法在此列出。因此,我建议您从阅读Swing基础知识开始

可以从
如何使用文本区域的部分开始。
TextDemo
将向您展示如何更好地构建代码,以便您:

  • 在事件分派线程上创建Swing组件
  • 创建具有合理大小的JTextArea
  • 使用布局管理器
  • 将所有组件添加到框架后,打包()框架并使用setVisible(true)

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