【问题标题】:Java GUI Buttons will not print string to interfaceJava GUI 按钮不会将字符串打印到界面
【发布时间】:2018-03-04 22:02:14
【问题描述】:

这里是 GUI 初学者。

我的问题不是创建 GUI 界面,而是让 2 个按钮在界面内的 JTextArea 中打印字符串。
第一个“学习”按钮从数组中获取一个随机元素并打印它。 第二个按钮“清除”应该在按下时打印一个字符串 我有两个按钮的动作监听器,但仍然无法完全理解。

感谢您的宝贵时间。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;


public class GUI extends JFrame {


String [] sentences = {"Random sentence 1", "random sentence 2", "random sentence 3", "random sentence 4", random sentence 5", "random sentence 6"};


private Container contents;
JButton learned = new JButton("Learned");
JButton clear = new JButton("Clear");
JTextArea clearDisplay;


public GUI()
{
    super ("GUI"); //title bar text

    contents = getContentPane ();
    contents.setLayout(new FlowLayout()); //make buttons appear

    //set the layout manager

    //instantiate buttons
    learned = new JButton("I Learned");
    clear = new JButton("Clear");

    //add components to window
    contents.add(learned);
    contents.add(clear);



    //instantiate event handler
    ButtonHandler bh = new ButtonHandler ();

    //add event handler as listener for both buttons
    learned.addActionListener (bh);
    clear.addActionListener(bh);

    setSize (400, 200); //size of window
    setVisible (true); //see the window

    }


   public class ButtonHandler implements ActionListener
   {
    //implement ActionPerformed method

    public void actionPerformed(ActionEvent e)
    {
        Container contentPane = getContentPane();
       if (e.getSource() == learned)
       {
         String random = (sentances[new Random().nextInt(sentances.length)]); //random from array           
         JTextArea learned = new JTextArea(random);
       }
       else if (e.getSource() == clear)           
       {
            JTextArea clearDisplay = new JTextArea("This is where it will display what I learned. \\nMessage Displayed Here.");
       }

    }

}

public static void main(String[] args)
{  
   GUI basicGui = new GUI (); 
   basicGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //program exits on close                              
}

}

【问题讨论】:

    标签: java string user-interface button


    【解决方案1】:

    你的问题在这里:

    else if (e.getSource() == clear)           
       {
            JTextArea clearDisplay = new JTextArea("This is where it will display what I learned. \\nMessage Displayed Here.");
       }
    

    您正在创建一个永远不会添加到 GUI 的新 JTextArea。你应该这样写:

    else if (e.getSource() == clear)           
       {
            clearDisplay = new JTextArea("This is where it will display what I learned. \\nMessage Displayed Here.");
       }
    

    并将其添加到 GUI。

    您还应该考虑一种不同的方法:首先创建 JTextField,将其添加到 GUI,然后仅在上面的代码中更改其文本。

    另外,这里有一些错别字:

    String random = (sentances[new Random().nextInt(sentances.length)]);
    

    【讨论】:

      【解决方案2】:

      不要在你的动作监听器中创建新的 JTextAreas。而是在 GUI 的构造函数和侦听器中创建一个 JTextArea,只需将适当的文本写入 JTextArea,如果要完全更改文本,请调用 .setText(...),如果要添加其他文本,请调用 .append(...)现有文本。

      【讨论】:

      • 添加了公共 JTextArea textArea = new JTextArea();给构造函数。并在组件部分添加了 contents.add(textArea);然后动作事件添加 textArea.setText(random);和 textArea.setText("这....");它成功了!非常感谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      相关资源
      最近更新 更多