GUI01

 

GUI编程

组件

  • 窗口

  • 鼠标

  • 面板

  • 文本框

  • 列表框

  • 按钮

  • 图片

  • 监听事件

1,简介

GUI技术核心:Swing AWT

1,界面不美观

2,需要jre环境

AWT

AWT1

窗口

import java.awt.*;

public class test1 {
   public static void main(String[] args) {
       new MyFrame(100,100,200,200,Color.red);
       new MyFrame(100,300,200,200,Color.red);
       new MyFrame(300,100,200,200,Color.red);
       new MyFrame(300,300,200,200,Color.red);
  }

   static class MyFrame extends Frame{
       static int id = 0;//可能需要多个窗口

       public MyFrame(int x,int y,int w,int h,Color color){
           super("Myframe"+(++id));
           setBounds(x,y,w,h);
           setBackground(color);
           setVisible(true);
      }
  }
}

面板Panel


import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

//面板Panel
public class Test2 {
   public static void main(String[] args) {
       //实例化一个窗口Frame
       Frame frame = new Frame("阿托");
       //实例化一个面板Panel
       Panel panel = new Panel();
       //设置布局
       frame.setLayout(null);

       frame.add(panel);
       //初始画窗口Frame
       frame.setVisible(true);
       //设置属性
       frame.setBackground(new Color(1,1,1));
       frame.setBounds(100,100,400,400);
       //初始化面板Panel
       //设置属性
       panel.setBackground(new Color(91, 135, 23));
       panel.setBounds(10,10,300,300);
       //关闭
       //适配器模式适配
       frame.addWindowListener(new WindowAdapter() {
           /**
            * Invoked when a window is in the process of being closed.
            * The close operation can be overridden at this point.
            *
            * @param e
            */
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
          }
      });


  }
}

布局器管理

  • 流式布局


import java.awt.*;

public class Test {
   public static void main(String[] args) {
       Frame frame = new Frame("阿托");
       //组件-按钮
       Button button1 = new Button("button1");
       Button button2 = new Button("button2");
       Button button3 = new Button("button3");
       //设置流式布局
       frame.setLayout(new FlowLayout());

       //将按钮添加上去
       frame.add(button1);
       frame.add(button2);
       frame.add(button3);
       frame.setSize(300,200);
       frame.setVisible(true);
  }
 
}

 

  • 东西南北中布局


import java.awt.*;

public class Test3 {
   public static void main(String[] args) {
       Frame frame = new Frame();
       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);
       frame.setVisible(true);
       frame.setBounds(100,100,500,500);
  }
}

 

  • 表格式布局


mport java.awt.*;

public class TestGridlayless {
   public static void main(String[] args) {
       Frame frame = new Frame();
       Button button1 = new Button("01");
       Button button2 = new Button("02");
       Button button3 = new Button("03");
       Button button4 = new Button("04");
       Button button5 = new Button("05");
       frame.setLayout(new GridLayout(2,3));
       frame.add(button1);
       frame.add(button2);
       frame.add(button3);
       frame.add(button4);
       frame.add(button5);
       frame.setVisible(true);
       frame.setSize(400,500);
  }
}

事件监听


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test6 {
   public static void main(String[] args) {
       //按一个按钮然后发生一些事情
       Frame frame = new Frame();
       Button button = new Button();
       MyAction action = new MyAction();
       button.addActionListener(action);
       frame.add(button);
       frame.pack();
       windowsClose(frame);
       frame.setVisible(true);
  }
   private static void windowsClose(Frame frame){
       frame.addWindowListener(new WindowAdapter() {
           /**
            * Invoked when a window is in the process of being closed.
            * The close operation can be overridden at this point.
            *
            * @param e
            */
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
          }
      });
  }
}

class MyAction implements ActionListener{

   /**
    * Invoked when an action occurs.
    *
    * @param e
    */
   @Override
   public void actionPerformed(ActionEvent e) {
       System.out.println("Hi");
  }
}

多个按钮实现统一个事件


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test7 {
   public static void main(String[] args) {
       //多个按钮共享一个实现
       Frame frame = new Frame();
       Button button1 = new Button("start");
       Button button2 = new Button("stop");
       MyActiontwo myAction = new MyActiontwo();
       button1.addActionListener(myAction);
       button2.addActionListener(myAction);
       frame.setLayout(new BorderLayout());
       frame.add(button1,BorderLayout.NORTH);
       frame.add(button2,BorderLayout.SOUTH);
       frame.setVisible(true);
       frame.pack();
  }
}

class MyActiontwo implements ActionListener{

   /**
    * Invoked when an action occurs.
    *
    * @param e
    */
   @Override
   public void actionPerformed(ActionEvent e) {
       System.out.println(""+e.getActionCommand());
  }
}

文本框事件监听


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test8 {
   public static void main(String[] args) {
       //输入框事件
       new MyFrame();

  }
}

class MyFrame extends Frame {
   public MyFrame(){
       TextField textField = new TextField();
       Frame frame = new Frame();
       frame.add(textField);
       MyAction2 myAction2 = new MyAction2();
       textField.addActionListener(myAction2);
       textField.setEchoChar('*');
       frame.setVisible(true);
       frame.pack();

  }
}

class MyAction2 implements ActionListener{

   /**
    * Invoked when an action occurs.
    *
    * @param e
    */
   @Override
   public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();
       System.out.println(textField.getText());
       textField.setText(null);
  }
}

简易计算机


package com.atuo.test1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalulator {
   public static void main(String[] args) {
       new Calulator();
  }
}
class Calulator extends Frame{
   /**
    * Constructs a new instance of <code>Frame</code> that is
    * initially invisible. The title of the <code>Frame</code>
    * is empty.
    *
    * @throws HeadlessException when
    *                           <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
    * @see GraphicsEnvironment#isHeadless()
    * @see Component#setSize
    * @see Component#setVisible(boolean)
    */
   public Calulator(){
       //3个文本框
       TextField num1 = new TextField(10);
       TextField num2 = new TextField(10);
       TextField num3 = new TextField(10);
       //1个标签
       Label label = new Label("+");
       //1个按钮
       Button button = new Button("=");
       button.addActionListener(new MyCalulatorListener(num1,num2,num3));
       setLayout(new FlowLayout());
       add(num1);
       add(label);
       add(num2);
       add(button);
       add(num3);
       pack();
       setVisible(true);
  }
}
class MyCalulatorListener implements ActionListener{
   private TextField num1,num2,num3;

   public MyCalulatorListener(TextField num1,TextField num2,TextField num3){
       this.num1 = num1;
       this.num2 = num2;
       this.num3 = num3;
  }
   /**
    * Invoked when an action occurs.
    *
    * @param e
    */

   @Override
   public void actionPerformed(ActionEvent e) {
       //获取n1,n2
       int n1 = Integer.parseInt(num1.getText());
       int n2 = Integer.parseInt(num2.getText());

       //获取n3
       num3.setText(""+(n1+n2));

       //n1,n2
       num1.setText("");
       num2.setText("");
  }
}

优化


package com.atuo.test1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalulator {
   public static void main(String[] args) {
       new Calulator().loadFrame();
  }
}
class Calulator extends Frame{
   TextField num1;
   TextField num2;
   TextField num3;
   /**
    * Constructs a new instance of <code>Frame</code> that is
    * initially invisible. The title of the <code>Frame</code>
    * is empty.
    *
    * @throws HeadlessException when
    *                           <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
    * @see GraphicsEnvironment#isHeadless()
    * @see Component#setSize
    * @see Component#setVisible(boolean)
    */
   public void loadFrame(){
       //3个文本框
       num1 = new TextField(10);
       num2 = new TextField(10);
       num3 = new TextField(10);
       //1个标签
       Label label = new Label("+");
       //1个按钮
       Button button = new Button("=");
       button.addActionListener(new MyCalulatorListener());
       setLayout(new FlowLayout());
       add(num1);
       add(label);
       add(num2);
       add(button);
       add(num3);
       pack();
       setVisible(true);
  }
   private class MyCalulatorListener implements ActionListener {
       /**
        * Invoked when an action occurs.
        *
        * @param e
        */

       @Override
       public void actionPerformed(ActionEvent e) {
           //获取n1,n2
           int n1 = Integer.parseInt(num1.getText());
           int n2 = Integer.parseInt(num2.getText());

           //获取n3
           num3.setText(""+(n1+n2));

           //n1,n2
           num1.setText("");
           num2.setText("");
      }
  }
}

画笔


package com.atuo.test1;

import java.awt.*;

public class TestPaint {
   public static void main(String[] args) {
       new MyPaint().loadFrame();
  }
}
class MyPaint extends Frame{

   public void loadFrame(){
       setVisible(true);
       setBounds(200,200,600,400);
  }
   @Override
   public void paint(Graphics g) {
       g.setColor(Color.red);
       g.drawOval(100,100,200,200);
       g.setColor(Color.GREEN);
       g.fillRect(300,300,100,200);
       g.setColor(Color.BLACK);
  }
}

鼠标监听


package com.atuo.test1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;

public class TestPaintListener {
   public static void main(String[] args) {
       new MyFrame2("hi");
  }
}
class MyFrame2 extends Frame{
   ArrayList points;

   /**
    * Constructs a new instance of <code>Frame</code> that is
    * initially invisible. The title of the <code>Frame</code>
    * is empty.
    *
    * @throws HeadlessException when
    *                           <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
    * @see GraphicsEnvironment#isHeadless()
    * @see Component#setSize
    * @see Component#setVisible(boolean)
    */
   public MyFrame2(String title){
       super(title);
       setBounds(200,200,600,400);
       setVisible(true);
       points = new ArrayList<>();
       this.addMouseListener(new MyMouseListener());
  }

   @Override
   public void paint(Graphics g) {
       Iterator iterator = points.iterator();
       while (iterator.hasNext()){
           Point point = (Point) iterator.next();
           g.setColor(Color.blue);
           g.fillOval(point.x,point.y,10,10);
      }

  }

   private void addPaint(Point point){
       points.add(point);
  }



   private class MyMouseListener extends MouseAdapter {
       /**
        * {@inheritDoc}
        *
        * @param e
        */
       @Override
       public void mousePressed(MouseEvent e) {
           MyFrame2 frame = (MyFrame2) e.getSource();
           frame.addPaint(new Point(e.getX(),e.getY()));
           frame.repaint();
      }

  }
}

键盘监听


package com.atuo.test1;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyListener {
   public static void main(String[] args) {
       new MyFrame3();
  }
   static class MyFrame3 extends Frame{
       /**
        * Constructs a new instance of <code>Frame</code> that is
        * initially invisible. The title of the <code>Frame</code>
        * is empty.
        *
        * @throws HeadlessException when
        *                           <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
        * @see GraphicsEnvironment#isHeadless()
        * @see Component#setSize
        * @see Component#setVisible(boolean)
        */
       public MyFrame3() {
           setBackground(Color.blue);
           setBounds(1,1,400,400);
           setVisible(true);
           addKeyListener(new KeyAdapter() {
               /**
                * Invoked when a key has been pressed.
                *
                * @param e
                */
               @Override
               public void keyPressed(KeyEvent e) {
                   int code = e.getKeyCode();
                   if (code == KeyEvent.VK_A){
                       System.out.println("a");
                  }
              }
          });
      }
  }
}
package com.atuo.test1;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyListener {
   public static void main(String[] args) {
       new MyFrame3();
  }
   static class MyFrame3 extends Frame{
       /**
        * Constructs a new instance of <code>Frame</code> that is
        * initially invisible. The title of the <code>Frame</code>
        * is empty.
        *
        * @throws HeadlessException when
        *                           <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
        * @see GraphicsEnvironment#isHeadless()
        * @see Component#setSize
        * @see Component#setVisible(boolean)
        */
       public MyFrame3() {
           setBackground(Color.blue);
           setBounds(1,1,400,400);
           setVisible(true);
           addKeyListener(new KeyAdapter() {
               /**
                * Invoked when a key has been pressed.
                *
                * @param e
                */
               @Override
               public void keyPressed(KeyEvent e) {
                   int code = e.getKeyCode();
                   if (code == KeyEvent.VK_A){
                       System.out.println("a");
                  }
              }
          });
      }
  }
}

Swing

窗口,面板


package com.atuo.test1;

import javax.swing.*;
import java.awt.*;

public class TestJFrame {
   public static void main(String[] args) {
       new MyJFrame().init();
  }
}
class MyJFrame extends JFrame{
   public void init(){
       setBounds(1,1,500,500);
       Container contentPane = getContentPane();
       contentPane.setBackground(Color.blue);
       JLabel jl = new JLabel("哈哈哈");
       jl.setHorizontalAlignment(SwingConstants.CENTER);
       add(jl);
       setVisible(true);
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
}

弹窗


package com.atuo.test1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestDialog {
   public static void main(String[] args) {
       new MyJFrame5();
  }
}
class MyJFrame5 extends JFrame{
   /**
    * Constructs a new frame that is initially invisible.
    * <p>
    * This constructor sets the component's locale property to the value
    * returned by <code>JComponent.getDefaultLocale</code>.
    *
    * @throws HeadlessException if GraphicsEnvironment.isHeadless()
    *                           returns true.
    * @see GraphicsEnvironment#isHeadless
    * @see Component#setSize
    * @see Component#setVisible
    * @see JComponent#getDefaultLocale
    */
   public MyJFrame5() {
       setBounds(1,1,500,500);
       setVisible(true);
       setLayout(null);
       Container cont = getContentPane();
       cont.setBackground(Color.blue);
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       JButton button = new JButton("HIHIHI");
       button.setBounds(10,10,200,50);
       button.setVisible(true);
       button.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               new MyDialog();
          }
      });
       add(button);

  }

   private class MyDialog extends JDialog{
       /**
        * Creates a modeless dialog without a title and without a specified
        * {@code Frame} owner. A shared, hidden frame will be
        * set as the owner of the dialog.
        * <p>
        * This constructor sets the component's locale property to the value
        * returned by {@code JComponent.getDefaultLocale}.
        * <p>
        * NOTE: This constructor does not allow you to create an unowned
        * {@code JDialog}. To create an unowned {@code JDialog}
        * you must use either the {@code JDialog(Window)} or
        * {@code JDialog(Dialog)} constructor with an argument of
        * {@code null}.
        *
        * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}
        *                           returns {@code true}.
        * @see GraphicsEnvironment#isHeadless
        * @see JComponent#getDefaultLocale
        */
       public MyDialog() {
           Container container = this.getContentPane();
           container.setBackground(Color.blue);
           this.setBounds(10,10,500,200);
           this.setVisible(true);
           this.setLayout(null);
      }
  }
}

图标


package com.atuo.test1;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class Demo2 extends JFrame {
   public static void main(String[] args) {
       new Demo2();
  }
   public Demo2(){
       URL url = Demo2.class.getResource("tx.png");
       ImageIcon icon = new ImageIcon(url);
       JLabel jl = new JLabel("imageIcon");
       jl.setIcon(icon);
       Container contentPane = getContentPane();
       contentPane.add(jl);
       setVisible(true);
       pack();
  }
}

面板


package com.atuo.test1;

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {
   public JPanelDemo(){
       Container container = getContentPane();
       container.setLayout(new GridLayout(2,1,10,10));
       JPanel jPanel1 = new JPanel(new GridLayout(2, 1));
       JPanel jPanel2 = new JPanel(new GridLayout(2, 1));
       JPanel jPanel3 = new JPanel(new GridLayout(2, 2));
       jPanel1.add(new Button("1"));
       jPanel1.add(new Button("1"));
       jPanel2.add(new Button("1"));
       jPanel2.add(new Button("1"));
       jPanel2.add(new Button("1"));
       jPanel2.add(new Button("1"));
       jPanel3.add(new Button("1"));
       jPanel3.add(new Button("1"));
       jPanel3.add(new Button("1"));
       jPanel3.add(new Button("1"));
       this.add(jPanel1);
       this.add(jPanel2);
       this.add(jPanel3);
       setVisible(true);
       pack();
  }

   public static void main(String[] args) {
       new JPanelDemo();
  }
}

按钮


package com.atuo.test1;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtenDemo extends JFrame {
   /**
    * Constructs a new frame that is initially invisible.
    * <p>
    * This constructor sets the component's locale property to the value
    * returned by <code>JComponent.getDefaultLocale</code>.
    *
    * @throws HeadlessException if GraphicsEnvironment.isHeadless()
    *                           returns true.
    * @see GraphicsEnvironment#isHeadless
    * @see Component#setSize
    * @see Component#setVisible
    * @see JComponent#getDefaultLocale
    */
   public JButtenDemo(){
       Container container = getContentPane();
       URL url = JButtenDemo.class.getResource("tx.png");
       Icon icon = new ImageIcon(url);
       JButton button = new JButton();
       button.setIcon(icon);
       container.add(button);
       setVisible(true);
       pack();
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

   public static void main(String[] args) {
       new JButtenDemo();
  }
}

 

posted @ 2022-03-23 19:47  亚托克斯的泯灭  阅读(22)  评论(0)    收藏  举报