120 likes | 287 Views
Java GUI - java.awt and javax.swing -. Useful Components. JFrame (or Frame) JPanel JLabel JTextField JButton JTextArea JTable JCheckBox JComboBox …. Listener. ActionListener KeyListener MouseListener … . import java.io.*; import java.awt.*; import java.awt.event.*;
E N D
Useful Components • JFrame (or Frame) • JPanel • JLabel • JTextField • JButton • JTextArea • JTable • JCheckBox • JComboBox • …
Listener • ActionListener • KeyListener • MouseListener • …
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class java_gui_example extends JFrame implements ActionListener { public Button b1; public JButton jb2; public JButton jb3; public JButton jb4; //想放在JPanel裡的東西=== public JPanel center_jp; public JLabel jl1; public JTextField jtf1; public my_jpanel second_jp; public JTextArea jta; //共用的listener public my_listener_for_action listener1; public my_listener_for_mouse listener2; public my_listener_for_key listener3;
public java_gui_example() { super("我就是最左上角的標題!"); init(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(640,480); this.setVisible(true); //this.pack(); } void init() { try { listener1=new my_listener_for_action(this); listener2=new my_listener_for_mouse(this); listener3=new my_listener_for_key(this); b1=new Button("上,老式Button物件,加到BorderLayout.NORTH"); jb2=new JButton("下,新式Button物件,加到BorderLayout.SOUTHT"); jb3=new JButton("左"); jb4=new JButton("右"); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(b1,BorderLayout.NORTH); this.getContentPane().add(jb2,BorderLayout.SOUTH); this.getContentPane().add(jb3,BorderLayout.WEST); this.getContentPane().add(jb4,BorderLayout.EAST);
b1.addActionListener(this); jb2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { jta.append("專屬\"下\"的ActionListener聽到了動作!\n"); } }); jb3.addActionListener(listener1); jb4.addActionListener(listener1); init_center_jp(); this.getContentPane().add(center_jp,BorderLayout.CENTER); } catch(Exception e) { System.out.println(e); } } void init_center_jp() { //最簡單的FlowLayout // center_jp=new JPanel(new FlowLayout(FlowLayout.RIGHT));
//簡單 但是不太好看的GridLayout int row_count=2; int column_count=1; center_jp=new JPanel(new GridLayout(row_count,column_count)); jl1=new JLabel("<html><body bgColor='yellow'>我是<b style='Color:red'>JLabel</b><br>事實上我可以寫HTML喔!</body></html>"); jtf1=new JTextField("",20); second_jp=new my_jpanel(); jta=new JTextArea(); jl1.addMouseListener(listener2); jtf1.addKeyListener(listener3); center_jp.add(jl1); center_jp.add(jtf1); center_jp.add(second_jp); center_jp.add(new JScrollPane(jta)); } public void actionPerformed(ActionEvent ev) { jta.append("繼承的listener 聽到\"上\"的動作!\n"); }
public void actions(ActionEvent ev) { try { if(ev.getSource().equals(jb3)) jta.append("共用的action listener聽到\"左\"的動作!\n"); if(ev.getSource().equals(jb4)) jta.append("共用的action listener聽到\"右\"的動作!\n"); } catch(Exception e) { System.out.println("actions exception:"+e); } } public void mouse_talk(String a) { jta.append(a); } public static void main(String args[]) { java_gui_example a=new java_gui_example(); } }
class my_jpanel extends JPanel { JButton jb; JCheckBox jcb; public my_jpanel() { setLayout(null); this.setBackground(Color.red); jb=new JButton("亂放"); jb.setBounds(10,20,200,25); this.add(jb); jcb=new JCheckBox("如果出席數超過70%就打勾!",false); jcb.setBounds(12,40,220,25); this.add(jcb); } }
class my_listener_for_action implements ActionListener { java_gui_example adaptee; my_listener_for_action(java_gui_example adaptee) { this.adaptee=adaptee; } public void actionPerformed(ActionEvent e) { adaptee.actions(e); } }
class my_listener_for_mouse extends MouseAdapter { java_gui_example adaptee; my_listener_for_mouse(java_gui_example adaptee) { this.adaptee=adaptee; } public void mouseClicked(MouseEvent e) { adaptee.mouse_talk("呃~~被按到囉~~\n"); } public void mouseEntered(MouseEvent e) { adaptee.mouse_talk("阿~~進來囉~~\n"); } public void mouseExited(MouseEvent e) { adaptee.mouse_talk("嗯~~出來囉~~\n"); } }
class my_listener_for_key extends KeyAdapter { java_gui_example adaptee; my_listener_for_key(java_gui_example adaptee) { this.adaptee=adaptee; } public void keyReleased(KeyEvent e) { adaptee.mouse_talk("現在被輸入的是"+e.getKeyChar()+"\n"); } }