-
Swing (interface) vs. AWT (event handling)
-
import javax.swing.*; public class MyFrame { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(400, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
-
import javax.swing.*; public class MyFrameWithComponents { public static void main(String[] args) { JFrame frame = new JFrame( "MyFrameWithComponen") JButton jbtOK = new JButton("OK"); // Add a button into the frame frame.add(jbtOK); frame.setSize(400, 300); frame.setDefau1tC10seOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRe1ativeTo(nu11); // Center the frame frame.setVisib1e(true); } }
-
For arraning a position into containers
-
Color
class-
Color mycolor = new Color(228, 100, 50); // how to set for button mybutton.setBackground(Color.mycolor); mybutton.setForeground(Color.mycolor);
-
-
In
JFrame
we add a componentJPanel
-
Event-driven programming (depends of action) <-> procedural programming (step by step)
-
HandleEvent1 with the use of Inner Classes
-
User Action Object Generated Click a button JButton ActionEvent Click a check box JCheckBox ItemEvent, ActionEvent Click a radio button JRadioButton ItemEvent, ActionEvent Press return on a text field JTextField ActionEvent Select a new item JComboBox ItemEvent, ActionEvent Window opened, closed, etc. Window WindowEvent Mouse pressed, released, etc. Component MouseEvent Key released, pressed, etc. Component KeyEvent
-
Event Class Listener Interface Listener Methods (Handlers) ActionEvent ActionListener actionPerformed(ActionEvent) ItemEvent ItemListener itemStateChanged(ItemEvent) WindowEvent WindowListener windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) ContainerEvent ContainerListener componentAdded(ContainerEvent) componentRemoved(ContainerEvent) MouseEvent MouseListener mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseClicked(MouseEvent) mouseExited(MouseEvent) mouseEntered(MouseEvent) KeyEvent KeyListener keyPressed(KeyEvent) keyReleased(KeyEvent) keyTypeed(KeyEvent)