基本的java。事件未在按钮单击时将值从文本字段传递到标签

时间:2016-07-17 16:15:59

标签: java swing

这是非常基本的,但我无法将值从文本字段传递到标签。单击该按钮时,该值将从文本字段中获取并写入标签中。全部评论

打包窗口;

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

public class FirstWindow extends JFrame implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

        //defining the objects          

            private JButton jboton;
            private JTextField jtexto;
            private JLabel jlabel;

        // constructor



                public FirstWindow(){                       
                super("mi ventana");
                this.setSize(400,400);
                definirVentana();
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setVisible(true);

            }



             public void definirVentana(){

            //define how components are laid out

            this.setLayout(new FlowLayout());

            //now let's instantiate the objects above
            JButton jboton = new JButton("Press me");
            JTextField jtexto = new JTextField(20);
            JLabel jlabel = new JLabel();

            //and now we add them to the window
            this.add(jboton);
            this.add(jtexto);
            this.add(jlabel);
            jboton.addActionListener(this);

        }


        public void actionPerformed(ActionEvent e) {

            if(e.getSource() == jboton){            
                jlabel.setText(jtexto.getText()) ;

            }

        }

    }

0 个答案:

没有答案