GUI编程

来源:互联网 发布:lca算法建模 编辑:程序博客网 时间:2024/06/06 17:50
                           GUI编程GUI(图形用户界面)GUIGraphical User Interface(图形用户接口)。用图形的方式,来显示计算机操作的界面,这样更方便更直观。CLICommand line User Interface (命令行用户接口)就是常见的Dos命令行操作。需要记忆一些常用的命令,操作不直观。举例:比如:创建文件夹,或者删除文件夹等awt和swing包的概述java.awt:Abstract Window ToolKit (抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。javax.swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。增强了移植性,属轻量级控件。GUI继承体系图

体验import java.awt.Frame;public class FrameDemo {public static void main(String[] args) {// 创建窗体对象// Frame f = new Frame();// Frame(String title)Frame f = new Frame("林青霞");// 设置窗体标题f.setTitle("HelloWorld");// 设置窗体大小f.setSize(400, 300); // 单位:像素// 设置窗体位置f.setLocation(400, 200);// 调用一个方法,设置让窗体可见// f.show();f.setVisible(true);// System.out.println("helloworld");}}GUI第一个案例Container常用子类:Window Panel(面板,不能单独存在。)Window常用子类:Frame Dialog简单的窗体创建过程:Frame f = new Frame(“my window”);f.setLayout(new FlowLayout());f.setSize(300,400);//设置窗体大小f.setLocation(300,200);//设置窗体出现在屏幕的位置f.setVisible(true);GUI小案例的修改public class FrameDemo2 {public static void main(String[] args) {// 创建对象Frame f = new Frame("方法调用的前后关系");// f.setVisible(true);// try {// Thread.sleep(3000);// } catch (InterruptedException e) {// e.printStackTrace();// }// // f.setSize(400, 300);// // Dimension(int width, int height)// Dimension d = new Dimension(400, 300);// f.setSize(d);// // f.setLocation(400, 200);// // Point(int x, int y)// Point p = new Point(400, 200);// f.setLocation(p);// 一个方法搞定f.setBounds(400, 200, 400, 300);f.setVisible(true);}}事件监听机制:事件监听机制:A:事件源 事件发生的地方B:事件 就是要发生的事情C:事件处理 就是针对发生的事情做出的处理方案D:事件监听 就是把事件源和事件关联起来举例:人受伤事件。事件源:人(具体的对象)Person p1 = new Person("张三");Person p2 = new Person("李四");事件:受伤interface 受伤接口 {一拳();一脚();一板砖();}事件处理:受伤处理类 implements 受伤接口 {一拳(){ System.out.println("鼻子流血了,送到卫生间洗洗"); }一脚(){ System.out.println("晕倒了,送到通风处"); }一板砖(){ System.out.println("头破血流,送到太平间"); }}事件监听:p1.注册监听(受伤接口)public class FrameDemo {public static void main(String[] args) {// 创建窗体对象Frame f = new Frame("窗体关闭案例");// 设置窗体属性f.setBounds(400, 200, 400, 300);// 让窗体关闭//事件源//事件:对窗体的处理//事件处理:关闭窗口(System.exit(0));//事件监听// f.addWindowListener(new WindowListener() {// @Override// public void windowOpened(WindowEvent e) {// }//// @Override// public void windowIconified(WindowEvent e) {// }//// @Override// public void windowDeiconified(WindowEvent e) {// }//// @Override// public void windowDeactivated(WindowEvent e) {// }//// @Override// public void windowClosing(WindowEvent e) {// System.exit(0);// }//// @Override// public void windowClosed(WindowEvent e) {// }//// @Override// public void windowActivated(WindowEvent e) {// }// });//用适配器类改进f.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});// 设置窗体可见f.setVisible(true);}}适配器设计模式/* * 针对用户操作的四种功能 */public interface UserDao {public abstract void add();public abstract void delete();public abstract void update();public abstract void find();}public class UserDaoImpl implements UserDao {@Overridepublic void add() {System.out.println("添加功能");}@Overridepublic void delete() {System.out.println("删除功能");}@Overridepublic void update() {System.out.println("修改功能");}@Overridepublic void find() {System.out.println("查找功能");}}/* * 问题: * 接口(方法比较多) -- 实现类(仅仅使用一个,也得把其他的实现给提供了,哪怕是空实现) * 太麻烦了。 * 解决方案: * 接口(方法比较多) -- 适配器类(实现接口,仅仅空实现) -- 实现类(用哪个重写哪个) */public class UserDaoDemo {public static void main(String[] args) {UserDao ud = new UserDaoImpl();ud.add();// 我没有说我们需要四种功能都实现啊。UserDao ud2 = new UserDaoImpl2();ud2.add();}}public abstract class UserAdapter implements UserDao {@Overridepublic void add() {}@Overridepublic void delete() {}@Overridepublic void update() {}@Overridepublic void find() {}}public class UserDaoImpl2 extends UserAdapter {@Overridepublic void add() {System.out.println("添加功能");}}布局方式/* * 需求:把按钮添加到窗体,并对按钮添加一个点击事件。 * A:创建窗体对象 * B:创建按钮对象 * C:把按钮添加到窗体 * D:窗体显示 */public class FrameDemo {public static void main(String[] args) {// 创建窗体对象Frame f = new Frame("添加按钮");// 设置属性f.setBounds(400, 200, 400, 300);// 设置布局为流式布局f.setLayout(new FlowLayout());// 创建按钮对象Button bu = new Button("点我啊");// bu.setSize(20, 10);// 把按钮添加到窗体f.add(bu);// 设置窗体可以关闭f.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});bu.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("你再点试试");}});// 窗体显示f.setVisible(true);}}鼠标事件案例
public class FrameDemo {public static void main(String[] args) {// 创建窗体对象final Frame f = new Frame("更改背景色");// 设置窗体属性和布局f.setBounds(400, 200, 400, 300);f.setLayout(new FlowLayout());// 创建四个按钮Button redButton = new Button("红色");Button greenButton = new Button("绿色");Button buleButton = new Button("蓝色");// 添加按钮f.add(redButton);f.add(greenButton);f.add(buleButton);// 设置窗体关闭f.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});// 对按钮添加动作事件// redButton.addActionListener(new ActionListener() {// @Override// public void actionPerformed(ActionEvent e) {// f.setBackground(Color.RED);// }// });// 对按钮添加鼠标点击事件// redButton.addMouseListener(new MouseAdapter() {// @Override// public void mouseClicked(MouseEvent e) {// f.setBackground(Color.RED);// }// });// 对按钮添加鼠标的进入事件redButton.addMouseListener(new MouseAdapter() {@Overridepublic void mouseEntered(MouseEvent e) {f.setBackground(Color.RED);}});redButton.addMouseListener(new MouseAdapter() {@Overridepublic void mouseExited(MouseEvent e) {f.setBackground(Color.WHITE);}});greenButton.addMouseListener(new MouseAdapter() {@Overridepublic void mouseEntered(MouseEvent e) {f.setBackground(Color.GREEN);}});greenButton.addMouseListener(new MouseAdapter() {@Overridepublic void mouseExited(MouseEvent e) {f.setBackground(Color.WHITE);}});buleButton.addMouseListener(new MouseAdapter() {@Overridepublic void mouseEntered(MouseEvent e) {f.setBackground(Color.BLUE);}});buleButton.addMouseListener(new MouseAdapter() {@Overridepublic void mouseExited(MouseEvent e) {f.setBackground(Color.WHITE);}});// 设置窗体显示f.setVisible(true);}}案例三/* * 你输入的如果是非数字字符,就取消你键盘录入的效果。 */public class FrameDemo {public static void main(String[] args) {// 创建窗体对象并设置属性Frame f = new Frame("不能输入非数字字符");f.setBounds(400, 200, 400, 300);f.setLayout(new FlowLayout());// 创建Label标签对象Label label = new Label("请输入你的QQ号码,不能是非数字,不信你试试");TextField tf = new TextField(40);// 添加到窗体上f.add(label);f.add(tf);// 设置窗体关闭f.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});// 给文本框添加事件tf.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {// 如果你取得的字符不是数字字符就取消事件// 思路:先获取字符,判断字符,取消事件// char getKeyChar()  char ch = e.getKeyChar();//System.out.println(ch);if(!(ch>='0' && ch<='9')){e.consume();}}});// 设置窗体可见f.setVisible(true);}}菜单/* * 一级菜单 */public class FrameDemo {public static void main(String[] args) {// 创建窗体对象并设置属性Frame f = new Frame("一级菜单");f.setBounds(400, 200, 400, 300);f.setLayout(new FlowLayout());// 创建菜单栏MenuBar mb = new MenuBar();// 创建菜单Menu m = new Menu("文件");// 创建菜单项MenuItem mi = new MenuItem("退出系统");// 谁添加谁呢m.add(mi);mb.add(m);// 设置菜单栏f.setMenuBar(mb);// 设置窗体关闭f.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});mi.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});// 设置窗体可见f.setVisible(true);}}多级菜单/* * 多级菜单 */public class FrameDemo {public static void main(String[] args) {// 创建窗体对象并设置属性final Frame f = new Frame("多级菜单");f.setBounds(400, 200, 400, 300);f.setLayout(new FlowLayout());final String name = f.getTitle();// 创建菜单栏MenuBar mb = new MenuBar();// 创建菜单Menu m1 = new Menu("文件");Menu m2 = new Menu("更改名称");// 创建菜单项final MenuItem mi1 = new MenuItem("好好学习");final MenuItem mi2 = new MenuItem("天天向上");MenuItem mi3 = new MenuItem("恢复标题");MenuItem mi4 = new MenuItem("打开记事本");MenuItem mi5 = new MenuItem("退出系统");// 谁添加谁呢m2.add(mi1);m2.add(mi2);m2.add(mi3);m1.add(m2);m1.add(mi4);m1.add(mi5);mb.add(m1);// 设置菜单栏f.setMenuBar(mb);// 设置窗体关闭f.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});mi1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {f.setTitle(mi1.getLabel());}});mi2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {f.setTitle(mi2.getLabel());}});mi3.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {f.setTitle(name);}});mi4.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Runtime r = Runtime.getRuntime();try {r.exec("notepad");} catch (IOException e1) {e1.printStackTrace();}}});mi5.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});// 设置窗体可见f.setVisible(true);}}做一个文件的打开读取和保存import java.awt.FileDialog;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;public class MyTextEditor extends JFrame implements ActionListener{private static final long serialVersionUID = 6351611109227931816L;public static void main(String[] args) {new MyTextEditor();}//文本域private JTextArea taContent;private JButton btnSave ;private JButton btnOpen ;public MyTextEditor(){init();this.setVisible(true);}/** * 初始化窗口 */private void init() {this.setBounds(100, 50, 800, 600);this.setLayout(null);//textJScrollPane panel = new JScrollPane();panel.setLayout(null);panel.setBounds(10, 10, 800, 350);panel.setAutoscrolls(true);panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);this.add(panel);taContent = new JTextArea();taContent.setBounds(0, 0, 750, 350);panel.add(taContent);btnSave = new JButton("保存");btnSave.setBounds(100, 450, 100, 50);btnSave.addActionListener(this);this.add(btnSave);btnOpen = new JButton("打开");btnOpen.setBounds(500, 450, 100, 50);btnOpen.addActionListener(this);this.add(btnOpen);//this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(-1);}});}public void actionPerformed(ActionEvent e) {Object s= e.getSource();if(s == btnSave){try {FileWriter w = new FileWriter("d:/111.txt");w.write(taContent.getText());w.close();taContent.setText("");} catch (Exception e2) {// TODO: handle exception}}else if(s == btnOpen){//System.out.println("open");try {FileDialog  fd = new FileDialog(this, "打开文件");fd.setVisible(true);//File f = new File(fd.getDirectory(), fd.getFile());FileReader reader = new FileReader(f);char[] buf = new char[1024];int len = 0 ;while((len = reader.read(buf)) != -1){String ss = new String(buf,0,len);taContent.append(ss);}} catch (Exception e2) {e2.printStackTrace();}}}}代码看效果public class MyWindow {public static void main(String[] args) {JFrame f = new JFrame();//设置大小//f.setSize(800, 600);//位置//f.setLocation(100, 50);//f.setBounds(100, 50, 800, 600);//f.setLayout(null);//创建按钮JButton btnOK = new JButton("OK");btnOK.setBounds(100, 50, 100, 50);btnOK.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.out.println("点击了按钮");}});//在frame中添加按钮f.add(btnOK);//添加创建监听f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(-1);}});JMenuBar bar = new JMenuBar();JMenu menu = new JMenu("文件");JMenuItem mi = new JMenuItem("打开");menu.add(mi);bar.add(menu);//f.setJMenuBar(bar);//f.setVisible(true);}}

0 0
原创粉丝点击