标签:sea 自动 直接 pack ima tor drawing selection stat
import java.awt.*; import java.awt.event.*; public class TestActionEvent2 { public static void main(String args[]) { Frame f = new Frame("Test"); Button b1 = new Button("Start"); Button b2 = new Button("Stop"); Monitor2 bh = new Monitor2(); b1.addActionListener(bh); b2.addActionListener(bh); b2.setActionCommand("game over"); f.add(b1,"North"); f.add(b2,"Center"); f.pack(); f.setVisible(true); } } class Monitor2 implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("a button has been pressed," + "the relative info is:\n " + e.getActionCommand()); } }
运行结果:
先后点击:“start”和“stop”
构造函数:TextField()
TextField(String Text)
TextField(int columns)
TextField(String Text,int columns)
常用方法:public void setText();
public String getText();
public void setEchoChar(char c) //设置此文本字段的回显字符。
public void setEditable(boolean b) //设置判断此文本组件是否可编辑的标志
public boolean isEditable()
public void setBackground(Color c)
public void select(int selectionStart,int selectionEnd) //选择指定开始位置和结束位置之间的文本。
public void selectAll() //选择此文本组件中的所有文本。
public void addActionListener(ActionListener l) //添加动作监听器
例1:
import java.awt.*; import java.awt.event.*; public class TFPassword { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new TFFrame2(); } } class TFFrame2 extends Frame { TFFrame2() { TextField tf = new TextField(); add(tf); tf.addActionListener(new TFActionListener2()); tf.setEchoChar(‘*‘); pack(); setVisible(true); } } class TFActionListener2 implements ActionListener { public void actionPerformed(ActionEvent e) { TextField tf = (TextField)e.getSource(); System.out.println(tf.getText()); tf.setText(""); } }
运行结果:
输入后,按回车键在运行栏中显示。
好处:方便访问包装类的成员;
可以防止其他类访问;
何时使用:该类不允许其他类访问时;
import java.awt.*; import java.awt.event.*; public class TFMath { public static void main(String[] args) { new TFFrame8().launchFrame(); } } class TFFrame8 extends Frame{ TextField t1,t2,t3; public void launchFrame(){ t1=new TextField(10); t2=new TextField(10); t3=new TextField(15); Label l=new Label("+"); Button bt=new Button("="); bt.addActionListener(new MyMonitor()); //bt.addActionListener(new MyMonitor(this)); setLayout(new FlowLayout()); add(t1); add(l); add(t2); add(bt); add(t3); pack(); setVisible(true); } private class MyMonitor implements ActionListener {//内部类;可直接访问包装类的成员变量和成员方法;生成TFFrame public void actionPerformed(ActionEvent e) { int n1 = Integer.parseInt(t1.getText()); int n2 = Integer.parseInt(t2.getText()); t3.setText("" + (n1+n2)); } } } /*class MyMonitor implements ActionListener{ TFFrame8 t=null; public MyMonitor(TFFrame8 tf){ t=tf; } public void actionPerformed(ActionEvent e) { int n1=Integer.parseInt(t.t1.getText()); int n2=Integer.parseInt(t.t2.getText()); t.t3.setText(""+(n1+n2)); } }*/
运行结果:
每次需要重画的时候自动调用Paint方法
Graphics可以想象成画笔,其中包含很多方法,具体查看API文档
import java.awt.*; public class TestPaint { public static void main(String[] args) { new PaintFrame().launchFrame(); } } class PaintFrame extends Frame { public void launchFrame() { setBounds(200,200,640,480); setVisible(true); } public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.red); g.fillOval(50, 50, 30, 30); g.setColor(Color.green); g.fillRect(80,80,40,40); g.setColor(c); } }
运行结果:
几种易混的方法:
mouseClicked(MouseEvent e) // 鼠标按键在组件上单击(按下并释放)时调用。
mousePressed(MouseEvent e) //鼠标按键在组件上按下时调用。
void mouseReleased(MouseEvent e) // 鼠标按钮在组件上释放时调用。
例:
import java.awt.*; import java.awt.event.*; import java.util.*; public class MyMouseAdapter{ public static void main(String args[]) { new MyFrame("drawing..."); } } class MyFrame extends Frame { ArrayList points = null;//ArrayList<Point> points=null(有泛型) MyFrame(String s) { super(s); points = new ArrayList(); setLayout(null); setBounds(300,300,400,300); this.setBackground(new Color(204,204,255)); setVisible(true); this.addMouseListener(new Monitor()); } public void paint(Graphics g) { Iterator i = points.iterator();//Iterator<Point> =points.iterator(); while(i.hasNext()){ Point p = (Point)i.next();//Point p = i.next(); g.setColor(Color.BLUE); g.fillOval(p.x,p.y,10,10); } } public void addPoint(Point p){ points.add(p); } } class Monitor extends MouseAdapter {//实现了MouseLisener接口,如果implement接口的话需要重写很多方法 public void mousePressed(MouseEvent e) { MyFrame f = (MyFrame)e.getSource(); f.addPoint(new Point(e.getX(),e.getY())); f.repaint();//先调用update方法然后调用paint方法 } }
运行结果:用鼠标任意点击会出现实心圆点;
匿名类:语句简单且不经常修改时使用
import java.awt.*; import java.awt.event.*; public class TestWindowClose { public static void main(String args[]) { new MyFrame55("MyFrame"); } } class MyFrame55 extends Frame { MyFrame55(String s) { super(s); setLayout(null); setBounds(300, 300, 400, 300); this.setBackground(new Color(204, 204, 255)); setVisible(true); //this.addWindowListener(new MyWindowMonitor()); this.addWindowListener( //匿名类 new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); System.exit(-1);//非正常退出。。。。但是和正常退出在效果上没差 } }); } /* class MyWindowMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) { setVisible(false); System.exit(0);//正常退出 } } */ }
运行结果:可以关闭窗口
标签:sea 自动 直接 pack ima tor drawing selection stat
原文地址:http://www.cnblogs.com/Lune-Qiu/p/7366259.html