java - 如何从 JTextfield 获取文本并将其显示在 JTextArea 上

标签 java swing

我试图从 JTextField 获取文本输入,并在单击按钮时将其显示在 JTextField 上。有人可以帮忙吗? 我知道我应该使用 getText 和 setText,但不太确定单击按钮时如何实现它。请看下面的代码。 谢谢。

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

        public class CyberPet extends JFrame 
            implements ActionListener {

            private JButton makePetButton,hungryButton, randomButton;
            private JPanel panel;
            private JLabel label, petName, flyLabel;
            private JTextArea responseArea;
            private JTextField textField;
            int x =10;
            int y=10;   
            int xMax = 700;
            int yMax = 500;

            public static void main (String[] args) {
                CyberPet frame = new CyberPet();
                frame.setSize(700, 500);
                frame.createGUI();
                frame.show();
                frame.getContentPane().setBackground(Color.blue);

            }

            private void createGUI() {
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                Container window = getContentPane();
                window.setLayout(new FlowLayout() );

                JPanel buttonGUI = new JPanel();

                panel = new JPanel();
                panel.setPreferredSize(new Dimension(500, 300));
                panel.setLocation(500, 300);
                panel.setBackground(Color.white);
                panel.setLayout(null);
                window.add(panel);

                buttonGUI = new JPanel();
                buttonGUI.setPreferredSize(new Dimension(400, 100));
                buttonGUI.setLocation(200, 100);
                buttonGUI.setBackground(Color.white);
                window.add(buttonGUI);

                label = new JLabel();
                label.setBackground(Color.white);
                Image img = new ImageIcon (this.getClass().getResource("/frog.gif")).getImage();
                label.setIcon(new ImageIcon(img));
                label.setLocation(400, 0);
                label.setSize(80, 80);
                panel.add(label);

                flyLabel = new JLabel();
                flyLabel.setBackground(Color.black);
                Image img1 = new ImageIcon (this.getClass().getResource("/fly.gif")).getImage();
                flyLabel.setIcon(new ImageIcon(img1));
                flyLabel.setLocation(10, 10);
                flyLabel.setSize(50, 50);
                panel.add(flyLabel);

                petName = new JLabel("Enter Pet Name!");
                buttonGUI.add(petName);

                textField = new JTextField("");
                textField.setPreferredSize(new Dimension(100, 30));
                textField.setLocation(200, 60);
                textField.addActionListener(this);
                buttonGUI.add(textField);

                makePetButton = new JButton("Make Pet");
                makePetButton.setLocation(160, 60);
                makePetButton.addActionListener(this);
                buttonGUI.add(makePetButton);

                hungryButton = new JButton("Hungry!");
                hungryButton.setLocation(280, 60);
                hungryButton.setSize(100, 30);
                hungryButton.addActionListener(this);
                buttonGUI.add(hungryButton);

                responseArea = new JTextArea("Pet Status");
                buttonGUI.add(responseArea);       


            }
            //   ***** nb line of 4 spaces after insert

            public void actionPerformed(ActionEvent event) {

                //Move down
                 if (event.getSource() == makePetButton)
                    {


                     }

                 //Move Up
                     if (event.getSource() == hungryButton)  
                     {
                    if (y > 10){
                     y=y-20;     
                     label.setLocation(x, y);
                    }
                     }
                 //Makes the Pet
                     if (event.getSource() == makePetButton)
                     {
                     if (x > 10){
                     x=x-20;     
                     label.setLocation(x, y);
                      }
                     }
                 //Move Right
                     if (event.getSource() == textField)
                     {
                     if (x < 280){
                     x=x+20;
                     label.setLocation(x, y);
                        }
                     }
                    //Move random
                     if(event.getSource() == randomButton)
                        {
                         Random rnd = new Random();
                         int xMax = panel.getWidth()-label.getWidth();
                         int yMax = panel.getHeight()-label.getHeight();
                         x = rnd.nextInt(xMax+10);
                         y = rnd.nextInt(yMax+10);
                         label.setLocation(x,y);


                     }
                     }

            }

最佳答案

您拥有 eventHandler,因此只需在处理相应按钮上的点击时调用 responseArea.setText(textField.getText()) 即可。

关于java - 如何从 JTextfield 获取文本并将其显示在 JTextArea 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28773027/

相关文章:

java - 如何将服务帐户 OAuth 2.0 流程与 GroupsSettings-api 结合使用

java - 两个 swing 文档之间的双向连接

java - JFrame 不呈现任何组件

在 javafx.scene.media.MediaPlayer.init 处使用媒体播放器时出现 Java FX 空指针异常

java - Java 中强大的 Javascript 解析器

Java - 为什么 ClassName.this.variable 在变量为静态时起作用?

java - 传递给另一个类时清空数组列表

java - Spring Security配置问题

java - 为什么我的程序可以编译但不能运行?

java - JFrame 踢出我不明白的错误