博文

布局和事件(2007-12-22 21:16:00)

摘要:一个简单的布局和时间的组合程序,没什么可借鉴的的地方,只是作为曾经做过的一个标志。 import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class Copy extends JFrame implements ActionListener
{JPanel p1,p2;TextField t1,t2;Label l1,l2;JButton b1,b2;
 public static void main(String args[])
 {new Copy();
 }
 Copy()
 {addWindowListener(new WindowAdapter()
  {public void windowClosing(WindowEvent e)
   {System.exit(0);
   }
  });
  setLayout(new GridLayout(2,1));
  p1=new JPanel();p2=new JPanel();t1=new TextField();t2=new TextField();
  l1=new Label();l2=new Label();b1=new JButton("复制");b2=new JButton("复制");
  p1.setLayout(new BorderLayout());
  p2.setLayout(new GridLayout(3,1));
  p1.add("North",t1);p1.add("East",b1);p1.add("Center",l1);
  p2.add(t2);p2.add(l2);p2.add(b2);
  b1.addActionListener(this);b2.addActionListener(this);
  add(p1);add(p2);setVi......

阅读全文(2766) | 评论:0

单选按钮(2007-12-20 08:38:00)

摘要:最基本的单选按钮使用,见笑了。 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Check extends JFrame implements ActionListener
{JRadioButton c1,c2;ButtonGroup b;
 public static void main(String args[])
 {new Check();
 }
 public Check()
 {super("单选按钮");
  addWindowListener(new WindowAdapter()
  {public void windowClosing(WindowEvent e)
   {System.exit(0);
   }
  });
  b=new ButtonGroup();
  c1=new JRadioButton("启用",true);
  c2=new JRadioButton("禁用",false);
  b.add(c1);b.add(c2);
  c1.setEnabled(false);
  setLayout(null);add(c1);add(c2);
  c1.setBounds(60,140,60,20);c2.setBounds(180,140,60,20);
  c1.addActionListener(this);
  c2.addActionListener(this);
  setVisible(true);
  setSize(300,240);
 }
 public void actionPerformed(ActionEvent e)
 {if(e.getSource()==c2)......

阅读全文(2771) | 评论:0

JApplet上画圆(2007-12-16 21:49:00)

摘要:一个小程序,自己写一个网页文件嵌入运行就行了,不过没什么技术含量,仅作为一个学习过程加以收藏。 import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class RandomClick extends JApplet
{Color c;int x,y;
 public void init()
 {addMouseListener(new MouseAdapter()
  {public void mousePressed(MouseEvent e)
   {x=e.getX();y=e.getY();
    repaint();
   }
  });
 }
 public void paint(Graphics g)
 {c=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
    g.setColor(c);
    g.fillOval(x-10,y-10,20,20);
 }
}......

阅读全文(3369) | 评论:0

Java基础计算(2007-12-16 21:45:00)

摘要:一个小程序,自行编写一个HTML文件嵌入运行就行了。 import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Add extends Applet implements ActionListener
{Label ask,result;TextField answer;Button submit;int a=6,b=9;
 public static void main(String args[])
 {Add a=new Add();
  a.init();
 }
 public void init()
 {Panel p=new Panel();
  setLayout(new BorderLayout());
  p.setLayout(null);
  ask=new Label(Integer.toString(a)+"+"+Integer.toString(b));
  ask.setFont(new Font("宋体",Font.BOLD,16));
  answer=new TextField(5);
  result=new Label();
  submit=new Button("OK");
  submit.addActionListener(this);
  p.add(ask);p.add(answer);p.add(result);p.add(submit);
  ask.setBounds(50,20,70,20);answer.setBounds(160,20,70,20);
  result.setBounds(50,60,90,20);submit.setBounds(160,60,70,20);
  add("Center",p);p.setVisible(true);
 }
 publ......

阅读全文(3062) | 评论:0

Java画布画圆(2007-12-14 22:07:00)

摘要:这个程序很简单,没有什么好说的了,希望大家多多指教一下,在JDK1.6下通过。 import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Draw extends WindowAdapter implements ActionListener,WindowListener
{Canvas c;TextField radius,x,y;Button b;
 public static void main(String args[])
 {Draw d=new Draw();
  Frame f=new Frame("画布画圆");
  d.c=new Canvas();
  d.x=new TextField(5);
  d.y=new TextField(5);
  d.radius=new TextField(5);
  d.b=new Button("清空画布");
  Label x1=new Label("请输入左上角x:");
  Label y1=new Label("请输入左上角y:");
  Label radius1=new Label("请输入半径:");
  Panel p=new Panel();
  f.setLayout(new BorderLayout());
  p.setLayout(new FlowLayout());
  f.add("Center",d.c);
  f.add("South",p);
  p.add(radius1);p.add(d.radius);p.add(x1);p.add(d.x);
  p.add(y1);p.add(d.y);p.add(d.b);
  d.x.addActionListener(d);
  d.y.addActionListener(d);
......

阅读全文(6408) | 评论:0

是非程序(2007-12-13 08:11:00)

摘要:此程序在JDK1.6环境下一切正常,甚至可以不添加ButtonGroup也可以使用JRadioButton,但是至少在JDK1.4中是不正确的,需要明确使用容器,在不同的版本之间已经有了语法上的不同,而且也充分体现了教学中与实际中的的确确的脱节,还是跟上时代的好啊。 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Check extends JFrame implements ActionListener
{JRadioButton c1,c2;ButtonGroup b;
 public static void main(String args[])
 {new Check();
 }
 public Check()
 {super("单选按钮");
  addWindowListener(new WindowAdapter()
  {public void windowClosing(WindowEvent e)
   {System.exit(0);
   }
  });
  b=new ButtonGroup();
  c1=new JRadioButton("启用",true);
  c2=new JRadioButton("禁用",false);
  b.add(c1);b.add(c2);
  c1.setEnabled(false);
  setLayout(null);add(c1);add(c2);
  c1.setBounds(60,140,60,20);c2.setBounds(180,140,60,20);
  c1.addActionListener(this);
  c2.addActionListener(this);
  setVisible(true);
&......

阅读全文(2799) | 评论:0

Java中Menu的简单使用(2007-12-13 08:02:00)

摘要:在一个JFrame上使用Menu,Menu使用要先添加MenuBar,Menu加到MenuBar上,MenuItem加到Menu上,如此级联添加。 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Me extends JFrame implements ActionListener
{MenuBar mb;Menu m;MenuItem mi1,mi2,mi3;
 public Me()
 {addWindowListener(new WindowAdapter()
  {public void windowClosing(WindowEvent e)
   {System.exit(0);
   }
  });
  mb=new MenuBar();
  m=new Menu("文件");
  mi1=new MenuItem("功能1");
  mi2=new MenuItem("功能2");
  mi3=new MenuItem("退出");
  m.add(mi1);m.add(mi2);m.addSeparator();m.add(mi3);mb.add(m);
  mi1.addActionListener(this);
  mi2.addActionListener(this);
  mi3.addActionListener(this);
  setMenuBar(mb);
  setSize(320,240);setVisible(true);
 }
 public static void main(String args[])
 {new Me();
 }
 public void actionPerformed(ActionEvent e)......

阅读全文(7088) | 评论:0

Java中布局的简单使用(2007-12-13 07:58:00)

摘要:本例是为了体现Java中布局的使用,在一个Frame上添加了两个Panel,在两个Panel上分别体现了一个Butoon,一个Label和一个TextField的边框布局和网格布局,而Frame本身使用了网格布局。 import java.awt.*;
import java.awt.event.*; class TwoLayout extends WindowAdapter implements WindowListener,ActionListener
{Frame f;Panel pborder,pgrid;
 Label lborder,lgrid;TextField tborder,tgrid;Button bborder,bgrid;
 public static void main(String args[])
 {TwoLayout a=new TwoLayout();
  a.execute();
 }
 public void execute()
 {f=new Frame("布局");
  f.setLayout(new GridLayout(2,1));
  f.addWindowListener(this);
  pborder=new Panel();pgrid=new Panel();
  f.add(pborder);f.add(pgrid);
  pborder.setLayout(new BorderLayout());
  pgrid.setLayout(new GridLayout(3,1));
  lborder=new Label();tborder=new TextField();bborder=new Button("边框布局复制");
  lgrid=new Label();tgrid=new TextField();bgrid=new Button("网格布局复制");
  lborder.setBackground(Color.yellow);l......

阅读全文(3834) | 评论:0

Java中awt的简单使用(2007-12-13 07:51:00)

摘要:这是Java中简单的java.awt包的使用,并且使用了监听接口,只是作为一个程序思想的展示,没有什么高深之处。 import java.awt.*;
import java.awt.event.*; class ChangeColor extends WindowAdapter implements WindowListener,ActionListener
{Frame f;Button r,b,y;
 public static void main(String args[])
 {ChangeColor c=new ChangeColor();
  c.execute();
 }
 public void execute()
 {f=new Frame("改变颜色");
  f.setLayout(new FlowLayout());
  f.addWindowListener(this);
  r=new Button("红色");
  b=new Button("蓝色");
  y=new Button("黄色");
  f.add(r);f.add(b);f.add(y);
  r.addActionListener(this);
  b.addActionListener(this);
  y.addActionListener(this);
  f.setSize(400,300);f.setVisible(true);
 }
 public void windowClosing(WindowEvent e)
 {System.exit(0);
 }
 public void actionPerformed(ActionEvent e)
 {if(e.getSource()==r)f.setBackground(Color.red);
  else if(e.getSou......

阅读全文(3373) | 评论:0

Java中接口的简单使用(2007-12-13 07:45:00)

摘要:接口和C++中的抽象类差不多,是只能被实现但是不能在自身实现某种方法的一种结构,下面的程序是一个Interface的简单应用,功能是实现一个数组中几个数据的升序排序和求最大值,实际上排序后最后一个数组元素即为最大值,此处使用一个独立方法只为体现接口的用法,确有欠妥。 interface vir  //这是一个接口,用interface关键字声明
{void bigsmall(int a[]);  //这里在接口里声明bigsmall和sort两个方法用来被实现它的类继承
 void sort(int b[]);
} class function implements vir  //这是一个来实现上面的接口的类,即implements(实现)接口
{public void bigsmall(int c[])  //实现接口的第一个函数,形式必须和接口中相同并且        {int i=0,small=c[0],big=c[0];  //前面要有public关键字
  for(i=1;i<c.length;i++)
  {if(c[i]<small)small=c[i];
   if(c[i]>big)big=c[i];
  }
  System.out.print("The smallest number is: "+small+'\n');
  System.out.print("The biggest number is: "+big+'\n');
 }
 public void sort(int c[])  //这是实现接口的第二个函数,形式与与上相同,带public关键字
 {int j=0,k=0,temp=0;  //程序本身及算法没有什么可说之处了
  for(j=0;j<4;j++)
  {for(k=j+1;k<5;k++)
&nb......

阅读全文(2871) | 评论:0