java创建一个有一个文本区域和三个按钮的程序。当我们按下每个按钮时,使不同的文字显示在文本区域中
我用 awt 写的:
-------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class Demo extends Frame {
Button btnChinese = new Button("中文");
Button btnEnglish = new Button("英文");
Button btnPunctuation = new Button("标点");
TextArea txtMessage = new TextArea();
public Demo () {
super("AWT--threeButton--Test");
addWindowListener(new WindowAdapter() {
@Override public void windowClosing (WindowEvent we) {
System.exit(0);
}
});
setSize(400, 400);
setLocationRelativeTo(null);
btnChinese.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent ae) {
txtMessage.append("你按了中文按钮\n");
}
});
btnEnglish.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent ae) {
txtMessage.append("You typed the English button\n");
}
});
btnPunctuation.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent ae) {
txtMessage.append(",.!~\n");
}
});
add(btnChinese, BorderLayout.NORTH);
add(btnEnglish, BorderLayout.WEST);
add(btnPunctuation, BorderLayout.SOUTH);
add(txtMessage, BorderLayout.CENTER);
setVisible(true);
}
public static void main (String args[]) {
new Demo();
}
}
---------------------------------------------------------------
效果如下:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CommonLayouts extends JFrame implements ActionListener
{
public JButton chinese=new JButton("中文");
public JButton English=new JButton("英文");
public JButton Flag=new JButton("标点");
public JTextArea show=new JTextArea(400,400);
public CommonLayouts()
{
this.setTitle("写字板");
this.setLayout(new BorderLayout());
this.add(chinese, BorderLayout.NORTH);
this.add(Flag, BorderLayout.SOUTH);
this.add(English, BorderLayout.WEST);
this.add(show, BorderLayout.CENTER);
this.setSize(600,500);
chinese.addActionListener(this);
English.addActionListener(this);
Flag.addActionListener(this);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new CommonLayouts();
}
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
if(arg0.getSource()==chinese)
{
show.setText(show.getText()+"显示中文!\n");
}
else if(arg0.getSource()==English)
{
show.setText(show.getText()+"Display English!\n");
}
else
{
show.setText(show.getText()+",.+-+==-=-*/-.<><>(显示标点)\n");
}
}
}