zoukankan      html  css  js  c++  java
  • GUI

    GUI

    Frame

    public class TestFrame {
        public static void main(String[] args) {
            Frame frame = new Frame("java图形界面");
    //        设置可见性
            frame.setVisible(true);
    //        设置窗口大小
            frame.setSize(400,400);
            frame.setBackground(Color.PINK);
    //        弹出的初始位置
            frame.setLocation(200,200);
    //        设置大小固定
            frame.setResizable(false);
        }
    }
    

    Panel

    //面板 可以看成一个空间 但是不能单独存在
    public class TestPanel {
        public static void main(String[] args) {
            Frame frame = new Frame();
    //        布局的概念
            Panel panel = new Panel();
    
    //        设置布局
            frame.setLayout(null);//不设置 默认置顶 所以给它个空
            frame.setBounds(300,300,300,300);
            frame.setBackground(Color.pink);
    
    //        panel设置坐标 相对于frame
            panel.setBounds(50,50,200,200);
            panel.setBackground(Color.gray);
    
    //        面板放入窗口中
            frame.add(panel);
            frame.setVisible(true);
    
    //        监听事件 窗口关闭事件
    //        适配器模式
            frame.addWindowListener(new WindowAdapter() {
    //            窗口点击关闭的时候做的事情
                @Override
                public void windowClosing(WindowEvent e) {
    //                结束程序
                    System.exit(0);
                }
            });
        }
    }
    

    GridLayout

    //表格布局
    public class TestGridLayout {
        public static void main(String[] args) {
            Frame frame = new Frame("");
    
            Button btn1 = new Button("btn1");
            Button btn2 = new Button("btn2");
            Button btn3 = new Button("btn3");
            Button btn4 = new Button("btn4");
            Button btn5 = new Button("btn5");
            Button btn6 = new Button("btn6");
    
            frame.setLayout(new GridLayout(3, 2));
    
            frame.add(btn1);
            frame.add(btn2);
            frame.add(btn3);
            frame.add(btn4);
            frame.add(btn5);
            frame.add(btn6);
    
            frame.pack();
            frame.setBounds(200, 200, 200, 200);
            frame.setVisible(true);
    
        }
    }
    

    FlowLayout

    public class TestFlowLayout {
        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(FlowLayout.RIGHT));
    //        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
    
            frame.setSize(200,200);
            frame.setVisible(true);
    
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
        }
    

    BorderLayout

    //东西南北中布局
    public class TestBorderLayout {
        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.setBounds(200,200,200,200);
            frame.setVisible(true);
    
    
        }
    }
    

    练习

    public class Think1 {
        public static void main(String[] args) {
            Frame frame = new Frame("联系布局");
            Button btn1 = new Button("btn1");
            Button btn2 = new Button("btn2");
            Button btn3 = new Button("btn3");
            Button btn4 = new Button("btn4");
            Button btn5 = new Button("btn5");
            Button btn6 = new Button("btn6");
            Button btn7 = new Button("btn7");
            Button btn8 = new Button("btn8");
            Button btn9 = new Button("btn9");
            Button btn10 = new Button("btn10");
    
            Panel panel1 = new Panel();
    //        panel1.setLayout(new BorderLayout());
            Panel panel = new Panel();
            panel.setLayout(new GridLayout(2,1));
            panel.add(btn2);
            panel.add(btn3);
            panel1.setLayout(new GridLayout(1,3));
    //        panel1.add(btn1,BorderLayout.EAST);
    //        panel1.add(panel,BorderLayout.CENTER);
    //        panel1.add(btn4,BorderLayout.WEST);
            panel1.add(btn1);
            panel1.add(panel);
            panel1.add(btn4);
    
            Panel panel2 = new Panel();
            Panel panel3 = new Panel();
            panel3.setLayout(new GridLayout(2,2));
            panel3.add(btn6);
            panel3.add(btn7);
            panel3.add(btn8);
            panel3.add(btn9);
            panel2.setLayout(new GridLayout(1,3));
            panel2.add(btn5,BorderLayout.EAST);
            panel2.add(panel3,BorderLayout.CENTER);
            panel2.add(btn10,BorderLayout.WEST);
    
            frame.setLayout(new GridLayout(2,1));
    
            frame.add(panel1);
            frame.add(panel2);
            frame.setBounds(200, 200, 400, 400);
            frame.setVisible(true);
        }
    }
    

    WindowListener

    package com.summer.demo1;
    
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class TestWindow {
        public static void main(String[] args) {
            new WindowFrame();
        }
    }
    
    class WindowFrame extends Frame {
        public WindowFrame() {
            setVisible(true);
            setBackground(Color.pink);
            setBounds(200, 200, 200, 200);
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
    //                关闭窗口
                    System.out.println("windowClosing");
                    System.exit(0);
                }
                @Override
                public void windowActivated(WindowEvent e) {
    //                窗口激活
                    WindowFrame source =(WindowFrame) e.getSource();
                    source.setTitle("被激活了");
                    System.out.println("windowActivated");
                }
            });
        }
    }
    

    JFrame

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class JFrameDemo {
        public void init() {
            JFrame jFrame = new JFrame("JFrame");
            jFrame.setBounds(100, 100, 200, 200);
            jFrame.setVisible(true);
    
            JLabel jLabel = new JLabel("Summer");
            jFrame.add(jLabel);
    //       居中
            jLabel.setHorizontalAlignment(SwingConstants.CENTER);
    //      JFrame用容器设置背景颜色
            Container contentPane = jFrame.getContentPane();
            contentPane.setBackground(Color.pink);
            jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new JFrameDemo().init();
        }
    }
    
    

    JDialog

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    //主窗口
    public class TestDialog extends JFrame {
        public TestDialog() {
            setVisible(true);
            setSize(700, 500);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
    //        JFrame 放东西 容器
            Container container = getContentPane();
    //        绝对布局
            container.setLayout(null);
    
    //        按钮
            JButton jButton = new JButton("点击弹出对话框");
            jButton.setBounds(30, 30, 200, 50);
    
    //        点击按钮弹 弹窗
            container.add(jButton);
            jButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
    //                弹窗
                    new MyDialog();
                }
            });
    
        }
    
        public static void main(String[] args) {
            new TestDialog();
        }
    }
    
    //弹窗的窗口
    class MyDialog extends JDialog {
        public MyDialog() {
            this.setVisible(true);
            this.setBounds(100,100,500,500);
            Container container = this.getContentPane();
            container.setLayout(null);
            container.add(new JLabel("Summer搞得弹框"));
        }
    }
    

    JLabel/ImageIcon

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;
    
    public class TestImageIcon extends JFrame {
        public TestImageIcon(){
    //获取图片地址
    
            URL resource = TestImageIcon.class.getResource("picture.png");
            ImageIcon imageIcon = new ImageIcon(resource);
            JLabel label = new JLabel("ImageIcon", imageIcon, SwingConstants.CENTER);
            Container container = this.getContentPane();
            container.add(label);
            this.setVisible(true);
            this.pack();
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TestImageIcon();
        }
    
    
    

    JPanel

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TestJPanel extends JFrame {
        public TestJPanel()  {
            Container container = getContentPane();
    //        后面两个参数代表间距
            container.setLayout(new GridLayout(2,1,10,10));
            JPanel jPanel1 = new JPanel(new GridLayout(1,1));
            JPanel jPanel2 = new JPanel(new GridLayout(1,1));
            JPanel jPanel3 = new JPanel(new GridLayout(2,1));
            JPanel jPanel4 = new JPanel(new GridLayout(3,2));
            jPanel1.add(new JButton("1"));
            jPanel1.add(new JButton("1"));
            jPanel1.add(new JButton("1"));
            jPanel2.add(new JButton("2"));
            jPanel2.add(new JButton("2"));
            jPanel3.add(new JButton("3"));
            jPanel3.add(new JButton("3"));
            jPanel4.add(new JButton("4"));
            jPanel4.add(new JButton("4"));
            jPanel4.add(new JButton("4"));
            jPanel4.add(new JButton("4"));
            jPanel4.add(new JButton("4"));
            jPanel4.add(new JButton("4"));
            jPanel4.add(new JButton("4"));
            jPanel4.add(new JButton("4"));
            jPanel4.add(new JButton("4"));
            jPanel4.add(new JButton("4"));
            container.add(jPanel1);
            container.add(jPanel2);
            container.add(jPanel3);
            container.add(jPanel4);
            setVisible(true);
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(500,500);
        }
        public static void main(String[] args) {
            new TestJPanel();
        }
    

    JScrollPane 滑动面板

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TestJScroll extends JFrame {
        public TestJScroll() {
            Container container = getContentPane();
    
            //文本域可以换行 JTextField文本框 不可以换行
            JTextArea jTextArea = new JTextArea(20,50);
            jTextArea.setText("Summer欢迎你~");
            //JScrollPane 面板
            JScrollPane jScrollPane = new JScrollPane(jTextArea);
    //        jScrollPane.add(jTextArea);
            container.add(jScrollPane);
    
            setVisible(true);
            setBounds(100,100,300,350);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TestJScroll();
        }
    }
    

    JButton按钮

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;
    
    public class TestJButton extends JFrame {
        public TestJButton() {
            Container container = getContentPane();
            URL resource = TestJButton.class.getResource("picture.png");
            JButton jButton = new JButton(new ImageIcon(resource));
            jButton.setToolTipText("图片按钮");
            jButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Hello!");
                }
            });
            container.add(jButton);
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            pack();
        }
    
        public static void main(String[] args) {
            new TestJButton();
        }
    }
    
    

    JRadioButton/ButtonGroup单选框

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;
    
    public class TestJRadioButton extends JFrame {
        public TestJRadioButton() {
            Container container = getContentPane();
            URL resource = TestJButton.class.getResource("picture.png");
    
            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);
    
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            pack();
        }
    
        public static void main(String[] args) {
            new TestJRadioButton();
        }
    }
    
    

    JCheckBox多选框

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;
    
    public class TestJButton extends JFrame {
        public TestJButton() {
            Container container = getContentPane();
            URL resource = TestJButton.class.getResource("picture.png");
    
            JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
            JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
            JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");
            JCheckBox jCheckBox4 = new JCheckBox("jCheckBox4");
    
            container.setLayout(new FlowLayout());
            container.add(jCheckBox1);
            container.add(jCheckBox2);
            container.add(jCheckBox3);
            container.add(jCheckBox4);
    
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            pack();
        }
    
        public static void main(String[] args) {
            new TestJButton();
        }
    }
    

    JCombobox下拉框

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class TestJCombobox extends JFrame {
    
        public TestJCombobox() {
            Container container = getContentPane();
    
            JComboBox status = new JComboBox();
            status.addItem(null);
            status.addItem("正在上映");
            status.addItem("即将上映");
            status.addItem("已下架");
            container.add(status);
    
    //        获取下拉框选中的值
            status.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JComboBox source = (JComboBox) e.getSource();
                    System.out.println("选中了===》"+source.getSelectedItem());
                }
            });
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(500, 400);
        }
    
        public static void main(String[] args) {
            new TestJCombobox();
        }
    }
    

    JList列表

    • 展示信息,一般动态扩容
    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TestJList extends JFrame {
        public TestJList() {
            Container container = getContentPane();
    
            String[] contens = {"qwe", "wer", "df"};
            JList jList = new JList(contens);
    
            container.add(jList);
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(500, 400);
        }
    
        public static void main(String[] args) {
            new TestJList();
        }
    }
    

    JTextField文本框

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TestText extends JFrame{
        public TestText() {
            Container container = getContentPane();
    
            JTextField jTextField1 = new JTextField("Hello");
            JTextField jTextField2 = new JTextField("World",20);
    
            container.add(jTextField1,BorderLayout.EAST);
            container.add(jTextField2,BorderLayout.WEST);
    
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(500, 400);
        }
    
        public static void main(String[] args) {
            new TestText();
        }
    }
    
    

    JPasswordField密码框

    package com.summer.demo1;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TestText extends JFrame{
        public TestText() {
            Container container = getContentPane();
            JPasswordField jPasswordField = new JPasswordField();
            jPasswordField.setEchoChar('*');
            container.add(jPasswordField);
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(500, 400);
        }
    
        public static void main(String[] args) {
            new TestText();
        }
    }
    
    

    JTextArea

    [注:]container中add面板来布局

    贪吃蛇

    Data.class

    package snake;
    
    import javax.swing.*;
    import java.net.URL;
    
    //数据中心
    public class Data {
    //    相对路径 picture.png
    //    绝对路径 / 相当于当前的项目
        private static URL bodyURL = Data.class.getResource("statics/body.png");
        public static ImageIcon body = new ImageIcon(bodyURL);
        private static URL headURL = Data.class.getResource("statics/head.png");
        public static ImageIcon head = new ImageIcon(headURL);
        private static URL foodURL = Data.class.getResource("statics/food.png");
        public static ImageIcon food = new ImageIcon(foodURL);
    
        private static URL upURL = Data.class.getResource("statics/up.png");
        private static URL downURL = Data.class.getResource("statics/down.png");
        private static URL rightURL = Data.class.getResource("statics/right.png");
        private static URL leftURL = Data.class.getResource("statics/left.png");
        public static ImageIcon up = new ImageIcon(upURL);
        public static ImageIcon down = new ImageIcon(downURL);
        public static ImageIcon right = new ImageIcon(rightURL);
        public static ImageIcon left = new ImageIcon(leftURL);
    
    }
    

    GamePanel.class

    package snake;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    //游戏的面板
    public class GamePanel extends JPanel implements KeyListener, ActionListener {
    
        //    定义蛇的数据结构
        int length; //蛇的长度
        int[] snakeX = new int[600];
        int[] snakeY = new int[500];
        String direction;
        boolean isStart = false;//默认不开始
        Timer timer = new Timer(100, this);//100毫秒执行一次 ms单位
    
        //    初始化方法
        public void init() {
            length = 3;
            snakeX[0] = 100;
            snakeY[0] = 100;//蛇的头
            snakeX[1] = 75;
            snakeY[1] = 100;//蛇的身子
            snakeX[2] = 50;
            snakeY[2] = 100;//蛇的身子
            direction = "R";
        }
    
        //构造器
        public GamePanel() {
            init();
    //        获得焦点 和 键盘事件
            this.setFocusable(true);//获得焦点事件
    //        获取键盘事件
            this.addKeyListener(this);
    
    //        游戏一开始定时器就启动
            timer.start();
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);//清屏
    //        绘制静态的面板
            this.setBackground(Color.PINK);
            Data.head.paintIcon(this, g, 25, 11);
            g.fillRect(25, 75, 850, 600);
    //      初始化朝右边
            if (direction.equals("R")) {
                Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
            } else if (direction.equals("L")) {
                Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
            } else if (direction.equals("U")) {
                Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
            } else if (direction.equals("D")) {
                Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
            }
            for (int i = 1; i < length; i++) {
                Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);
            }
            if (!isStart) {
                g.setColor(Color.white);
                g.setFont(new Font("微软雅黑", Font.BOLD, 30));
                g.drawString("按下空格开始游戏~", 300, 300);
            }
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
    
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_SPACE) {
                isStart = !isStart;
                repaint();
            }
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
    
        }
    
        //  事件监听--需要通过固定事件来刷新 1s 10次
        @Override
        public void actionPerformed(ActionEvent e) {
            if (isStart) {//游戏是开始状态 小蛇动起来
                //右移
                for (int i = length - 1; i > 0; i--) {
                    snakeX[i] = snakeX[i - 1];//后一节移动到前一节
                    snakeY[i] = snakeY[i - 1];
                }
                snakeX[0] = snakeX[0] + 25;
                if (snakeX[0] > 850) {
                    snakeX[0] = 25;
                }
                repaint();
            }
            timer.start();
        }
    }
    //键盘监听器 可以新写一个类 也可以内部类
    
    

    StartGame.class

    package snake;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class StartGame {
        public static void main(String[] args) {
            JFrame jFrame = new JFrame();
            jFrame.setBounds(10, 10, 900, 720);
            Container container = jFrame.getContentPane();
            container.add(new GamePanel());
            jFrame.setVisible(true);
            jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            jFrame.setResizable(false);//窗口大小不可变
        }
    }
    

  • 相关阅读:
    BZOJ 3236: [Ahoi2013]作业
    BZOJ 3234: [Ahoi2013]立方体
    BZOJ 3235: [Ahoi2013]好方的蛇
    Hadoop 系列HDFS的Java API( Java API介绍)
    Hadoop 系列 HDFS 的JavaAPI Windows+IDEA+HDFS快速入门
    Hadoop 系列 HDFS:分布式文件系统(HDFS参数解读)
    Hadoop 系列 HDFS:分布式文件系统(HDFS集群模式)
    Hadoop 系列 HDFS:分布式文件系统(HDFS文件读写)
    Hadoop 系列 HDFS:分布式文件系统( HDFS概述)
    Hadoop中DataNode没有启动解决办法
  • 原文地址:https://www.cnblogs.com/ls-summer/p/14988809.html
Copyright © 2011-2022 走看看