【上机作业】【java】2010.5.4 图形界面编程

java图形界面编程
1、设计一个图形界面,能够计算和输出圆面积

import java.awt.*;//引入低级界面开发包
import javax.swing.*;//引入高级界面开发包
import java.awt.event.*;//引入事件包
class Circle extends JFrame implements ActionListener
{ JLabel l1=new JLabel(“半径”);
JLabel l2=new JLabel(“面积”);
JTextField t1=new JTextField(20);
JTextField t2=new JTextField(20);
JButton b1=new JButton(“计算”);
JButton b2=new JButton(“关闭”);
public Circle()
{//在构造方法中生成界面
//窗口标题
super(“计算圆面积”);
//窗口布局设置网格布局
this.setLayout(new GridLayout(3,2,5,5));
//将各组件加入到窗体中
this.add(l1);this.add(t1);this.add(l2);
this.add(t2);this.add(b1);this.add(b2);
//为按钮注册监听
b1.addActionListener(this);
b2.addActionListener(this);
//设置窗体大小
this.setSize(400,400);
//设置窗体可见
this.setVisible(true);
}
//事件处理程序
public void actionPerformed(ActionEvent e)
{//判断事件源
JButton b=(JButton)e.getSource();
if(b==b1)
{//计算圆面积
//获得用户输入的半径
int r=Integer.parseInt(t1.getText());
double area=3.14*r*r;
//显示面积
t2.setText(String.valueOf(area));
}
if(b==b2)
{System.exit(0);//退出程序
}
}
public static void main(String args[])
{//调用窗体对象
Circle c=new Circle();
}
}

2、使用布局,设计简单计算器的界面,能够实现四则运算

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Math;

class Numerator extends JFrame implements ActionListener
{
JTextField t1=new JTextField(20);
JButton jia=new JButton(“+”);
JButton jian=new JButton(“-“);
JButton cheng=new JButton(“*”);
JButton chu=new JButton(“/”);
JButton deng=new JButton(“=”);
JButton one=new JButton(“1”);
JButton two=new JButton(“2”);
JButton three=new JButton(“3”);
JButton four=new JButton(“4”);
JButton five=new JButton(“5”);
JButton six=new JButton(“6”);
JButton seven=new JButton(“7”);
JButton eight=new JButton(“8”);
JButton nine=new JButton(“9”);
JButton zero=new JButton(“0”);
JButton dot=new JButton(“.”);
JButton AC=new JButton(“清零”);
Panel panel1 = new Panel(new GridLayout(5,1));
Panel panel2 = new Panel(new GridLayout(4,3));
double num_a,num_b,result;
char method;
boolean dot_flag=false;

String display;

public Numerator(){
super(“我的计算器”);
this.setLayout(null);
this.setBounds(0,0,400,400);
this.setVisible(true);

t1.setBounds(10,10,365,30);
this.add(t1);

panel1.setBounds(300,50,75,300);

panel1.add(jia);
panel1.add(jian);
panel1.add(cheng);
panel1.add(chu);
panel1.add(AC);
this.add(panel1);

panel2.setBounds(10,50,290,300);
panel2.add(one);
panel2.add(two);
panel2.add(three);
panel2.add(four);
panel2.add(five);
panel2.add(six);
panel2.add(seven);
panel2.add(eight);
panel2.add(nine);
panel2.add(zero);
panel2.add(dot);
panel2.add(deng);
this.add(panel2);

jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
deng.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
dot.addActionListener(this);
AC.addActionListener(this);

t1.setText(“”);
t1.setHorizontalAlignment(JTextField.RIGHT);
t1.enable(false);

}
public void actionPerformed(ActionEvent e){
JButton b=(JButton)e.getSource();
if(b==one){
t1.setText(t1.getText()+”1″);
}
if(b==two){
t1.setText(t1.getText()+”2″);
}
if(b==three){
t1.setText(t1.getText()+”3″);
}
if(b==four){
t1.setText(t1.getText()+”4″);
}
if(b==five){
t1.setText(t1.getText()+”5″);
}
if(b==six){
t1.setText(t1.getText()+”6″);
}
if(b==seven){
t1.setText(t1.getText()+”7″);
}
if(b==eight){
t1.setText(t1.getText()+”8″);
}
if(b==nine){
t1.setText(t1.getText()+”9″);
}
if(b==zero){
t1.setText(t1.getText()+”0″);
}

if(b==jia){
num_a=Double.parseDouble(t1.getText());
t1.setText(“”);
method=’+’;
dot_flag=false;
}
if(b==cheng){
num_a=Double.parseDouble(t1.getText());
t1.setText(“”);
method=’*’;
dot_flag=false;
}
if(b==jian){
num_a=Double.parseDouble(t1.getText());
t1.setText(“”);
method=’-‘;
dot_flag=false;
}
if(b==chu){
num_a=Double.parseDouble(t1.getText());
t1.setText(“”);
method=’/’;
dot_flag=false;
}

if(b==deng){
num_b=Double.parseDouble(t1.getText());
result=compute(num_a,num_b,method);
t1.setText(Double.toString(result));
dot_flag=false;
}

if(b==dot){

if(dot_flag){
}
else{
t1.setText(t1.getText()+”1″);
dot_flag=true;
}

}

if(b==AC){
num_a=0;num_b=0;result=0;
method=’’;
dot_flag=false;
t1.setText(“”);
}

}

public double compute(double a,double b,char m){
double temp=0;
if(m==’+’)
temp=num_a+num_b;
else
if(m==’-‘)
temp=num_a-num_b;
else
if(m==’*’)
return num_a*num_b;
else
if(m==’/’)
if(num_b!=0)
temp=num_a/num_b;
else temp=0;
else
temp=a;
return temp;
}

public static void main(String args[]){
Numerator n=new Numerator();
}
}

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注