需求分析 网站,nodejs同时做网站和后台管理,wordpress系统,免费设计签名在线生成GUI#xff08;Graphical User Interface#xff0c;简称 GUI#xff0c;图形用户界面#xff09;是指采用图形方式显示的计算机操作用户界面#xff0c;与早期计算机使用的命令行界面相比#xff0c;图形界面对于用户来说在视觉上更易于接受。Java GUI主要有两个核心库Graphical User Interface简称 GUI图形用户界面是指采用图形方式显示的计算机操作用户界面与早期计算机使用的命令行界面相比图形界面对于用户来说在视觉上更易于接受。Java GUI主要有两个核心库分别是AWTjava.awtAbstract Windows ToolKit(抽象窗口工具包)和Swingjavax.swingAWT的扩展AWT需要调用本地系统方法来实现功能属重量级控件Swing是在AWT的基础上建立的一套图像界面系统其中提供了更多的组件而且完全由Java实现增强了移植性属轻量级组件。AWT:1.包含了很多类和接口2.元素窗口、核心类组件Component可以直接用的基本组件button、TextArea、Label容器有窗口Window【分为Frame和Dialog】和面板Panel【Applet】基本组件存放在容器中用add操作窗口frameFrame类用于创建一个具有标题栏的框架窗口作为程序的主要界面它不依赖其他容器可以单独存在。运行后可以看到一个弹窗 public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setSize(400,400);//设置背景颜色frame.setBackground(new Color(145, 36, 44));//设置弹出的初始位置 0,0 在左上角frame.setLocation(0,0);//设置窗口是否可拉伸frame.setResizable(false);}面板Panel特性面板不能单独存在必须在容器里面可以看成一个空间。在程序中通常会使面板来实现一些特殊的布局。 public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setSize(400,400);//设置背景颜色frame.setBackground(new Color(145, 36, 44));//设置弹出的初始位置 0,0 在左上角frame.setLocation(0,0);//设置窗口是否可拉伸frame.setResizable(false);//设置布局frame.setLayout(null);//pannel面板Panel panel new Panel();panel.setBounds(0,0,100,400);panel.setBackground(new Color(83, 169, 56));frame.add(panel);}布局管理器LayOut流式布局默认FlowLayout流式布局管理器按水平方向依次排列放置组件排满一行换下一行继续排列。排列方向左到右 或 右到左取决于容器的componentOrientation属性该属性属于Component它可能的值如下:ComponentOrientation.LEFT_TO_RIGHT默认ComponentOrientation.RIGHT_TO_LEFT public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setBounds(0,0,400,400);//文本框组件TextField num1 new TextField();TextField num2 new TextField();TextField num3 new TextField();//一个按钮Button button new Button();//一个标签Label label new Label();//设置流式布局FlowLayout flowLayout new FlowLayout();frame.setLayout(flowLayout);frame.add(num1);frame.add(label);frame.add(num2);frame.add(button);frame.add(num3);}东西南北中 public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setSize(400,400);//设置背景颜色frame.setBackground(new Color(145, 36, 44));//设置弹出的初始位置 0,0 在左上角frame.setLocation(0,0);//设置窗口是否可拉伸frame.setResizable(false);//按钮组件Button east new Button(east);Button west new Button(west);Button south new Button(south);Button north new Button(north);Button center new Button(center);frame.add(east,BorderLayout.EAST);frame.add(west,BorderLayout.WEST);frame.add(south,BorderLayout.SOUTH);frame.add(north,BorderLayout.NORTH);frame.add(center,BorderLayout.CENTER);}网格布局GridLayout网格布局管理器它以矩形网格形式对容器的组件进行布置把容器按行列分成大小相等的矩形网格一个网格中放置一个组件组件宽高自动撑满网格。 public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setSize(400,400);//设置背景颜色frame.setBackground(new Color(145, 36, 44));//设置弹出的初始位置 0,0 在左上角frame.setLocation(0,0);//设置窗口是否可拉伸frame.setResizable(false);//按钮组件Button button1 new Button(button1);Button button2 new Button(button2);Button button3 new Button(button3);Button button4 new Button(button4);Button button5 new Button(button5);Button button6 new Button(button6);//设置表格3行2列GridLayout gridLayout new GridLayout(3, 2);//设置布局frame.setLayout(gridLayout);frame.add(button1);frame.add(button2);frame.add(button3);frame.add(button4);frame.add(button5);frame.add(button6);}事件监听关闭窗口 //监听窗口关闭事件frame.addWindowListener(new WindowAdapter() {//适配器模式Overridepublic void windowClosing(WindowEvent e) {//结束程序System.exit(0);}});按钮点击事件public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setSize(400,400);//设置背景颜色frame.setBackground(new Color(145, 36, 44));//设置弹出的初始位置 0,0 在左上角frame.setLocation(0,0);//设置窗口是否可拉伸frame.setResizable(false);//按钮组件Button button1 new Button(button1);//显示设置按钮上的信息button1.setActionCommand(button1-11);button1.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {System.out.println(eee);System.out.println(e.getActionCommand());}});frame.add(button1,BorderLayout.CENTER);
}点击按钮后控制台打印。setActionCommand是为了在多个按钮的监听写在一个监听器用if判断文本框回车事件 public static void main(String[] args) {
//frame窗口
Frame frame new Frame(java图形化界面窗口);
//设置可见性 与 窗口大小
frame.setVisible(true);
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(145, 36, 44));
//设置弹出的初始位置 0,0 在左上角
frame.setLocation(0,0);
//设置窗口是否可拉伸
frame.setResizable(false);
//文本框组件
TextField textField new TextField();
//按下回车键会触发这个监听
textField.addActionListener(new ActionListener() {
Override
public void actionPerformed(ActionEvent e) {
//获取资源
TextField field (TextField) e.getSource();
//打印文本框内容System.out.println(field.getText());
}
});
frame.add(textField,BorderLayout.CENTER);
}鼠标监听监听鼠标按压、鼠标释放、鼠标点击 public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setBounds(0,0,400,400);frame.addMouseListener(new MouseAdapter() {Overridepublic void mouseEntered(MouseEvent e) {System.out.println(鼠标进入组件区域);}Overridepublic void mouseExited(MouseEvent e) {System.out.println(鼠标离开组件区域);}Overridepublic void mousePressed(MouseEvent e) {// 获取按下的坐标相对于组件System.out.println(相对组件 e.getPoint() 横坐标 e.getX() , 纵坐标 e.getY());// 获取按下的坐标相对于屏幕System.out.println(相对屏幕 e.getLocationOnScreen() 横坐标 e.getXOnScreen() , 纵坐标 e.getYOnScreen());}Overridepublic void mouseReleased(MouseEvent e) {System.out.println(鼠标释放);}Overridepublic void mouseClicked(MouseEvent e) {// 鼠标在组件区域内按下并释放中间没有移动光标才识别为被点击System.out.println(鼠标点击);}});}鼠标拖动、鼠标移动、鼠标滚轮滚动 public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setBounds(0,0,400,400);/*** 鼠标移动/拖动监听器*/frame.addMouseMotionListener(new MouseMotionAdapter() {Overridepublic void mouseDragged(MouseEvent e) {// 鼠标保持按下状态移动即为拖动System.out.println(鼠标拖动);}Overridepublic void mouseMoved(MouseEvent e) {System.out.println(鼠标移动);}});/*** 鼠标滚轮监听器*/frame.addMouseWheelListener(new MouseWheelListener() {Overridepublic void mouseWheelMoved(MouseWheelEvent e) {// e.getWheelRotation() 为滚轮滚动多少的度量System.out.println(mouseWheelMoved: e.getWheelRotation());}});}键盘监听上下左右键和空格键KeyEvent.VK_UPpublic static final int VK_LEFT 0x25;public static final int VK_UP 0x26;public static final int VK_RIGHT 0x27;public static final int VK_DOWN 0x28;public static final int VK_SPACE 0x20; public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setBounds(0,0,400,400);/*** 键盘监听事件*/frame.addKeyListener(new KeyAdapter() {Overridepublic void keyPressed(KeyEvent e) {// 获取键值和 KeyEvent.VK_XXXX 常量比较确定所按下的按键System.out.println(按下: e.getKeyChar() 键值为 e.getKeyCode());}Overridepublic void keyTyped(KeyEvent e) {// e.getKeyChar() 获取键入的字符System.out.println(键入: e.getKeyChar());}Overridepublic void keyReleased(KeyEvent e) {System.out.println(释放: e.getKeyCode());}});}窗口监听 public static void main(String[] args) {//frame窗口Frame frame new Frame(java图形化界面窗口);//设置可见性 与 窗口大小frame.setVisible(true);frame.setBounds(0, 0, 400, 400);/*** 窗口监听事件*/frame.addWindowListener(new WindowAdapter() {Overridepublic void windowClosing(WindowEvent e) {System.out.println(窗口被关闭了);System.exit(0);}Overridepublic void windowActivated(WindowEvent e) {System.out.println(窗口被激活了);}});}画笔Paintpublic class MyPaint extends Frame {public void loadFrame(){setBounds(0,0,600,400);setVisible(true);}Overridepublic void paint(Graphics g) {//设置画笔的颜色g.setColor(Color.RED);//设置画一个圆圈g.drawOval(50,100,50,50);//画实心圆g.fillOval(100,100,50,50);//重新设置画笔的颜色g.setColor(Color.GREEN);//画实心矩形g.fillRect(150,100,100,50);}
}在别的类中启动 public static void main(String[] args) {new MyPaint().loadFrame();}SwingJframe