天天看點

建立有一個文本框和三個按鈕的小程式 當按下每個按鈕時 使不同的文字顯示在文本框中

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Ti94 extends JPanel implements ActionListener{

 JButton b1,b2,b3;

  static JLabel l1 ;

Ti94(){

  l1=new JLabel("",JLabel.CENTER);//10

  b1=new JButton("我的第一句話[a]");

 b1.setMnemonic(KeyEvent.VK_A);

 b1.setActionCommand("wellcome");

 b1.addActionListener(this);

 b2=new JButton("我的第二句話[b]");

 b2.setMnemonic(KeyEvent.VK_B);

 b2.setActionCommand("content");

 b2.addActionListener(this);

 b3=new JButton("我的最後的話[c]");

 b3.setMnemonic(KeyEvent.VK_C);//20

       b3.setActionCommand("end");

 b3.addActionListener(this);

 add(b1);add(b2);

 add(b3);

 }

public void actionPerformed(ActionEvent e){

 if(e.getActionCommand().equals("wellcome"))

  l1.setText("歡迎進入我的個人網站");

 else if (e.getActionCommand().equals("content"))

  l1.setText("我的QQ号碼是125671468");//30

 else if (e.getActionCommand().equals("end"))

l1.setText("謝謝惠顧");

  l1.setHorizontalAlignment(JLabel.CENTER);

}

public static void main(String args[]){

 JFrame frame=new JFrame("我的第一個GUI應用程式");

 frame.getContentPane().add(new Ti94(),BorderLayout.SOUTH);

 frame.getContentPane().add(l1,BorderLayout.NORTH);

 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 frame.pack();

 frame.setVisible(true);

 }