简体   繁体   English

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

[英]How to get a text from JTextfield and displaying it on a JTextArea

I am trying to get a text input from a JTextField and displaying it on a JTextField when a button is clicked. 我试图从JTextField获取文本输入,并在单击按钮时将其显示在JTextField上。 Can anyone help please? 有人可以帮忙吗? I know I am supposed to use getText and setText but not quite sure how I can implement this when the button is clicked. 我知道我应该使用getText和setText,但是不太确定单击按钮时如何实现。 Please have a look at the code below. 请看下面的代码。 Thanks. 谢谢。

 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM