当前位置:文档之家› java面向对象程序例子

java面向对象程序例子

之前我在网上查找java习题时发现一个现象,很多提供习题的人以一种错误的观念来对待java———他们将C语言的习题来当做java的习题,然后让读者来做练习,用面向过程语言的习题来训练面向对象的语言,对于此不知道诸位是一种什么感受... 因此提供几个java的小程序给大家



1.这是java鼠标事件监听程序,可以显示出你的鼠标点击的坐标位置,以及鼠标是否进入程序,或移出程序框之外。

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

public class Listen implements MouseMotionListener,KeyListener,ActionListener{
private JTextField tf,tf2;
private JFrame frame;
private JButton b1,b2;
public static void main(String args[]){
Listen listen=new Listen();
listen.go();
}

public void go(){
frame = new JFrame("事件监听示例");
Container contentPane=frame.getContentPane();
tf=new JTextField(30);
tf2=new JTextField(30);
contentPane.add(tf2,BorderLayout.SOUTH);
contentPane.add(tf,BorderLayout.NORTH);
JPanel pane=new JPanel();
contentPane.add(pane,BorderLayout.CENTER);
b1=new JButton("按钮1");
b2=new JButton("按钮2");
pane.add(b1);
pane.add(b2);
b1.addActionListener(this);//new actionPerformed(ActionEvent e)
b2.addMouseListener(this);//new mousePressed(MouseEvent e)
frame.addMouseMotionListener(this);
frame.addKeyListener(this);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public void mouseDragged(MouseEvent e){
String s="Mouse Dragging:X="+e.getX()+" "+"Y="+e.getY();
tf.setText(s);
}

public void mouseMoved(MouseEvent e){}


public void keyPressed(KeyEvent e){

String s2="You are press the key!";
tf2.setText(s2);
}

public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}

public void actionPerformed(ActionEvent e){
String s3="You are press the button";
tf2.setText(s3);
}
}
}




2.几种消息框。




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

public class JOptionPaneDemo implements ActionListener{
JFrame frame = new JFrame("JOptionPane Demo");
JTextField tf= new JTextField();
JButton messageButton,ConfirmButton,InputButton,OptionButton;

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

public void go(){
messageButton=new JButton("message dialog");
messageButton.addActionListener(this);
ConfirmButton=new JButton("Confire dialog");
ConfirmButton.addActionListener(this);
InputButton=new JButton("Input dialog");
InputButton.addActionListener(this);
OptionButton=new JButton("Option dialog");
OptionButton.addActionListener(this);

JPanel jp=new JPanel();
jp.add(messageButton);
jp.add(ConfirmButton);
jp.add(InputButton);
jp.add(OptionButton);

Container cp=frame.getContentPane();
cp.add(jp,BorderLayout.CENTER);
cp.add(tf,BorderLayout.SOUTH);
frame.setDe

faultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e){
JButton button=(JButton) e.getSource();
if(button==messageButton){
JOptionPane.showMessageDialog(frame,"File not found","An error",JOptionPane.ERROR_MESSAGE);
}

if(button==ConfirmButton){
int select = JOptionPane.showConfirmDialog(frame,"Creat one","Confirm",JOptionPane.YES_NO_OPTION);
if(select==JOptionPane.YES_OPTION);
tf.setText("choose YES");
if(select==JOptionPane.NO_OPTION);
tf.setText("choose NO");
if(select==JOptionPane.CLOSED_OPTION);
tf.setText("Closed");
}

if(button==InputButton){
Object[] possibleValues={"First","Second","Third",};
Object selectedValue=JOptionPane.showInputDialog(frame,"Choose one","Input",https://www.doczj.com/doc/0f13620026.html,RMATION_MESSAGE,null,possibleValues,possibleValues[0]);
if(selectedValue != null)
tf.setText(selectedValue.toString());
else
tf.setText("Cloosed");
}

if(button==OptionButton){
Object[] options={"OK","CANCEL"};
int select=JOptionPane.showOptionDialog(frame,"Click OK to continue","Warning",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
if(select==0)
tf.setText("choose OK");
else if(select==1)
tf.setText("choose CANCEL");
else if(select==-1)
tf.setText("Closed");
}
}
}


3.文件打开,保存,删除。


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class JFileChooserDemo implements ActionListener{
JFrame frame = new JFrame("JFileChooserDemo");
JFileChooser fc=new JFileChooser();
JTextField tf= new JTextField();
JButton openButton,saveButton,deleteButton;

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

public void go(){
ImageIcon openIcon=new ImageIcon("open.gif");
openButton=new JButton("Open a File...",openIcon);
openButton.addActionListener(this);

ImageIcon saveIcon=new ImageIcon("save.gif");
saveButton=new JButton("Save a File...",saveIcon);
saveButton.addActionListener(this);

ImageIcon deleteIcon=new ImageIcon("delete.gif");
deleteButton = new JButton("Delete a File...",deleteIcon);
deleteButton.addActionListener(this);

JPanel jp=new JPanel();
jp.add(openButton);
jp.add(saveButton);
jp.add(deleteButton);

Container cp=frame.getContentPane();
cp.add(jp,BorderLayout.CENTER);
cp.add(tf,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e){
JButton button=(JButton) e.getSource();

if(button==openButton){
int select=fc.showOpenDialog(frame);
if(select==JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
tf.setText("Opening:"+file.getName());
}else{
tf.setText("Open command cancelled by user");
}
}

if(button==saveButton){
int select=fc.showSaveDialog(frame);
if(select==JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();


tf.setText("Saving:"+file.getName());
}
else{
tf.setText("Save command cancelled by user");
}
}

if(button==deleteButton){
int select = fc.showDialog(frame,"删除");
if(select==JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
tf.setText("Deteling:"+file.getName());
}
else{
tf.setText("Detele command cancelled by user");
}
}
}
}



4.鼠标


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


public class mouseContral extends MouseAdapter implements MouseMotionListener,KeyListener,ActionListener{

JFrame frame;
JTextField tf;
JButton b;

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

void go(){
frame=new JFrame("许氏鼠标监视程序");
tf=new JTextField();

b=new JButton("关闭本程序");
b.addActionListener(this);

Container cp=frame.getContentPane();
cp.add(tf,BorderLayout.SOUTH);
cp.add(b,BorderLayout.NORTH);
//frame.getContentPane().add(tf,BorderLayout.SOUTH);

frame.addMouseMotionListener(this);
frame.addKeyListener(this);
frame.addMouseListener(this);

frame.setSize(300,300);
frame.setVisible(true);
}


public void mouseMoved(MouseEvent e){
String s="X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}

public void mouseDragged(MouseEvent e){}

public void keyPressed (KeyEvent e){
tf.setText("D");
}

public void keyReleased(KeyEvent e){
tf.setText("U");
}

public void keyTyped(KeyEvent e){}


public void mouseEnter(MouseEvent e){
tf.setText("mouse is Entering!");
}

public void mouseExited(MouseEvent e){
tf.setText("mouse is Exiting!");
}

public void mousePressed(MouseEvent e){
tf.setText("mouse is Pressing!");
}

public void actionPerformed(ActionEvent e){
System.exit(0);
}

}



5.精简计算器代码

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random.*;

class MC extends JFrame implements ActionListener{

JButton b[]=new JButton[16];
JTextField tf,tf2;
JButton jb;

private int flag,guessnum,mynum,count=0;
private double num1,num2,temp=0;
String s[]={"0","1","2","3","4","5","6","7","8","9","+","-","*","/","C","="};



MC(){
JFrame frame=new JFrame("小巧计算器");
tf=new JTextField("0");
tf2=new JTextField("欢迎使用");

JPanel pl=new JPanel(new GridLayout(5,4));
Container cp=frame.getContentPane();
cp.add(pl,BorderLayout.CENTER);
cp.add(tf,BorderLayout.NORTH);

for(int i=0;i<16;i++){
b[i]=new JButton(s[i]);
pl.add(b[i]);
b[i].addActionListener(this);
b[i].setBackground(Color.green);
b[i].setForeground(Color.blue);
}

pl.add(tf2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e){
String s2="";


for(int i=0;i<10;i++){
if(e.getSource()==b[i])
temp=temp*10+i;
s2=String.valueOf(temp);
tf.setText(s2);
num1=Double.parseDouble(tf.getText()); /*此句若置此,则每次接受计

算器的键盘按下的数值都被获取转型并赋值,增加程序开销*/
}

for(int i=10;i<14;i++){ //上面标注的语句宜移至本循环
}

for(int i=10;i<14;i++)
if(e.getSource()==b[i]){
if(i==10)
flag=10;
else if(i==11)
flag=11;
else if(i==12)
flag=12;
else if(i==13)
flag=13;
num2=num1;
s2=String.valueOf(num2);
tf.setText(s2);
temp=0;

}

if(e.getSource()==b[14]){
num1=0;
num2=0;
flag=0;
temp=0;
tf.setText("0");
}

if(e.getSource()==b[15]){
if(flag==10)
num1=num2+num1;
else if(flag==11)
num1=num2-num1;
else if(flag==12)
num1=num2*num1;
else if(flag==13)
num1=num2/num1;
s2=String.valueOf(num1);
tf.setText(s2);

flag=0;
temp=0;

}
}
}

public class MyCalculator{
public static void main(String args[]){
MC mc=new MC();
}
}


6.框

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

public class MyFrame extends JFrame implements ActionListener {
private JPanel pan;
private JButton but;

MyFrame(){
this.setBounds(100, 100, 200, 200);
pan = new JPanel();
but = new JButton("点我出来新窗口");
but.addActionListener(this);
pan.add(but);
this.add(pan);
this.setAlwaysOnTop(true);
this.setVisible(true);
}
public static void main(String args[]){
new MyFrame();
}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==but){
this.setVisible(false);
new Another();
}

}
class Another extends JFrame{
Another(){
this.setTitle("新窗口");
this.setBounds(300, 300, 200, 200);
this.setAlwaysOnTop(true);
this.setVisible(true);
}
}

}



7.框架

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

public class MyFrame2 implements ActionListener{
JFrame frame;
JComboBox jcb;
JButton jb;
JLabel l1,l2;
JTextField tf;

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

public void go(){
frame=new JFrame("许氏框架二");
tf=new JTextField();

String[] item={"Tom","Jack","Mary","Linda"};
jcb=new JComboBox(item);


JPanel p1=new JPanel(new GridLayout(1,2));
l1=new JLabel("Information",JLabel.CENTER);
l2=new JLabel("Select Name",JLabel.CENTER);

p1.add(l1);
p1.add(l2);

JPanel p2=new JPanel(new GridLayout(1,2));
JPanel p3=new JPanel(new BorderLayout());
p2.add(tf);
p2.add(p3);


jb=new JButton("Close");
p3.add(jb,BorderLayout.SOUTH);
p3.add(jcb,BorderLayout.CENTER);

JPanel p4=new JPanel(new GridLayout(2,1));
p4.add(p1);
p4.add(p2);


jb.addActionListener(this);
jcb.addActionListener(this);

Container cp=frame.getContentPane();
cp.add(p4);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setVisible(true);
}



public void actionPerformed(ActionEvent e){

//JButton b2=(JButton) e.getSource();
if(e.getActionCommand().equals("Close"))
System.exit(0);



tf.setText(jcb.getSelectedItem()+" is a good student!");
}
}



相关主题
文本预览
相关文档 最新文档