ȥå   Խ ʬ Хåå ź ʣ ̾ѹ   ñ측 ǽ   إ   ǽRSS

java/AWT

Last-modified: 2008-01-19 () 10:07:34 (3595d)
Top / java / AWT


ꥹʡϿγƼˡ

ꥹʡ󥿥ե򥤥ץȤƺꥸʥΥꥹʡ饹ˡƤӽФFrame󥹥󥹤򻲾ȤեɤѰդˡ

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	Button b1;
	
	public SampleApp() {
		this.setSize(300, 200);
		b1 = new Button("Click");
		MyButtonActionAdapter ba = new MyButtonActionAdapter();
		ba.sampleApp = this;
		b1.addActionListener(ba);
		this.add(b1,BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
}

class MyButtonActionAdapter implements ActionListener {
	SampleApp sampleApp;
	
	public void actionPerformed(ActionEvent ev) {
		sampleApp.b1.setLabel("you clicked.");
	}
}

ꥹʡ󥿥ե򥤥ץȤƺꥸʥΥꥹʡ饹Frame饹Ѿꥸʥ륯饹饹ˤˡ

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	Button b1;
	
	public SampleApp() {
		this.setSize(300, 200);
		b1 = new Button("Click");
		b1.addActionListener(new MyButtonActionAdapter());
		this.add(b1,BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}

	class MyButtonActionAdapter implements ActionListener {

		public void actionPerformed(ActionEvent ev) {
			b1.setLabel("you clicked.");
		}
	}
}

Frame饹Ѿ饹ˡꥹʡ󥿥ե򥤥ץȤ٥ȥꥹʡˡ

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame implements ActionListener {
	Button b1;
	
	public SampleApp() {
		this.setSize(300, 200);
		b1 = new Button("Click");
		b1.addActionListener(this);
		this.add(b1,BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}

	public void actionPerformed(ActionEvent ev) {
		b1.setLabel("you clicked.");
	}
}

٥ȥꥹʡɲûˡ٥ȥꥹʡ̵̾饹Ȥˡ

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	Button b1;
	
	public SampleApp(){
		this.setSize(300, 200);
		b1 = new Button("Click");
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				b1.setLabel("you clicked.");
			}
		});
		this.add(b1,BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
}

ܥåĤ̵̾饹륵ץ

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {

	public SampleApp(){
		this.setSize(300, 200);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent ev) {
				System.exit(0);
			}
		});
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
}

쥤ȥޥ͡㡼ѹˡ

FlowLayoutǥܥ10ĤϿ륵ץ

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	
	public SampleApp() {
		this.setSize(300, 200);
		this.setLayout(new FlowLayout());
		for (int i = 0; i < 10; i++) {
			this.add(new Button("No," + i));
		}
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
}

GridLayoutǥܥ10ĤϿ륵ץ

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	
	public SampleApp() {
		this.setSize(300, 200);
		this.setLayout(new GridLayout(4, 4));
		for (int i = 0; i < 10; i++) {
			this.add(new Button("No," + i));
		}
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
}

쥤ȥޥ͡Ȥ鷺ˡܥ10Ĥ֤֤륵ץ

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	
	public SampleApp() {
		this.setSize(300, 200);
		this.setLayout(new GridLayout(4, 4));
		for (int i = 0; i < 10; i++) {
			Button b = new Button("No," + i);
			b.setBounds(new Rectangle(i * 25 + 50, i * 25 + 50, 100, 20));
			this.add(b);
		}
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
}

Ƽ拾ݡͥȥץ

Checkbox

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	Checkbox cb1;
	
	public SampleApp() {
		this.setSize(300, 200);
		cb1 = new Checkbox("not checked.");
		cb1.addItemListener(new MyItemAdapter());
		this.add(cb1,BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
	class MyItemAdapter implements ItemListener {
		public void itemStateChanged(ItemEvent ev) {
			if (cb1.getState()) {
				cb1.setLabel("checked.");
			} else {
				cb1.setLabel("not checked.");
			}
		}
	}
}

RadioButton

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	Button b1;
	Label l1;
	CheckboxGroup cg1;
	
	public SampleApp() {
		this.setSize(300, 200);
		this.setLayout(new GridLayout(5, 1));
		cg1 = new CheckboxGroup();
		for (int i = 0; i < 3; i++) {
			Checkbox cb = new Checkbox("No," + i, cg1, false);
			this.add(cb);
		}
		l1 = new Label();
		this.add(l1);
		b1 = new Button("Click");
		b1.addActionListener(new MyActionAdapter());
		this.add(b1);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
	class MyActionAdapter implements ActionListener {
		
		public void actionPerformed(ActionEvent ev) {	
			Checkbox cb = cg1.getSelectedCheckbox();
			l1.setText(cb.getLabel());
		}
	}
}

TextField

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	Label l1;
	TextField t1;
	
	public SampleApp() {
		this.setSize(300, 200);
		l1 = new Label("0");
		this.add(l1,BorderLayout.CENTER);
		t1 = new TextField("0",20);
		t1.addActionListener(new MyActionAdapter());
		this.add(t1,BorderLayout.SOUTH);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
	class MyActionAdapter implements ActionListener {
		public void actionPerformed(ActionEvent ev) {
			try {
				int n = Integer.parseInt(t1.getText());
				n /= 1.05;
				l1.setText(Integer.toString(n));
			} catch(Exception ex) {}
		}
	}
}

Choice

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	Label l1;
	Choice c1;
	
	public SampleApp() {
		this.setSize(300, 200);
		l1 = new Label();
		this.add(l1,BorderLayout.NORTH);
		c1 = new Choice();
		c1.add("Japan");
		c1.add("America");
		c1.add("Europa");
		c1.add("Asia");
		c1.add("Africa");
		c1.add("Other");
		c1.addItemListener(new MyItemAdapter());
		this.add(c1,BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
	class MyItemAdapter implements ItemListener {
		public void itemStateChanged(ItemEvent ev) {
			int n = c1.getSelectedIndex();
			String str = c1.getSelectedItem();
			l1.setText(Integer.toString(n) + "," + str);
		}
	}
}

List

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	Label l1;
	List lt1;
	
	public SampleApp() {
		this.setSize(300, 200);
		l1 = new Label();
		this.add(l1,BorderLayout.NORTH);
		lt1 = new List(5, true);
		lt1.add("Japan");
		lt1.add("America");
		lt1.add("Europa");
		lt1.add("Asia");
		lt1.add("Africa");
		lt1.add("Other");
		lt1.addItemListener(new MyItemAdapter());
		this.add(lt1, BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
	class MyItemAdapter implements ItemListener {
		public void itemStateChanged(ItemEvent ev) {
			String str = "";
			for (int i = 0; i < lt1.getItemCount(); i++) {
				if (lt1.isIndexSelected(i))
					str += lt1.getItem(i) + " ";
			}
			l1.setText(str);
		}
	}
}

٥ˡ

MouseEvent

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	Label l1;
	
	public SampleApp() {
		this.setSize(300, 200);
		l1 = new Label();
		this.add(l1,BorderLayout.NORTH);
		this.addMouseListener(new MyMouseAdapter());
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
	class MyMouseAdapter extends MouseAdapter {
		public void mouseClicked(MouseEvent ev) {
			int x = ev.getX();
			int y = ev.getY();
			int n = ev.getClickCount();
			String str = "X:" + Integer.toString(x);
			str += " Y:" + Integer.toString(y);
			str += " click:" + Integer.toString(n);
			l1.setText(str);
		}
	}
}

KeyEvent

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	TextField t1, t2;
	Label l1;
	
	public SampleApp() {
		this.setSize(300, 200);
		t1 = new TextField();
		t1.addKeyListener(new MyKeyAdapter());
		this.add(t1,BorderLayout.NORTH);
		t2 = new TextField();
		t2.addKeyListener(new MyKeyAdapter());
		this.add(t2,BorderLayout.SOUTH);
		l1 = new Label();
		this.add(l1,BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
	class MyKeyAdapter extends KeyAdapter {
		public void keyTyped(KeyEvent ev) {
			char c = ev.getKeyChar();
			TextField t = (TextField)ev.getSource();
			String s = t.getName();
			l1.setText("\"" + c + "\"\t" + s);
		}
	}
}

FocusEvent

import java.awt.*;
import java.awt.event.*;

public class SampleApp extends Frame {
	TextField t1, t2;
	Label l1;
	
	public SampleApp() {
		this.setSize(300, 200);
		t1 = new TextField();
		t1.addFocusListener(new MyFocusAdapter());
		this.add(t1,BorderLayout.NORTH);
		t2 = new TextField();
		t2.addFocusListener(new MyFocusAdapter());
		this.add(t2,BorderLayout.SOUTH);
		l1 = new Label();
		this.add(l1,BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new SampleApp();
	}
	
	class MyFocusAdapter extends FocusAdapter {
		public void focusGained(FocusEvent ev) {
			String s1 = "";
			String s2 = "";
			TextField t1 = (TextField)ev.getSource();
			TextField t2 = (TextField)ev.getOppositeComponent();
			if (t1 != null) s1 = t1.getName();
			if (t2 != null) s2 = t2.getName();
			l1.setText(s1 + "  " + s2);
		}
	}
}