Java Swing簡單的加法器

來源:互聯網
上載者:User

標籤:

 1 package test; 2 import java.awt.*; 3 import javax.swing.*; 4 import java.awt.event.*; 5 public class FrameDemo2 6 { 7 static JTextField field1=new JTextField(5); 8 static JTextField field2=new JTextField(5); 9 static JTextField field3=new JTextField("0",5);10 11 public FrameDemo2(){12     JFrame f=new JFrame("加法器");13     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);14     f.setSize(220,100);15     f.setLocation(500,300);16     JLabel jb=new JLabel("+");17     f.setVisible(true);18     JPanel p1=new JPanel();19     f.setContentPane(p1);20     p1.setLayout(new FlowLayout());21 22     p1.add(field1);23     p1.add(jb);24     p1.add(field2);25     p1.add(field3);26     JButton b1=new JButton("計算");27     Color bg=new Color(255,255,255);28     b1.setBackground(bg); 29     p1.add(b1);30 31     b1.addActionListener(new ActionListener()32     {33     public void actionPerformed(ActionEvent e)    //重寫actionPerformed方法34     {35     int a,b;36     a=Integer.parseInt(field1.getText()); //擷取TextField1中的資料37     b=Integer.parseInt(field2.getText());38     field3.setText(String.valueOf(a+b));39     }40     }41     );42 }43 public static void main(String args[])44 {45     FrameDemo2 fd=new FrameDemo2();46 }47 }

運行結果:

 

初學Java Swing,以前一直感覺Swing沒什麼用處,所以就沒怎麼接觸,這幾天Java實訓,老師要求用Java Swing寫一個項目,接觸後感覺Swing還是挺不錯的,比MFC更容易理解。

Java Swing簡單的加法器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.