Java GUI - java.awt and javax.swing -

slimwhimperΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 10 μήνες)

53 εμφανίσεις

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.*;

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");


}

}