java经典例子代码学习



java经典例子代码学习。

一、
import java.awt.*;
import java.awt.event.*;
public class ThreeListener implements MouseMotionListener,MouseListener,WindowListener {
//实现了三个接口
private Frame f;
private TextField tf;

public static void main(String args[])
{
ThreeListener two = new ThreeListener();
two.go();
}

public void go() {
f = new Frame(“Three listeners example”);
f.add(new Label(“Click and drag the mouse”),”North”);
tf = new TextField(30);
f.add(tf,”South”); //使用缺省的布局管理器

f.addMouseMotionListener(this); //注册监听器MouseMotionListener
f.addMouseListener(this); //注册监听器MouseListener
f.addWindowListener(this); //注册监听器WindowListener

f.setSize(300,200);
f.setVisible(true);
}

public void mouseDragged (MouseEvent e) {//实现mouseDragged方法
String s = “Mouse dragging : X=”+e.getX()+”Y = “+e.getY();
tf.setText(s);
}
public void mouseMoved(MouseEvent e){}
//对其不感兴趣的方法可以方法体为空
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){
String s = “The mouse entered”;
tf.setText(s);}

public void mouseExited(MouseEvent e){
String s = “The mouse has left the building”;
tf.setText(s);}

public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){ }
public void windowClosing(WindowEvent e) {
//为了使窗口能正常关闭,程序正常退出,需要实现windowClosing方法
System.exit(1);}

public void windowOpened(WindowEvent e) {}
//对其不感兴趣的方法可以方法体为空
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }

二、计算器例子
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class testz extends JFrame implements ActionListener
{
private JPanel jPanel1,jPanel2;
private JTextField resultField;
private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2;
private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;

public testz()
{
super(“计算器”);
setSize(300,240);
Container con=getContentPane();
con.setLayout(new BorderLayout());
jPanel1=new JPanel();
jPanel1.setLayout(new GridLayout(1,1));
jPanel2=new JPanel();
jPanel2.setLayout(new GridLayout(4,4));

resultField=new JTextField(“0″);
jPanel1.add(resultField);
con.add(jPanel1,BorderLayout.NORTH);

s1=new JButton(” 1 “); s1.addActionListener(this);
s2=new JButton(” 2 “); s2.addActionListener(this);
s3=new JButton(” 3 “); s3.addActionListener(this);
s4=new JButton(” 4 “); s4.addActionListener(this);
s5=new JButton(” 5 “); s5.addActionListener(this);
s6=new JButton(” 6 “); s6.addActionListener(this);
s7=new JButton(” 7 “); s7.addActionListener(this);
s8=new JButton(” 8 “); s8.addActionListener(this);
s9=new JButton(” 9 “); s9.addActionListener(this);
s0=new JButton(” 0 “); s0.addActionListener(this);
b1=new JButton(” + “); b1.addActionListener(this);
b2=new JButton(” – “); b2.addActionListener(this);
b3=new JButton(” * “); b3.addActionListener(this);
b4=new JButton(” / “); b4.addActionListener(this);
f1=new JButton(” . “); f1.addActionListener(this);
f2=new JButton(” = “); f2.addActionListener(this);

jPanel2.add(s1);
jPanel2.add(s2);
jPanel2.add(s3);

jPanel2.add(b1);
jPanel2.add(s4);
jPanel2.add(s5);
jPanel2.add(s6);
jPanel2.add(b2);
jPanel2.add(s7);
jPanel2.add(s8);
jPanel2.add(s9);
jPanel2.add(b3);
jPanel2.add(s0);
jPanel2.add(f1);
jPanel2.add(f2);
jPanel2.add(b4);

con.add(jPanel2,BorderLayout.CENTER);

}

public void num(int i)
{
String s = null;
s=String.valueOf(i);
if(end){
//如果数字输入结束,则将文本框置零,重新输入
resultField.setText(“0″);
end=false;

}
if((resultField.getText()).equals(“0″)){
//如果文本框的内容为零,则覆盖文本框的内容
resultField.setText(s);

}
else{
//如果文本框的内容不为零,则在内容后面添加数字
str = resultField.getText() + s;
resultField.setText(str);

}
}

public void actionPerformed(ActionEvent e){ //数字事件
if(e.getSource()==s1)
num(1);
else if(e.getSource()==s2)
num(2);
else if(e.getSource()==s3)
num(3);
else if(e.getSource()==s4)
num(4);
else if(e.getSource()==s5)
num(5);
else if(e.getSource()==s6)
num(6);
else if(e.getSource()==s7)
num(7);
else if(e.getSource()==s8)
num(8);
else if(e.getSource()==s9)
num(9);
else if(e.getSource()==s0)
num(0);

//符号事件
else if(e.getSource()==b1)
sign(1);
else if(e.getSource()==b2)
sign(2);
else if(e.getSource()==b3)
sign(3);
else if(e.getSource()==b4)
sign(4);
//等号
else if(e.getSource()==f1){
str=resultField.getText();
if(str.indexOf(“.”)<=1){
str+=”.”;
resultField.setText(str);
}
}
else if(e.getSource()==f2){
num2=Double.parseDouble(resultField.getText());

if(add){
num1=num1 + num2;}
else if(sub){
num1=num1 – num2;}
else if(mul){
num1=num1 * num2;}
else if(div){
num1=num1 / num2;}
resultField.setText(String.valueOf(num1));
end=true;
}

}
public void sign(int s)
{
if(s==1)
{
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2){
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3){
add=false;
sub=false;
mul=true;
div=false;
}
else if(s==4)
{
add=false;
sub=false;
mul=false;
div=true;
}
num1=Double.parseDouble(resultField.getText());
end=true;
}
public static void main(String[] args){
testz th1=new testz();
th1.show();
}
}

java GUI编程教学实例

import java.awt.FlowLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Label;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.TextArea;
public class FrameDemo extends Frame
{
Label label = new Label(“名称:”,Label.RIGHT);
TextField tf = new TextField(20);
TextArea ta = new TextArea();
Button button = new Button(“OK”);
//单选
CheckboxGroup group1 = new CheckboxGroup();
Checkbox cb1 = new Checkbox(“男”,group1,false);
Checkbox cb2 = new Checkbox(“女”,group1,false);
Choice choice = new Choice();
String[] items = {“China”,”USA”,”Japan”,”Korea”,”Germany”};
FrameDemo(String title)
{
super(title);
//设置窗口大小
super.setSize(500,400);
super.setLayout(new FlowLayout(FlowLayout.LEFT));
super.add(label);
super.add(tf);
ta.setEnabled(false);//不可写
super.add(ta);
super.add(button);
super.add(cb1);
super.add(cb2);
for(int i = 0; i < items.length; i++)
{
choice.addItem(items[i]);
}
super.add(choice);
int width = (int)label.getSize().getWidth();
int height = (int)label.getSize().getHeight();
System.out.println(“Width:” + width);
System.out.println(“Height:” + height);
}
public static void main(String[] args)
{
//创建一个窗口的对象
FrameDemo frame = new FrameDemo(“Frame演示”);
//显示窗口
frame.setVisible(true);
}
}


import java.awt.Button;
import java.awt.Frame;
import java.awt.FlowLayout;
public class FlowLayoutDemo extends Frame
{
Button b1 = new Button(“button1″);
Button b2 = new Button(“button2″);
Button b3 = new Button(“button3″);
Button b4= new Button(“button4″);

FlowLayoutDemo(String title)
{
super(title);
super.setSize(500,400);

//将窗口设置为水平布局
super.setLayout(new FlowLayout(FlowLayout.LEFT));

super.add(b1);
super.add(b2);
super.add(b3);
super.add(b4);
}

public static void main(String[] args)
{
FlowLayoutDemo flowLayoutDemo =
new FlowLayoutDemo(“FlowLayout演示”);
flowLayoutDemo.setVisible(true);
}
}

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
public class BorderLayoutDemo extends Frame
{
Button b1 = new Button(“Button1″);
Button b2 = new Button(“Button2″);
Button b3 = new Button(“Button3″);
Button b4 = new Button(“Button4″);
Button b5 = new Button(“Button5″);

BorderLayoutDemo(String title)
{
super(title);
super.setSize(500,400);

//将窗口设置为边框布局
super.setLayout(new BorderLayout());

super.add(b1,BorderLayout.NORTH);
super.add(b2,BorderLayout.SOUTH);
super.add(b3,BorderLayout.WEST);
super.add(b4,BorderLayout.EAST);
super.add(b5,BorderLayout.CENTER);
}

public static void main(String[] args)
{
BorderLayoutDemo borderLayoutDemo =
new BorderLayoutDemo(“BorderLayout演示”);
borderLayoutDemo.setVisible(true);
}
}

import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Frame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CardLayoutDemo extends Frame
implements ActionListener
{
Button b1 = new Button(“Button1″);
Button b2 = new Button(“Button2″);
Button b3 = new Button(“Button3″);
Button b4 = new Button(“Button4″);
Button b5 = new Button(“Button5″);

CardLayout card = new CardLayout();

CardLayoutDemo(String title)
{
super(title);
super.setSize(500,400);

//将窗口设置为卡片布局
super.setLayout(card);

super.add(b1,”b1″);
super.add(b2,”b2″);
super.add(b3,”b3″);
super.add(b4,”b4″);
super.add(b5,”b5″);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
//翻书
card.next(this);
}

public static void main(String[] args)
{
CardLayoutDemo cardLayoutDemo =
new CardLayoutDemo(“CardLayout演示”);

cardLayoutDemo.setVisible(true);
}
}

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
public class GridLayoutDemo extends Frame
{
Button[] buttons = new Button[9];

GridLayoutDemo(String title)
{
super(title);
super.setSize(500,400);

super.setLayout(new GridLayout(3,3));

for(int i = 0; i < 9; i++)
{
buttons[i] = new Button(“b” + i);
super.add(buttons[i]);
}
}

public static void main(String[] args)
{
GridLayoutDemo gridLayoutDemo =
new GridLayoutDemo(“GridLayout演示”);
gridLayoutDemo.setVisible(true);
}
}

import java.awt.Color;
import java.awt.Panel;
import java.awt.Frame;
import java.awt.GridLayout;
public class ColorExample extends Frame
{
Panel aPaper = new Panel();

ColorDemo(String title)
{
super(title);
super.setSize(500,400);

aPaper.setBackground(Color.RED);
super.add(aPaper);
}

public static void main(String[] args)
{
ColorDemo colorDemo =
new ColorDemo(“Color”);

colorDemo.setVisible(true);
}
}