java创建一个有一个文本区域和三个按钮的程序。当我们按下每个按钮时,使不同的文字显示在文本区域中
怎么没分呢?!这个很easy啦~程序如下:
package com;
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");
}
}
}