JFrame创建及基本用法
package com.fxb.gui; import java.awt.Button; import java.awt.FileDialog; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseWheelEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; public class Test1 { private static StringBuilder builder = new StringBuilder(); public static void pt(Object a){ builder.setLength(0); builder.append(a); System.out.println(builder.toString()); } public static void main(String[] args){ final Frame frame = new Frame("MyFrame"); frame.setSize(500, 400); frame.setLocation(300, 200); frame.setLayout(new FlowLayout()); Button button1 = new Button("Button1"); frame.add(button1); Button button2 = new Button("Button2"); frame.add(button2); Button button3 = new Button("Button3"); frame.add(button3); TextField field = new TextField(20); // field.setSize(100, 10); frame.add(field); WindowListener listener1 = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("Closing!"); System.exit(0); } public void windowActivated(WindowEvent e) { System.out.println("Actived!"); } public void windowClosed(WindowEvent e) { System.out.println("Closed!"); } public void windowOpened(WindowEvent e) { System.out.println("Opened!"); } }; frame.addWindowListener(listener1); ActionListener listener2 = new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println("Exit!"); System.exit(0); } }; button1.addActionListener(listener2); final JPopupMenu popupMenu = new JPopupMenu("Popup"); popupMenu.add(new JMenuItem("Pop1")); popupMenu.add(new JMenuItem("Pop2")); popupMenu.add(new JMenuItem("Pop3")); // final PopupMenu popupMenu = new PopupMenu("Popup"); // popupMenu.add(new MenuItem("Pop1")); // popupMenu.add(new MenuItem("Pop2")); // popupMenu.add(new MenuItem("Pop3")); // frame.add(popupMenu); MouseListener listener3 = new MouseAdapter() { public void mouseClicked(MouseEvent e) { if(e.getButton() == MouseEvent.BUTTON3){ popupMenu.show(e.getComponent(), e.getX(), e.getY()); }else{ if(e.getClickCount() == 2){ pt("Double click"); }else if(e.getClickCount() == 1){ pt("Single click"); } } } public void mouseWheelMoved(MouseWheelEvent e) { pt("Wheel move"); } public void mouseDragged(MouseEvent e) { pt("Mouse drag"); } public void mouseMoved(MouseEvent e) { pt("Mouse move"); } }; frame.addMouseListener(listener3); frame.setVisible(true); final FileDialog openDialog = new FileDialog(frame, "Open", FileDialog.LOAD); final FileDialog saveDialog = new FileDialog(frame, "Save", FileDialog.SAVE); MenuBar menuBar = new MenuBar(); final Menu menu1 = new Menu("文件"); final MenuItem[] items = new MenuItem[3]; ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getSource() == items[1]){ pt("Open"); openDialog.setVisible(true); String strDir = openDialog.getDirectory(); String strFile = openDialog.getFile(); pt(strDir+strFile); } else if(e.getSource() == items[2]){ pt("Save"); saveDialog.setVisible(true); } } }; String[] strs = new String[]{"新建", "打开", "保存"}; for(int i=0; i<items.length; ++i){ items[i] = new MenuItem(strs[i]); items[i].addActionListener(actionListener); menu1.add(items[i]); } Menu menu2 = new Menu("编辑"); menu2.add(new MenuItem("复制")); menu2.add(new MenuItem("剪切")); menu2.add(new MenuItem("粘贴")); menuBar.add(menu1); menuBar.add(menu2); frame.setMenuBar(menuBar); // frame.add(openDialog); // frame.add(saveDialog); } }
时间: 2024-11-09 03:08:26