Java 按钮不存在';t在文本区域中显示文本

Java 按钮不存在';t在文本区域中显示文本,java,swing,jtextarea,Java,Swing,Jtextarea,我正在设计一个简单的GUI(在Eclipse中使用WindowBuilder Pro),在按下按钮(测试)后在文本区域显示“Hello World” 但是,当我按下按钮时,它不会显示在文本区域中!有人能调整代码或者至少告诉我该怎么做吗 public class TextA { private JFrame frame; /** * Launch the application. */ public static void main(String[] args) { Event

我正在设计一个简单的GUI(在Eclipse中使用WindowBuilder Pro),在按下按钮(测试)后在
文本区域
显示“Hello World”

但是,当我按下按钮时,它不会显示在文本区域中!有人能调整代码或者至少告诉我该怎么做吗

public class TextA {

private JFrame frame;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TextA window = new TextA();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public TextA() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JTextArea textArea = new JTextArea();
    textArea.setBounds(113, 44, 226, 96);
    frame.getContentPane().add(textArea);

    JButton btnTesting = new JButton("Testing");
    btnTesting.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JTextArea textArea = new JTextArea();
            textArea.setText("Hello World!");


        }
    });
    btnTesting.setBounds(168, 167, 117, 29);
    frame.getContentPane().add(btnTesting);
}
}

您正在处理错误的
JTextArea
对象。应该是这样的:

final JTextArea textArea = new JTextArea(); // final added here
textArea.setBounds(113, 44, 226, 96);
frame.getContentPane().add(textArea);

JButton btnTesting = new JButton("Testing");
btnTesting.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            //JTextArea textArea = new JTextArea(); this should be removed.
            textArea.setText("Hello World!");


        }
    });

您正在处理错误的
JTextArea
对象。应该是这样的:

final JTextArea textArea = new JTextArea(); // final added here
textArea.setBounds(113, 44, 226, 96);
frame.getContentPane().add(textArea);

JButton btnTesting = new JButton("Testing");
btnTesting.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            //JTextArea textArea = new JTextArea(); this should be removed.
            textArea.setText("Hello World!");


        }
    });

把你的代码改成这样

 final JTextArea textArea = new JTextArea();
 frame.getContentPane().add(textArea);
 btnTesting.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textArea.setText("Hello World!");
        }
    });
您正在
actionListener
中创建一个新实例,以引用要添加到帧中的对象。正如@andrewhompson总是建议不要使用
null
布局原因:


Java GUI可能必须在不同的平台上工作 屏幕分辨率&使用不同的plaf。事实并非如此 有利于准确放置组件。组织组件的步骤 对于健壮的GUI,使用布局管理器或 它们,以及空白区域的布局填充和边框


把你的代码改成这样

 final JTextArea textArea = new JTextArea();
 frame.getContentPane().add(textArea);
 btnTesting.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textArea.setText("Hello World!");
        }
    });
您正在
actionListener
中创建一个新实例,以引用要添加到帧中的对象。正如@andrewhompson总是建议不要使用
null
布局原因:


Java GUI可能必须在不同的平台上工作 屏幕分辨率&使用不同的plaf。事实并非如此 有利于准确放置组件。组织组件的步骤 对于健壮的GUI,使用布局管理器或 它们,以及空白区域的布局填充和边框


您正在actionPerformed(ActionEvent e)中创建一个新的JTextArea对象。只需使用您已经定义的文本区域对象,并将其设置为最终对象,因为它将用于动作事件方法。您可以在action event方法中删除JTextArea textArea=new JTextArea(),它应该可以工作。

您正在actionPerformed(ActionEvent e)中创建一个新的JTextArea对象。只需使用您已经定义的文本区域对象,并将其设置为最终对象,因为它将用于动作事件方法。您可以在action event方法中删除JTextArea textArea=new JTextArea(),它应该可以工作。

您正在操作侦听器中创建一个新的
JTextArea
,并在其上设置文本。这与您添加到
JFrame
中的文本区域不同。Java GUI可能需要在多种平台、不同的屏幕分辨率和使用不同的PLAF上工作。因此,它们不利于部件的精确放置。要为一个健壮的GUI组织组件,可以使用布局管理器,或者与布局填充和边框一起使用。或者,使用JavaFX,您可以使GUI更丰富、更精确。您正在操作侦听器中创建一个新的
JTextArea
,并在其上设置文本。这与您添加到
JFrame
中的文本区域不同。Java GUI可能需要在多种平台、不同的屏幕分辨率和使用不同的PLAF上工作。因此,它们不利于部件的精确放置。要为一个健壮的GUI组织组件,可以使用布局管理器,或者使用布局填充和边框。或者,使用JavaFX,您可以使GUI更丰富、更精确。这就是我所说的,或者至少尝试过的:)。也许我应该说得更清楚些。我就是这么说的,至少我试过:)。也许我应该说得更清楚些。