Abstract Window Toolkit (AWT)
Ennek a fejezetnek a megvalósításához feltételezem a Java alapismereteket és a Java objektumorientált programozási ismereteket. Jelenleg fejlesztés alatt van, ajánlott a „Java GUI Swing” használata. Mi a különbség az AWT és a Swing között? Az AWT rendszerben készül programok minden operációs rendszeren másként nézne ki, mivel a komponensek kinézete a az operációs rendszer ablakozó felületéhez igazodik.
import java.awt.*; class Program extends Frame { Program() { setSize(400, 300); setVisible(true); } public static void main(String args[]) { new Program(); } }
Figyeljük meg, hogy az előző ablakunk valójában nem záródik be. Rá kell vennünk az ablakot, hogy figyelje a Close eseményt és lépjen ki erre.
import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; class Program extends Frame { Program() { addWindowListener(new AblakKezeles()); setSize(400, 300); setVisible(true); } class AblakKezeles extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String[] args) { new Program(); } }
import java.awt.Frame; import java.awt.Button; import java.awt.event.WindowListener; import java.awt.event.WindowEvent; class Program01 extends Frame { Button button1 = new Button("Klikk"); Program01() { addWindowListener(new WindowListener() { public void windowClosing(WindowEvent e) { System.exit(0); } public void windowDeactivated(WindowEvent e){} public void windowActivated(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowOpened(WindowEvent e){} }); add(button1); pack(); setVisible(true); } public static void main(String args[]) { new Program01(); } }
import java.awt.Frame; import java.awt.Button; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; class Program extends Frame { Button gomb; Program() { gomb = new Button("Klikkelj ide"); add(gomb); addWindowListener(new AblakKezeles()); setSize(400, 300); setVisible(true); } class AblakKezeles extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String[] args) { new Program(); } }
Ebben a formában kitölti az egész képernyőt.
Most alkalmazzuk a pack() metódust az ablakon, így az ablak igazodik annak tartalmához, vagyis felveszi a nyomógomb méretét.
import java.awt.Frame; import java.awt.Button; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; class Program extends Frame { Button gomb; Program() { gomb = new Button("Klikkelj ide"); add(gomb); addWindowListener(new AblakKezeles()); //~ setSize(400, 300); pack(); setVisible(true); } class AblakKezeles extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String[] args) { new Program(); } }
Vegyük észre, hogy megjegyzésbe tettem a setSize(400,300);-as utasítást, mivel a pack() metódus mellett nincs értelme.
import java.awt.Frame; import java.awt.Button; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; class Program extends Frame { Button gomb; Program() { gomb = new Button("Klikkelj ide erre a gombra"); gomb.addActionListener(new Gomb_Click()); add(gomb); addWindowListener(new AblakKezeles()); //~ setSize(400, 300); pack(); setVisible(true); } class Gomb_Click implements ActionListener { public void actionPerformed(ActionEvent e) { setTitle("Lenyomva"); } } class AblakKezeles extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String[] args) { new Program(); } }
import java.awt.Frame; import java.awt.event.WindowListener; import java.awt.event.WindowEvent; class Program extends Frame implements WindowListener { Program() { addWindowListener(this); setSize(400, 300); setVisible(true); } public void windowDeactivated(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { System.exit(0); } public void windowOpened(WindowEvent e) { } public static void main(String args[]) { new Program(); } }
import java.awt.Frame; import java.awt.Button; import java.awt.Label; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.FlowLayout; class Program extends Frame { Button gomb; Label cimke; Program() { gomb = new Button("Klikkelj ide erre a gombra"); cimke = new Label("Több komponensű program"); gomb.addActionListener(new Gomb_Click()); gomb.setBounds(10, 50, 150, 30); cimke.setBounds(10, 100, 180, 30); add(gomb); add(cimke); setLayout(null); addWindowListener(new AblakKezeles()); setSize(400, 300); setVisible(true); } class Gomb_Click implements ActionListener { public void actionPerformed(ActionEvent e) { setTitle("Lenyomva"); } } class AblakKezeles extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String[] args) { new Program(); } }
import java.awt.Frame; import java.awt.Button; import java.awt.Label; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.FlowLayout; class Program extends Frame { Button gomb; Label cimke; Program() { gomb = new Button("Klikkelj ide erre a gombra"); cimke = new Label("Több komponensű program"); gomb.addActionListener(new Gomb_Click()); add(gomb); add(cimke); setLayout(new FlowLayout()); addWindowListener(new AblakKezeles()); pack(); setVisible(true); } class Gomb_Click implements ActionListener { public void actionPerformed(ActionEvent e) { setTitle("Lenyomva"); } } class AblakKezeles extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String[] args) { new Program(); } }
import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; class Program extends Frame { Program() { addKeyListener(new BillentyuKezeles()); addWindowListener(new AblakKezeles()); setSize(400, 300); setVisible(true); } class BillentyuKezeles implements KeyListener { public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_A : setTitle("a"); break; case KeyEvent.VK_B : setTitle("b"); break; case KeyEvent.VK_C : setTitle("c"); break; case KeyEvent.VK_1 : setTitle("1"); break; case KeyEvent.VK_2 : setTitle("2"); break; } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } } class AblakKezeles extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String[] args) { new Program(); } }
import java.awt.Frame; import java.awt.Button; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; class Program extends Frame implements ActionListener { Button gomb; Program() { gomb = new Button("Nyomj meg"); gomb.setBounds(50, 50, 100, 30); gomb.addActionListener(this); add(gomb); setLayout(null); setSize(400, 300); setVisible(true); } public void actionPerformed(ActionEvent e) { setTitle("Esemény volt"); } public static void main(String args[]) { new Program(); } }
Több komponens
import java.awt.Frame; import java.awt.Button; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; class Program extends Frame implements ActionListener { Button gomb1; Button gomb2; Program() { gomb1 = new Button("Nyomj meg"); gomb2 = new Button("Vagy nyomd ezt"); gomb1.setBounds(50, 50, 100, 30); gomb2.setBounds(50, 90, 100, 30); gomb1.addActionListener(this); gomb2.addActionListener(this); add(gomb1); add(gomb2); setLayout(null); setSize(400, 300); setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource() == gomb1) setTitle("Első gomblt"); if(e.getSource() == gomb2) setTitle("Második gombon"); } public static void main(String args[]) { new Program(); } }
TabPanel egyszerűen:
import java.awt.*; import javax.swing.JTabbedPane; class tabpanel extends Frame { tabpanel(String cim) { super(cim); JTabbedPane tabbedPane; tabbedPane = new JTabbedPane(JTabbedPane.TOP); tabbedPane.addTab("Első", new Label("Felirat 1 ")); tabbedPane.addTab("Második", new Label("Felirat 2 ")); add(tabbedPane); } public static void main(String args[]) { tabpanel ablak = new tabpanel("tabpanel"); ablak.pack(); ablak.setVisible(true); } }
import java.awt.*; class Program extends Frame { Program() throws Exception { setSize(400, 300); setVisible(true); // Sleep for 3 seconds, then minimize Thread.sleep (3000); setState ( Frame.ICONIFIED ); // Sleep for 3 seconds, then restore Thread.sleep (3000); setState ( Frame.NORMAL ); // Sleep for 3 seconds, then kill window Thread.sleep (3000); setVisible (false); dispose(); // Terminate test System.exit(0); } public static void main(String args[]) throws Exception { new Program(); } }
import java.awt.Robot; import java.awt.AWTException; class Program01 { public static void main(String[] args) { try { Robot r = new Robot(); r.mouseMove(40,40); } catch(AWTException e) {} } }