zoukankan      html  css  js  c++  java
  • GUI编程练习

    GUI编程

    告诉大家该怎么学? ●这是什么? ●它怎么玩? ●该如何去在我们平时运用? 组件 ●窗口 ●弹窗 ●面板 ●文本框 ●列表框 ●按钮

    ●图片

    ●监听事件 ●鼠标 ●键盘事件 ●破解工具

    1、简介

    Gui的核心技术: Swing AWT 1.因为界面不美观。 2.需要jre环境! 为什么我们要学习? 1.可以写出自己心中想要的一-些小工具 2.工作时候,也可能需要维护到swing界面,概率极小! 3.了解MVC架构,了解监听!

    2、AWT

    2.1、Awt介绍

    1.包含了很多类和接口! GUI! 2.元素:窗口,按钮,文本框

    3.java.awt

    image-20200819120548115

    2.2、组件和容器

    1、Frame 弹窗
    package com.shao.gui;

    import java.awt.*;
    //GUI的第一个界面
    public class TestFrame {
       public static void main(String[] args) {
           //Frame,JDK,看源码
           Frame frame = new Frame("我的第一次学习JAVA图像界面窗口");

           //需要设置可见性 w h
           frame.setVisible(true);

           //设置窗口大小
           frame.setSize(400,400);

           //设置背景颜色,IDEA中,颜色可以拖动选择
           frame.setBackground( new Color(82, 155, 60));

           //弹出的初始位置
           frame.setLocation(200,200);

           //设置大小固定
           frame.setResizable(false);
      }
    }

     

    image-20200819212848829

    窗口此时无法关闭,停止java程序

    尝试回顾封装:

    package com.shao.gui;

    import java.awt.*;

    public class TestFrame2 {
       public static void main(String[] args) {
           //展示多个窗口
           MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.blue);
           MyFrame myFrame2 = new MyFrame(100,300,200,200,Color.red);
           MyFrame myFrame3 = new MyFrame(300,100,200,200,Color.pink);
           MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.green);


      }
    }


    class MyFrame extends Frame{
           static int id = 0;//可能存在多个窗口,所以我们需要一个计数器

           //构造器
           public MyFrame(int x,int y,int w,int h,Color color){
               super("MyFrame"+(++id));//继承父类
               setBackground(color);
               setBounds(x,y,w,h);//直接设置坐标和长宽高
               setVisible(true);//设置可见性
          }
    }

     

    image-20200820012224971

     

    2、Panel 面板 ,内嵌在frame上
    package com.shao.gui;



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

    //Panek 可以看作是一个空间,但是不能单独存在
    public class TestPanel {
       public static void main(String[] args) {
           Frame frame = new Frame();
           //布局的概念
           Panel panel = new Panel();

           //设置布局
           frame.setLayout(null);

           //坐标
           frame.setBounds(300,300,500,500);
           frame.setBackground(new Color(89, 191, 43));


           panel.setBounds(100,100,300,300);
           panel.setBackground(new Color(191, 107, 55));

           frame.add(panel);

           frame.setVisible(true);

           //监听时间,监听窗口关闭事件,System.exit(0)
           //适配器模式
           frame.addWindowListener(new WindowAdapter() {  //WindowAdapter 继承 WindowsListenner 接口

               //窗口点击关闭时要做的事
               @Override
               public void windowClosing(WindowEvent e) {
                  System.exit(0);
              }
          });

      }
    }


    //此时窗口可关闭************************************************

     

    2.3、布局管理器

    ●流式布局
    package com.shao.gui;

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

    public class TestFlowLayout {
       public static void main(String[] args) {
           Frame frame = new Frame();
           Button button1 = new Button("table1");
           Button button2 = new Button("table2");
           Button button3 = new Button("table3");

           //设置为流式布局
           frame.setLayout(new FlowLayout(FlowLayout.CENTER));//CENTER 中心,可以选择其他

           frame.setSize(200,200);

           frame.add(button1);
           frame.add(button2);
           frame.add(button3);

           frame.setVisible(true);

           frame.addWindowListener(new WindowAdapter() {
               @Override
               public void windowClosing(WindowEvent e) {
                   System.exit(0);
              }
          });

      }
    }

    image-20200820124527701

     

     

    ●东西南北中
    package com.shao.gui;

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

    public class TestFlowLayout2 {
      public static void main(String[] args) {
          Button button1 = new Button("east");
          Button button2 = new Button("west");
          Button button3 = new Button("south");
          Button button4 = new Button("nouth");
          Button button5 = new Button("center");

          Frame frame = new Frame();
          frame.add(button1,BorderLayout.EAST);
          frame.add(button2,BorderLayout.WEST);
          frame.add(button3,BorderLayout.SOUTH);
          frame.add(button4,BorderLayout.NORTH);
          frame.add(button5,BorderLayout.CENTER);

          frame.setSize(400,400);
          frame.setVisible(true);

          frame.addWindowListener(new WindowAdapter() {
              @Override
              public void windowClosing(WindowEvent e) {
                  System.exit(0);
              }
          });

      }
    }

    image-20200820180224213

     

     

     

    ●表格
    package com.shao.gui;

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

    public class TestGridLayout {
       public static void main(String[] args) {
           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");

           Frame frame = new Frame("TestGridLayout");
           frame.setLayout(new GridLayout(3,2,10,20));//参数是,行,列,行间距,列间距,四个参数

           frame.add(button1);
           frame.add(button2);
           frame.add(button3);
           frame.add(button4);
           frame.add(button5);
           frame.add(button6);

           frame.pack();//java函数,用来自动布局,用最优布局
           frame.setVisible(true);

           frame.addWindowListener(new WindowAdapter() {
               @Override
               public void windowClosing(WindowEvent e) {
                   System.exit(0);
              }
          });

      }
    }

     

    image-20200820200904956

     

    综合应用

    image-20200820234907477

    代码实现

    package com.shao.gui;

    import com.shao.duixiang.Person;

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

    public class TestLesson1 {
       public static void main(String[] args) {

           Frame frame = new Frame();
           frame.setSize(400,400);
           frame.setBackground(new Color(46, 125, 223));
           frame.setLocation(300,300);
           frame.setVisible(true);
           frame.setLayout(new GridLayout(2,1));

           //4个面板
           Panel panel1 = new Panel(new BorderLayout());
           Panel panel2 = new Panel(new GridLayout(1,1));
           Panel panel3 = new Panel(new GridLayout(2,2));
           Panel panel4 = new Panel(new BorderLayout());

           //上面
           panel1.add(new Button("Easrt-1"),BorderLayout.EAST);
           panel1.add(new Button("West-1"),BorderLayout.WEST);
           panel2.add(new Button("p2-btn-1"));
           panel2.add(new Button("p2-btn-2"));
           panel1.add(panel2,BorderLayout.CENTER);

           //下面
           panel4.add(new Button("East-2"),BorderLayout.EAST);
           panel4.add(new Button("East-2"),BorderLayout.WEST);

           //下面中间四个
           for (int i = 0; i <4 ; i++) {
               panel3.add(new Button("square"+i));
          }
           panel4.add(panel3,BorderLayout.CENTER);

           frame.add(panel1);
           frame.add(panel4);

           frame.addWindowListener(new WindowAdapter() {
               @Override
               public void windowClosing(WindowEvent e) {
                   System.exit(0);
              }
          });
      }
    }

     

    image-20200820234641549

    总结:

    1. Frame是一个顶级窗口

    2. Panel无法单独显示,必须添加到某个容器中。 3.布局管理器 1.流式 2.东西南北中 3.表格 4.大小,定位,背景颜色,可见性,监听!


    2.4、事件监听

    事件监听:当某个事情发生的时候,干什么?

    package com.shao.gui;

    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 TestLesson2 {
       public static void main(String[] args) {
           //按下按钮,触发一些事件
           Frame frame = new Frame();
           Button button = new Button("start");
           //因为。addActionListener()需要一个ActionListener,所以我们需要构造一个Actionalistener
           MyActionLintener myActionLintener = new MyActionLintener();
           button.addActionListener(myActionLintener);

           frame.add(button,BorderLayout.CENTER);
           frame.pack();
           frame.setVisible(true);


           winderClose(frame);



      }
       //创建一个关闭的方法
       private  static void winderClose(Frame frame){
           frame.addWindowListener(new WindowAdapter() {
               @Override
               public void windowClosing(WindowEvent e) {
                   System.exit(0);
              }
          });
      }
    }

    //事件监听
    class MyActionLintener implements  ActionListener{

       @Override
       public void actionPerformed(ActionEvent e) {
           System.out.println("tianxia");
      }
    }

     

    多个按钮共享一个事件

    //多个按钮实现同一个监听类
    public class TesLesson3 {
       public static void main(String[] args) {
           Frame frame = new Frame();
           Button button1 = new Button("start");
           Button button2 = new Button("stop");

           //对button1的值进行设置,默认为按键的label
           button1.setActionCommand("one");
         // button2.setActionCommand("two");
           button1.addActionListener(new MyMonitor());
           button2.addActionListener(new MyMonitor());

           frame.add(button1,BorderLayout.CENTER);
           frame.add(button2,BorderLayout.WEST);

           frame.setSize(200,200);
           frame.setVisible(true);
      }
    }

    class MyMonitor implements ActionListener{
       @Override
       public void actionPerformed(ActionEvent e) {
           if (e.getActionCommand().equals("one")){
               System.out.println("开始了");
          }else if (e.getActionCommand().equals("stop")){//button2 未进行setActionCmond()设置,为默认按键值
               System.out.println("已关闭");
               System.exit(0);
          }
      }
    }

    2.5、输入框 TextField监听

    实现:输出的是*,输出空格清空文本框

    package com.shao.gui;

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

    public class TextText1 {
       public static void main(String[] args) {
           MyFrame2 myFrame2 = new MyFrame2();
      }
    }

    class MyFrame2 extends Frame{
       public MyFrame2() {
           TextField textField = new TextField();
           add(textField);

           //监听这个文本框输入的文字
           MyAcctionListener myAcctionListener = new MyAcctionListener();
           textField.addActionListener(myAcctionListener);

           textField.setEchoChar('*');    //设置替换编码
           setVisible(true);
           pack();


      }
    }

    class  MyAcctionListener implements ActionListener{

       @Override
       public void actionPerformed(ActionEvent e) {
           TextField field = (TextField) e.getSource();      //e.getSource()获得一些资源,TextField field = (TextField)强制转型
           System.out.println(field.getText());//获得输入框的文本
           field.setText("");//null != 空,这里是在field里设置空

      }
    }

    2.6 、简易计算器,组合+内部类回顾复习!

    实现代码:

    package com.shao.gui;

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

    public class TestCalculate1 {
       public static void main(String[] args) {
           new Calculate();
      }
    }


    class Calculate extends Frame{
       public Calculate(){
           //3个文本框
           TextField num1 = new TextField(10);//字符数
           TextField num2 = new TextField( 10);//字符数
           TextField num3 = new TextField( 20);//字符数
           //1个按钮
           Button button = new Button( "=");
           //1个标签

           Label label = new Label( "+");
           //布局
           setLayout(new FlowLayout());
           add( num1);
           add(label);
           add(num2);
           add(button);
           add( num3);

           button.addActionListener(new MyAcctionListener2(num1,num2,num3));
           pack();
           setVisible(true);
      }
    }

    class MyAcctionListener2 implements ActionListener{

       private  TextField num1,num2,num3;

       public MyAcctionListener2(TextField num1, TextField num2, TextField num3) {
           this.num1 = num1;
           this.num2 = num2;
           this.num3 = num3;
      }

       @Override
       public void actionPerformed(ActionEvent e) {
           //1.获得加数和被加数
           int n1 = Integer. parseInt(num1.getText());
           int n2 = Integer .parseInt(num2.getText());

           //2.将这个值+法运算后,放到第三个框
           num3. setText(""+(n1+n2));

           //3.清除前两个框
           num1. setText("" );
           num2. setText("");

      }
    }

     

    代码优化

    package com.shao.gui;

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

    public class TestCalculate1 {
       public static void main(String[] args) {
           new Calculate().loadFrame();
      }
    }

    //计算器类
    class Calculate extends Frame{
       //属性
       TextField num1, num2, num3;

           //方法
           public void loadFrame(){


               num1 = new TextField(  10);//字符数
               num2 = new TextField(  10);//字符数.
               num3 = new TextField(  20);//字符数
               Button button = new Button(  "=");
               Label label = new Label( "+");
               button. addActionListener(new MyAcctionListener2());

           //布局
               setLayout(new FlowLayout());
               add( num1);
               add(label);
               add( num2);
               add( button);
               add( num3);

               pack();
               setVisible(true);

          }

           //监听器类
           private class MyAcctionListener2 implements ActionListener{

               @Override
               public void actionPerformed(ActionEvent e) {
                   //1.获得加数和被加数
                   int n1 = Integer. parseInt(num1.getText());
                   int n2 = Integer .parseInt(num2.getText());

                   //2.将这个值+法运算后,放到第三个框
                   num3. setText(""+(n1+n2));

                   //3.清除前两个框
                   num1. setText("" );
                   num2. setText("");

              }
          }


    }

    2.7画笔

    package com.shao.gui;

    import java.awt.*;

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

    class  MyPaint extends Frame{

       public void loadFrame(){
           setBounds(200,300,500,600);
           setVisible(true);
      }
       @Override
       public void paint(Graphics g) {
           //画笔需要有颜色,颜色设置后,接下来的图片在重新设置颜色前都是用的这个颜色
           g.setColor(Color.red);
           g.drawOval(100,100,100,100);

           g.setColor(Color.green);
           g.fillRect(200,200,100,100);//实心的圆
           
           //养成习惯,画笔用完,把它还原到最初的颜色(黑色)
      }
    }

    2.8、鼠标监听

    package com.shao.gui;

    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    import java.util.Iterator;

    public class TestMouseListener1 {
       public static void main(String[] args) {
           new MyFrame4("画图");
      }
    }

    class MyFrame4 extends Frame{
       //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
       ArrayList points;

       public MyFrame4(String title) {
           super(title);
           setBounds(  200, 200,  400,  300) ;
           //存鼠标点击的点
           points = new ArrayList<>();

           setVisible(true);
           //鼠标监听器,正对这个窗口
           this.addMouseListener( new MyMouseListener1());

          }

       @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);
          }

      }

       //添加一个点到界面上
       public void addPaint (Point point){
           points.add(point);
      }

            //适配器模式
          private class MyMouseListener1 extends MouseAdapter {
               //鼠标按下,弹起,按住不放
               @Override
                public void mousePressed(MouseEvent e) {
                  MyFrame4 frame = (MyFrame4) e.getSource();
                  //这个我们点击的时候,就会在界面上产生一个点!画
                   //这个点就是鼠标的点;
                  frame.addPaint(new Point(e.getX(),e.getY()));

                   //每次点击鼠标都需要重新画一-遍
                   frame.repaint();//刷新
          }

          }
      }

    image-20200823004310996


    2.9、窗口监听

    package com.shao.gui;

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

    public class TestWindow1 {
       public static void main(String[] args) {
           new WindowFrame();
      }
    }


    class WindowFrame extends Frame{
       public WindowFrame(){
           setBackground(Color.red);
           setVisible(true);
           setBounds(100,100,200,200);
           
           //匿名内部类
          this.addWindowListener(new WindowAdapter() {
             
              //关闭窗口
              @Override
              public void windowClosing(WindowEvent e) {
                  super.windowClosing(e);
              }

              //激活窗口,在窗口打开时运行
              @Override
              public void windowActivated(WindowEvent e) {
                  super.windowActivated(e);
              }
          });
      }
       

    /*   class MyWindowListener5 extends WindowAdapter{
           @Override
           public void windowClosing(WindowEvent e) {
               setVisible(false);//隐藏窗口,通过按钮,隐藏当前窗口
           }
       }

    */
    }

    2.10、键盘监听

    package com.shao.gui;

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

    public class TestKListener1 {
       public static void main(String[] args) {
           new KeyFrame();
      }
    }

    class KeyFrame extends Frame{
       public KeyFrame(){
           setBackground(Color.red);
           setVisible(true);
           setBounds(2,2,300,400);

           this.addKeyListener(new KeyAdapter() {
               //键盘按下
               @Override
               public void keyPressed(KeyEvent e) {
                   //获得键盘下的键是哪一个,当前的
                   int keyCode = e.getKeyCode();//不用去记录这个数值,直接使用静态属性VK——XXX
                   System.out.println(keyCode);
                   if (keyCode == KeyEvent.VK_W){
                       System.out.println("这是上");
                  }
                   if (keyCode == 83){//在源码KeyEcent里,数值是16进制
                       System.out.println("这是下");
                  }
              }
          });

      }
    }

    3、Swing

    3.1、窗口,面板

    package com.shao.gui;

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

    public class TestJFrame1 {

       //init(); 初始化
       public void  init(){
           //顶级窗口
           JFrame jf = new JFrame("这是一个新的JFrame窗口");
           jf.setVisible(true);
           jf.setBackground(Color.BLUE);// 此处代码无用,无法设置颜色,因为JFrame是顶级窗口,是设置在容器上***************
           jf.setBounds(200,200,200,200);
           JLabel jLabel = new JLabel("我在学习GUI编程");
           jf.add(jLabel);
           //标签居中
           jLabel.setHorizontalAlignment(SwingConstants.CENTER);
           
           //容器实例化
           Container contentPane = jf.getContentPane();
           contentPane.setBackground(Color.RED);

           //JFrame关闭事件
           jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

       public static void main(String[] args) {
           //建立一个窗口
           new TestJFrame1().init();
      }
    }

    3.2、弹窗

    JDialog,用来被弹出,默认就有关闭事件

    package com.shao.gui;

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

    public class TestDialog1    extends JFrame {

           public TestDialog1(){
               this . setVisible(true);
               this. setSize(  700,  500);
               this . setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
               //JFrame放东西,容器
               Container container = this.getContentPane();
               //绝对布局.
               container.setLayout(null);
               //按钮
               JButton button = new JButton("点击弹出一个对话框"); //创建
               button. setBounds(  30,  30,  200,  50);
               //点击这个按钮的时候,弹出一个弹窗
               button. addActionListener(new ActionListener() {
                   @Override
                   public void actionPerformed(ActionEvent e) {
                       //弹窗
                       new MyDialogDemo();
                  }
              });
               container . add(button);
          }

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

    class  MyDialogDemo extends JDialog{
       public MyDialogDemo() {
           this.setVisible(true);
           this.setBounds(100,100,500,500);
          // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //弹框不用再写关闭
           Container container = this.getContentPane();
           container.setLayout(null);
           JLabel Jlabel1 = new JLabel("好好学习");
           Jlabel1.setBounds(20,20,200,200);

           container.add(Jlabel1);
      }
    }

    image-20200823234033843


    3.3、标签

    label

    new JLabel("xxx");

    图标ICON Icon是Swing包下的一个接口

    package com.shao.gui;

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

    public class TestIcon1 extends JFrame implements Icon {

       private int width;
       private int height;

       public void init(){
           TestIcon1 testIcon1 = new TestIcon1(15,15);//此处传参,
           
           JLabel label = new JLabel("ivontest", testIcon1, SwingConstants.CENTER);

           Container container = getContentPane();
           container.add(label);

           this.setVisible(true);
           this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

      }

       public static void main(String[] args) {
           new TestIcon1().init();
      }

       public TestIcon1() { }//无参构造

       public TestIcon1(int width,int height) {
           this.width = width;
           this.height = height;
      }
       @Override
       public void paintIcon(Component c, Graphics g, int x, int y) {
       g.fillOval(x,y,width,height);

      }

       @Override
       public int getIconWidth() {
           return this.width;
      }

       @Override
       public int getIconHeight() {
           return this.height;
      }
    }

    image-20200824100404932

     


    package com.shao.gui;

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

    public class TestImageIcon extends JFrame {
       public  TestImageIcon(){
           JLabel label = new JLabel("ImageIcon");
           URL url = TestImageIcon.class.getResource("zhuomian.jpg");

           ImageIcon imageIcon = new ImageIcon(url);
           label.setIcon(imageIcon);
           label.setHorizontalAlignment(SwingConstants.CENTER);

           Container container = getContentPane();
           container.add(label);

           setVisible(true);
           setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
           setBounds(100,200,400,500);
      }


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


    }

    image-20200828005255130


    3.4、面板文本域

    面板

    package com.shao.gui;

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

    public class TestJpanel extends JFrame {
       public TestJpanel(){
           Container container = this.getContentPane();
           container . setLayout(new GridLayout(  2, 1,  10,10)); //后面的参数的意思,间距
           JPanel panel1 = new JPanel(new GridLayout( 1,  3));
           panel1. add(new JButton( "1"));
           panel1. add(new JButton( "1"));
           panel1. add(new JButton( "1"));
           container . add(panel1);
           this . setVisible(true);
           this . setSize(500, 500);
           this . setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
      }

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

    image-20200828171725225

     


    文本域

    package com.shao.gui;

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

    public class TestJScroll extends JFrame {
       public TestJScroll(){
           Container container = this.getContentPane();

           //文本域
           JTextArea textArea = new JTextArea(20,50);//每行20字,50行
           textArea.setText("加油加油,乘风破浪");

           //Scroll面板
           JScrollPane scrollPane = new JScrollPane(textArea);//滚动面板
           container.add(scrollPane);

           setVisible(true);
           setBounds(100,100,200,200);
           setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

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

    image-20200828160043608

    3.5、按钮

    ●图片按钮

    package com.shao.gui;

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

    public class TesJButton1 extends JFrame {
       public TesJButton1(){
           Container container = this.getContentPane();

           //将-一个图片变为图标
           URL resource = TesJButton1. class . getResource( "zhuomian.jpg");
           Icon icon = new ImageIcon( resource);
           //把这个图标放在按钮上
           JButton button = new JButton();
           button. setIcon(icon);
           button. setToolTipText("图片按钮");

           //add
           container . add(button);
           this . setVisible(true);
           this. setSize(500, 300);
           this . setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

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

    image-20200828161442955


    ●单选按钮

    package com.shao.gui;

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

    public class TestJButton2 extends JFrame {
       public TestJButton2(){
           Container container = this.getContentPane();
           //单选框
           JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
           JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
           JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");

           //由于单选框只能选择一个分组,一个组中只能选择一个,如果去掉分组,就会类似多选
           ButtonGroup buttonGroup = new ButtonGroup();
           buttonGroup.add(jRadioButton1);
           buttonGroup.add(jRadioButton2);
           buttonGroup.add(jRadioButton3);

           container.add(jRadioButton1,BorderLayout.NORTH);
           container.add(jRadioButton2,BorderLayout.CENTER);
           container.add(jRadioButton3,BorderLayout.SOUTH);

           this . setVisible(true);
           this. setSize(500, 300);
           this . setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

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

    image-20200828170952217


    ●复选按钮

    package com.shao.gui;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TestJButton3 extends JFrame {
        public TestJButton3(){
            Container container = this.getContentPane();
            //多选框
            JCheckBox jCheckBox1 = new JCheckBox("checkbox1");
            JCheckBox jCheckBox2 = new JCheckBox("checkbox2");
            JCheckBox jCheckBox3 = new JCheckBox("checkbox3");
    
            container.add(jCheckBox1,BorderLayout.NORTH);
            container.add(jCheckBox2,BorderLayout.CENTER);
            container.add(jCheckBox3,BorderLayout.SOUTH);
    
            this . setVisible(true);
            this. setSize(500, 300);
            this . setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TestJButton3();
        }
    }

    image-20200828171504973


    3.6列表

    ●下拉框

    package com.shao.gui;

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

    public class TestCombobox1 extends JFrame {
       public TestCombobox1(){
           Container container = this.getContentPane();
           JPanel jPanel = new JPanel();
           jPanel.setBounds(100,100,100,20);

           JComboBox jComboBox = new JComboBox();

           jComboBox.addItem("");
           jComboBox.addItem("星期一");
           jComboBox.addItem("星期二");
           jComboBox.addItem("星期三");

           jPanel.add(jComboBox);
           container.add(jPanel);

           this.setVisible(true);
           this.setBounds(100,100,400,500);
           this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

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

    image-20200828180259949

    ●列表框

    package com.shao.gui;

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

    public class TestCombobox2 extends JFrame {
       public TestCombobox2(){
           Container container = this.getContentPane();
           JPanel jPanel = new JPanel();
           jPanel.setBounds(100,100,100,20);

          //生成列表的内容
           String[] contents = {"1","2","3"};

           JList jList = new JList(contents);


           jPanel.add(jList);
           container.add(jPanel);

           this.setVisible(true);
           this.setBounds(100,100,400,500);
           this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

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

    image-20200828185909372

     


    3.7文本框

    ●文本框

    package com.shao.gui;

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

    public class TestText1 extends JFrame {
       public TestText1(){
           Container container = this.getContentPane();

           JTextField text1 = new JTextField("hello");
           JTextField text2 = new JTextField("hello");
           JTextField text3 = new JTextField("hello");

           container.add(text1,BorderLayout.NORTH);
           container.add(text2,BorderLayout.CENTER);
           container.add(text3,BorderLayout.SOUTH);

           this.setVisible(true);
           this.setBounds(100,100,400,500);
           this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

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

    image-20200828221233483

     

    ●密码框

    package com.shao.gui;

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

    public class TestText1 extends JFrame {
       public TestText1(){
           Container container = this.getContentPane();

           JPasswordField jPasswordField = new JPasswordField();
           container.add(jPasswordField);//默认是黑色圆点
           jPasswordField.setEchoChar('*');//换成星号替换
           
           this.setVisible(true);
           this.setBounds(100,100,400,500);
           this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

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

     


    ●文本域

    package com.shao.gui;

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

    public class TestJScroll extends JFrame {
       public TestJScroll(){
           Container container = this.getContentPane();

           //文本域
           JTextArea textArea = new JTextArea(20,50);//每行20字,50行
           textArea.setText("加油加油,乘风破浪");

           //Scroll面板
           JScrollPane scrollPane = new JScrollPane(textArea);//滚动面板
           container.add(scrollPane);

           setVisible(true);
           setBounds(100,100,200,200);
           setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

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

    image-20200828160043608


    4、贪吃蛇项目练习

    项目已完成

    加油! 加油!
  • 相关阅读:
    web控件文本框不响应回车事件
    封装的概念
    js 中eval的使用
    C#调用存储过程
    javascript和C#对URI编码
    比较好的博客日历控件
    扩展方法实例
    C# 集合类
    数据访问层的几种数据库连接方式
    aspnetpage分页控件的使用
  • 原文地址:https://www.cnblogs.com/shaozhi/p/13887984.html
Copyright © 2011-2022 走看看