有点不对劲。
/*1.编写Application程序,在JFrame中加入2个按钮(JButton)和1个标签(Label),单击两个按钮,显示按钮的标签于Label*/
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
class JFrameDemo extends JFrame implements ActionListener {
JLabel label = new JLabel("标签1");
JButton button1 = new JButton();
JButton button2 = new JButton();
public JFrameDemo()
{
setTitle("第一个程序");
setLayout(null);
//加标签
label.setBounds(5,5,160,80);
label.setFont(new Font("",Font.BOLD,22));
label.setHorizontalAlignment(JLabel.CENTER);
getContentPane().add(label);
//加按钮
button1.setText("按钮1");
button1.setBounds(170,5,120,30);
getContentPane().add(button1);
button2.setText("按钮2");
button2.setBounds(170,40,120,30);
getContentPane().add(button2);
button1.addActionListener((ActionListener) this);
button2.addActionListener(this);
setBounds(400,200,450,230);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand()=="按钮1")
{ label.setText("按钮1");
}
else label.setText("按钮2");
}
}
public class ButtonHandler
{ public static void main(String[] args){
new JFrameDemo ();
}
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/*2.在JApplet中加入1个文本框,1个文本区,每次在文本框中输入文本,回车后将文本添加到文本区的最后一行。*/
class JFrameDemo extends JFrame implements ActionListener{
JTextField text = new JTextField();
JTextArea textArea = new JTextArea();
public JFrameDemo()
{
//设置文本框
text.setText("请输入文本");
text.setBounds(170,45,240,30);
getContentPane().add(text);
//设置文本域
textArea.setLineWrap(true);
textArea.setColumns(10);
textArea.setRows(3);
textArea.append("这是一个多行文本域");
textArea.setBounds(170,100,240,60);
getContentPane().add(textArea);
//监听器
text.addActionListener(this);
setTitle("第二个程序");
setLayout(null);
setBounds(200,200,450,500);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
textArea.append(text.getText());
}
}
public class Demo {
public static void main(String[] args){
new JFrameDemo ();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*3.设计一个简单计算器,如下图所示。在“操作数”标签右侧的两个文本框输入操作数,当单击操作符+,-,×,÷按钮时,对两个操作数进行运算并将结果填入到“结果”标签右侧的文本框中。*/
public class demo{
public static void main(String[] args){
new SimpleCalculator();
}
}
class SimpleCalculator extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
GridLayout g11,g12,g13;
JPanel resultPanel,controlPanel,statisticsPanel,computationPanel;
JTextField tf1;
TextField tf2;
Button[] btn = new Button[27];
String[] btnCaption = {"Backspace","CE","C","MC","MR","MS","M+","7","8","9","/",
"sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
StringBuffer str;//显示屏所显示的字符串
double x,y;//运算符
int z;//z表示单击了哪一个运算符,0表示+,1表示—,2表示*,3表示/
static double m;//记忆数字
public SimpleCalculator(){
//getContentPane().setBackground(Color.white);
/*super("计算器");
setLayout(null);*/
setResizable(false);//禁止调整框架的大小}
setTitle("计算器v1.0 by:zhangsan");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setBounds(200,200,330,370);
//实例化所有按钮。摄者其前景色并注册监听器
for(int i=0;i<27;i++){
btn[i] = new Button(btnCaption[i]);
btn[i].setFont(new Font("",Font.PLAIN,12));
btn[i].setForeground(Color.black);
btn[i].addActionListener(new Bt());
}
//创建结果面板,添加显示屏等
resultPanel = new JPanel();
resultPanel.setBounds(10,10,300,40);
tf1 = new JTextField(27);//显示屏
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setText("0");
resultPanel.add(tf1);
getContentPane().add(resultPanel);
//创建控制键面板,添加记忆框及3个控制键
controlPanel = new JPanel();
controlPanel.setBounds(10,50,300,25);
g11 = new GridLayout(1,4,10,0);//1行4列
controlPanel.setLayout(g11);//设置布局
tf2 = new TextField(8);
tf2.setEditable(false);
controlPanel.add(tf2);
for(int i=0;i<3;i++)
//添加3个控制键
controlPanel.add(btn[i]);
getContentPane().add(controlPanel);
//添加统计面板以及4个按键
statisticsPanel = new JPanel();
statisticsPanel.setBounds(10,90,40,150);
g12 = new GridLayout(4,1,0,15);//4行1列
statisticsPanel.setLayout(g12);
for(int i=3;i<7;i++){
statisticsPanel.add(btn[i]);
}
getContentPane().add(statisticsPanel);
//添加计算面板以及数字、运算符按键
computationPanel = new JPanel();
computationPanel.setBounds(60,90,250,150);
g13 = new GridLayout(4,5,10,15);
computationPanel.setLayout(g13);
for(int i=7;i<27;i++)
computationPanel.add(btn[i]);
getContentPane().add(computationPanel);
//创建一个空字符缓冲区
str = new StringBuffer();
setVisible(true);
//匿名类关闭窗口
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1){
System.exit(1);
}
});
}
//构造监听器
class Bt implements ActionListener{
public void actionPerformed(ActionEvent e2){
try{
if(e2.getSource() == btn[1]){
//显示屏清零
tf1.setText("0");
//清空字符串缓冲区以准备接收新的输入运算符
str.setLength(0);
}//选择"CE"清零
else if(e2.getSource() == btn[2]){
//选择"C清零"
tf1.setText("0");//显示屏清零
str.setLength(0);//单击"+/-"选择输入的运算数是正数还是负数。
}
else if(e2.getSource() == btn[23]){
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("" + (-x));
//单击加号按钮获得x的值和z的值并清空y的值
}
else if(e2.getSource() == btn[25]){
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
y = 0d;
z = 0;
//单击减号按钮获得x的值和z的值并清空y的值
}
else if(e2.getSource() == btn[20]){
x = Double.parseDouble(tf1.getText().trim());
y = 0d;
z = 1;
//单击乘号按钮获得x的值和z的值并清空y的值
}
else if(e2.getSource() == btn[15]){
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 2;
//单击除号按钮获得x的值和z的值并清空y的值
}
else if(e2.getSource() == btn[10]){
x = Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y = 0d;
z = 3;
//单击等号按钮输出计算结果
}
else if(e2.getSource() == btn[26]){
str.setLength(0);
switch(z){
case 0:
tf1.setText(""+(x+y));
break;
case 1:
tf1.setText(""+(x-y));
break;
case 2:
tf1.setText(""+(x*y));
break;
case 3:
tf1.setText(""+(x/y));
break;
}
}
else if(e2.getSource() == btn[24]){
//单击“。”按钮输入小数
//判断字符串中是否已经包含了小数点
if(tf1.getText().trim().indexOf('.')!= -1){
}
else //如果没有小数点
{
if(tf1.getText().trim().equals("0"))//如果初始显示为0
{
str.setLength(0);
tf1.setText((str.append("0"+e2.getActionCommand())).toString());
//如果初始显示为空则不做任何操作
}
else if(tf1.getText().trim().equals("")){
}else{
tf1.setText(str.append(e2.getActionCommand()).toString());
}
}
y = 0d;
}else if(e2.getSource() == btn[11])//求平方根
{
x = Double.parseDouble(tf1.getText().trim());
tf1.setText("数字格式异常");
if(x<0)
tf1.setText("负数没有平方根");
else
tf1.setText(""+Math.sqrt(x));
str.setLength(0);
y = 0d;
}else if(e2.getSource() == btn[16])//单击了“%”按钮
{
x = Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(0.01*x));
str.setLength(0);
y = 0d;
}else if(e2.getSource() == btn[21])//单击了“1/x”按钮子
{
x = Double.parseDouble(tf1.getText().trim());
if(x == 0){
tf1.setText("除数不能为0");
}else {
tf1.setText(""+(1/x));
}
str.setLength(0);
y = 0d;
}
else if(e2.getSource() == btn[3])//MC为清楚内存
{
m = 0d;
tf2.setText("");
str.setLength(0);
}else if(e2.getSource() == btn[4])//MR为重新调用存储的数据
{
if(tf2.getText().trim() != "")//有记忆数字
{
tf1.setText(""+m);
}
}
else if(e2.getSource() == btn[5]){
m = Double.parseDouble(tf1.getText().trim());
tf2.setText("M");
tf1.setText("0");
str.setLength(0);
//M+为将显示的数字与已经存储的数据相加,要查看新的数字单击MR
}else if(e2.getSource() == btn[6]){
m = m + Double.parseDouble(tf1.getText().trim());
}else //选择的是其他按钮
{//如果选择的是“0”这个数字键
if(e2.getSource() == btn[22]){//如果显示屏显示的为0,不做操作
if(tf1.getText().trim().equals("0")){
}else {
tf1.setText(str.append(e2.getActionCommand()).toString());
y = Double.parseDouble(tf1.getText().trim());
//选择的是“backspace”按钮
}
}
else if(e2.getSource() == btn[0]){//如果显示屏显示的不是零
if(!tf1.getText().trim().equals("0")){
if(str.length() != 1){
//可能抛出字符串越界异常
tf1.setText(str.delete(str.length()-1,str.length()).toString());
}else {
tf1.setText("0");
str.setLength(0);
}
}
y = Double.parseDouble(tf1.getText().trim());
}
else//其他的数字键
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y = Double.parseDouble(tf1.getText().trim());
}
}
}
catch (NumberFormatException e){
tf1.setText("数字格式异常");
}
catch (StringIndexOutOfBoundsException e){
tf1.setText("字符串索引越界");
}
}
}
}