java/AWT
Last-modified: 2018-11-14 (¿å) 15:24:09 (2316d)
- ¥ê¥¹¥Ê¡¼ÅÐÏ¿¤Î³Æ¼ïÊýË¡
- ¥ê¥¹¥Ê¡¼¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¥¤¥ó¥×¥ê¥á¥ó¥È¤·¤ÆºîÀ®¤·¤¿¥ª¥ê¥¸¥Ê¥ë¤Î¥ê¥¹¥Ê¡¼¥¯¥é¥¹Æâ¤Ë¡¢¸Æ¤Ó½Ð¤·¸µ¤ÎFrame¥¤¥ó¥¹¥¿¥ó¥¹¤ò»²¾È¤¹¤ë¥Õ¥£¡¼¥ë¥É¤òÍÑ°Õ¤¹¤ëÊýË¡
- ¥ê¥¹¥Ê¡¼¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¥¤¥ó¥×¥ê¥á¥ó¥È¤·¤ÆºîÀ®¤·¤¿¥ª¥ê¥¸¥Ê¥ë¤Î¥ê¥¹¥Ê¡¼¥¯¥é¥¹¤ò¡¢Frame¥¯¥é¥¹¤ò·Ñ¾µ¤·¤¿¥ª¥ê¥¸¥Ê¥ë¥¯¥é¥¹¤ÎÆâÉô¥¯¥é¥¹¤Ë¤¹¤ëÊýË¡¡ú¡ú¡ú
- Frame¥¯¥é¥¹¤ò·Ñ¾µ¤·¤¿¥¯¥é¥¹¤Ë¡¢¥ê¥¹¥Ê¡¼¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¥¤¥ó¥×¥ê¥á¥ó¥È¤·¡¢¥¤¥Ù¥ó¥È¥ê¥¹¥Ê¡¼¤òÄêµÁ¤¹¤ëÊýË¡
- ¥¤¥Ù¥ó¥È¥ê¥¹¥Ê¡¼Äɲûþ¤Ë¡¢¥¤¥Ù¥ó¥È¥ê¥¹¥Ê¡¼¤ò̵̾¥¯¥é¥¹¤È¤·¤ÆÄêµÁ¤¹¤ëÊýË¡¡ú¡ú
- ¥ì¥¤¥¢¥¦¥È¥Þ¥Í¡¼¥¸¥ã¡¼Êѹ¹ÊýË¡
- ³Æ¼ï¥³¥ó¥Ý¡¼¥Í¥ó¥È¥µ¥ó¥×¥ë
- ¥¤¥Ù¥ó¥ÈÍøÍÑÊýË¡
¥ê¥¹¥Ê¡¼ÅÐÏ¿¤Î³Æ¼ïÊýË¡ †
¥ê¥¹¥Ê¡¼¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¥¤¥ó¥×¥ê¥á¥ó¥È¤·¤ÆºîÀ®¤·¤¿¥ª¥ê¥¸¥Ê¥ë¤Î¥ê¥¹¥Ê¡¼¥¯¥é¥¹Æâ¤Ë¡¢¸Æ¤Ó½Ð¤·¸µ¤Î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); } } }