Skip to content

Latest commit

 

History

History
109 lines (85 loc) · 3.06 KB

java-swing.md

File metadata and controls

109 lines (85 loc) · 3.06 KB

Java Swing

GUI Basics

  • Lecture Notes

  • Swing Notes

  • 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);
    	}
    }
  • JFrame with inheritence

Layout Manager

  • For arraning a position into containers

  • FlowLayout

  • GridLayout

  • BorderLayout

  • Color class

    • Color mycolor = new Color(228, 100, 50);
      // how to set for button
      mybutton.setBackground(Color.mycolor);
      mybutton.setForeground(Color.mycolor);

JPanel

Event-Handling in Java (Event-Driven Programming)

  • Lecture Notes

  • Event-driven programming (depends of action) <-> procedural programming (step by step)

  • HandleEvent

  • 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)