Java初學gui
阿新 • • 發佈:2018-11-11
package guiDemo; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import com.sun.org.apache.xalan.internal.xsltc.compiler.sym; public class FrameDemo { public static void main(String[] args) { Frame f = new Frame("我是一個視窗"); f.setSize(500, 400);//設定視窗的尺寸 f.setLocation(300, 50);//設定初始視窗的位置距離螢幕左上角 0,0 //f.setIconImage(Toolkit.getDefaultToolkit().createImage("圖片路徑"));//設定視窗的圖示 //窗體預設是便捷佈局 管理器 東南西北中方位填充 f.setLayout(new FlowLayout());//設定窗體為流佈局管理器, 從左到右依次排放 Button b1 = new Button("按鈕1");//建立一個按鈕 Button b2 = new Button("按鈕2");//建立一個按鈕 Button b3 = new Button("按鈕3");//建立一個按鈕 Button b4 = new Button("按鈕4");//建立一個按鈕 Button b5 = new Button("按鈕5");//建立一個按鈕 f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);//新增按鈕到佈局管理器 //新增按鈕到五個方位 邊界佈局管理器才行 /* f.add("Center", b1); f.add("East", b2); f.add("North", b3); f.add("West", b4); f.add("South", b5);*/ /** * 滑鼠監聽 MouseListener MouseAdapter */ //給按鈕加上滑鼠監聽 /*b1.addMouseListener(new MouseAdapter() { //當點選按鈕1滑鼠釋放時關閉視窗, @Override public void mouseReleased(MouseEvent e) { System.exit(0); } });*/ /* b2.addMouseListener(new MouseAdapter() { 如果設定成點選關閉可能會出現bug, @Override public void mouseClicked(MouseEvent e) { System.exit(0); } });*/ /** * 鍵盤監聽KeyListener KeyAdapter */ //給按鈕加上鍵盤監聽 b1.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { System.out.println(e.getKeyCode()); if(e.getKeyCode() == KeyEvent.VK_SPACE) {//按空格推出在按鈕上新增 System.exit(0); } } }); /** * 動作監聽 addActionListener 只有一個方法不需要加介面卡 */ b2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("我被添加了動作,點選我啊!快點選我"); } }); //給窗體加上 監聽 //f.addWindowListener(new MyWindowsListener());//自定義類實現windowsListener太浪費資源所以使用介面卡模式 f.addWindowListener(new WindowAdapter() {//匿名物件重寫 windowAdater 的方法實現點選關閉視窗 public void windowClosing(WindowEvent e) { System.out.println("視窗已關閉"); System.exit(0); } }); f.setVisible(true);//初始視窗預設不可見,要使用此方法設定可見 } } class MyWindowsAdapter extends WindowAdapter{ /** * 重寫closing方法實現點選關閉窗體 */ @Override public void windowClosing(WindowEvent e) { System.exit(0); } } class MyWindowsListener implements WindowListener{ @Override public void windowOpened(WindowEvent e) { System.out.println("windowOpened"); } @Override public void windowClosing(WindowEvent e) { System.out.println("Cloising"); System.exit(0); } @Override public void windowClosed(WindowEvent e) { System.out.println("Closed"); } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } }