Java里怎么實現點擊一個按鈕然后在文本框里顯示按鈕對應的字符?


package com.y;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Test extends JFrame implements ActionListener{
    JPanel panel   =new JPanel();
    JButton b1     =new JButton("1");
    JButton b2     =new JButton("2");
    JButton b3     =new JButton(".");
    JLabel  lbl    =new JLabel("我是標簽");
    JTextField txt =new JTextField("",10);
    public Test(){
        this.setLayout(new FlowLayout());
        panel.add(b1);panel.add(b2);panel.add(b3);panel.add(lbl);panel.add(txt);
        this.add(panel);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
    }
    public static void main(String[] args) {
        Test test=new Test();
        test.setSize(400,260);
        test.setLocationRelativeTo(null);
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent ae) {
        txt.setText( txt.getText() + ae.getActionCommand() );
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM