- AWT(Abstract Window ToolKit)抽象窗口工具包,需要调用本地系统方法实现功能。属于重量级控件。
- GUI(Graphical User Interface)图形用户界面,在AWT的基础上建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现,增强了移植性,属于轻量级控件
- 创建图形化界面:
1,创建Frame窗体。
2,对窗体进行基本设置,如大小,位置,布局。
3,自定义组件。
4,将组件通过窗体的add方法添加到窗体中。
5,通过setVisible(true)显示窗体。
- 事件监听机制流程图:
- consume方法使用事件不按默认方式处理。例如限制输入内容。
- 示例:输入路径,输出目录列表
import java.awt.*; import java.awt.event.*; import java.io.*; public class TextTest { public static void main(String[] args) { MyFrame2 frame = new MyFrame2("文件目录"); frame.setVisible(true); } } class MyFrame2 extends Frame { private TextField tf; private TextArea ta; private Button button; private Dialog dialog; private Label label; private Button ok_button; private Frame frame; MyFrame2(String title) { super(title); this.setBounds(400, 300, 500, 500); this.setLayout(new FlowLayout()); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); frame = this; tf = new TextField("输入目录",20); tf.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_ENTER) { doLikeIWant(); } } }); tf.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e) { tf.setText(""); } }); button = new Button("转到"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { doLikeIWant(); } }); ta = new TextArea(15,40); this.add(tf); this.add(button); this.add(ta); } private void doLikeIWant() { ta.setText(""); String dirPath = tf.getText(); File dir = new File(dirPath); if(dir.exists() && dir.isDirectory()) { String[] names = dir.list(); for(String name : names) { ta.append(name+"\n"); } } else { dialog = new Dialog(frame,"提示信息",true); label = new Label("您输入的目录是:\"" + tf.getText() + "\",输入有误,请重新输入!"); ok_button = new Button("确定"); dialog.setBounds(450, 350, 300, 175); dialog.setLayout(new FlowLayout()); dialog.add(label); dialog.add(ok_button); ok_button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }); dialog.setVisible(true); } } }
- 记事本Demo:
import java.awt.*; import java.awt.event.*; import java.io.*; public class MyMenuDemo { public static void main(String[] args) { MyFrame3 frame = new MyFrame3("NoteBookDemo"); frame.setVisible(true); } } class MyFrame3 extends Frame { private Frame frame; private TextArea ta; private File file; MyFrame3(String title) { super(title); this.setBounds(400, 300, 500, 500); this.setLayout(new FlowLayout()); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); frame = this; MenuBar menu = new MenuBar(); Menu fileMenu = new Menu("File"); MenuItem openItem = new MenuItem("Open"); fileMenu.add(openItem); openItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(frame,"打开文件",FileDialog.LOAD); fd.setVisible(true); String dirPath = fd.getDirectory(); String fileName = fd.getFile(); //System.out.println(dirPath+fileName); if(dirPath == null || fileName == null) return; ta.setText(""); file = new File(dirPath,fileName); try { BufferedReader bufr = new BufferedReader(new FileReader(file)); String line = null; while((line = bufr.readLine())!=null) { ta.append(line+"\n"); } bufr.close(); } catch(IOException we) { throw new RuntimeException("打开文件错误!"); } } }); MenuItem saveItem = new MenuItem("Save"); fileMenu.add(saveItem); saveItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { if(file == null) { FileDialog saveDia = new FileDialog(frame,"保存文件",FileDialog.SAVE); saveDia.setVisible(true); String dirPath = saveDia.getDirectory(); String fileName = saveDia.getFile(); if(dirPath == null || fileName == null) return; file = new File(dirPath,fileName); } try { BufferedWriter bufw = new BufferedWriter(new FileWriter(file)); String text = ta.getText(); bufw.write(text); bufw.close(); } catch(IOException we) { throw new RuntimeException("保存文件错误!"); } } }); MenuItem exitItem = new MenuItem("Exit"); exitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); fileMenu.add(exitItem); menu.add(fileMenu); this.setMenuBar(menu); ta = new TextArea(27,65); this.add(ta,BorderLayout.CENTER); } }
- eclipse导出jar包要新建一个manifest.mf文件,文件中包含Class-Path:,结尾要空一行
javase_GUI
最新推荐文章于 2024-05-14 20:55:34 发布