首页 > 其他分享 >GUI

GUI

时间:2022-12-13 22:57:16浏览次数:36  
标签:java GUI add frame import new public

  • 适配器 实现监听器 只需要重写一个方法? 继承抽象类?
  • 什么是适配器模式
  • 画笔 重写paint就行了 不用调用?
  • 匿名内部类中的windowActivated方法为什么可以直接调setTitle();
  • p30 2. JDialog窗体 程序执行顺序
  • 拉大一下才显示
  • Timer定时器
  • setFocusable

AWT

Frame

package com.GUI.frame;
//Frame窗口
import java.awt.*;

public class FrameDemo01 {
    public static void main(String[] args) {
        Frame frame = new Frame("第一个窗口");

        frame.setSize(400,400);

        frame.setLocation(200,200);

        frame.setBackground(Color.gray);

        frame.setVisible(true);

        frame.setResizable(false);
    }
}
package com.GUI.frame;
//Frame窗口 多个
import java.awt.*;

public class FrameDemo02 {
    public static void main(String[] args) {
        new MyFrame(200,200,300,300,Color.gray);
        new MyFrame(500,200,300,300,Color.lightGray);
        new MyFrame(200,500,300,300,Color.darkGray);
        new MyFrame(500,500,300,300,Color.pink);
    }
}
class MyFrame extends Frame{
    static int id = 0;
    public MyFrame(int x,int y,int width,int height,Color color){
        super("MyFrame"+(++id));
        setBounds(x,y,width,height);
        setVisible(true);
        setBackground(color);
        setResizable(false);
    }
}

Panel

package com.GUI.panel;
//Panel面板
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class PanelDemo01 {
    public static void main(String[] args) {
        Frame frame = new Frame("这是个窗口");
        Panel panel = new Panel();

        frame.setLayout(null);

        frame.setBounds(200,200,500,500);
        frame.setBackground(new Color(103, 103, 112));

        panel.setBounds(100,100,300,300);
        panel.setBackground(new Color(173, 173, 182));

        frame.add(panel);

        frame.setVisible(true);

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

    }
}

布局管理器

package com.GUI.layout;
//布局管理器:FlowLayout布局
import java.awt.*;

public class FlowLayoutDemo01 {
    public static void main(String[] args) {
        Frame frame = new Frame("FlowLayout");

//        frame.setLayout(new FlowLayout());
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        frame.setLocation(200,200);
        frame.setSize(400,400);

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

        frame.setVisible(true);

    }
}
package com.GUI.layout;
//布局管理器:BorderLayout布局
import java.awt.*;

public class BorderLayoutDemo02 {
    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayout");
        frame.setVisible(true);
        frame.setBounds(200,200,400,400);
        frame.setBackground(new Color(58, 96, 108));

        frame.setLayout(new BorderLayout());

        Button buttonEast = new Button("buttonEast");
        Button buttonWest = new Button("buttonWest");
        Button buttonSouth = new Button("buttonSouth");
        Button buttonNorth = new Button("buttonNorth");
        Button buttonCenter = new Button("buttonCenter");

        frame.add(buttonEast,BorderLayout.EAST);
        frame.add(buttonWest,BorderLayout.WEST);
        frame.add(buttonSouth,BorderLayout.SOUTH);
        frame.add(buttonNorth,BorderLayout.NORTH);
        frame.add(buttonCenter,BorderLayout.CENTER);

    }
}
package com.GUI.layout;
//布局管理器:GridLayout布局
import java.awt.*;

public class GridLayoutDemo03 {
    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayout");
        frame.setVisible(true);

        frame.setBackground(new Color(58, 96, 108));

        frame.setLayout(new GridLayout(3,2));

        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.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);

        frame.pack();

    }
}
package com.GUI.layout;
//quiz 嵌套
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Quiz04 {
    public static void main(String[] args) {
        Frame frame = new Frame("Quiz");
        frame.setVisible(true);
        frame.setBounds(200,200,500,300);
        frame.setLayout(new GridLayout(2,1));

        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2,1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2,2));

        panel1.add(new Button("west-1"),BorderLayout.WEST);
        panel1.add(new Button("east-1"),BorderLayout.EAST);
        panel2.add(new Button("1-center-1"));
        panel2.add(new Button("1-center-2"));
        panel1.add(panel2, BorderLayout.CENTER);

        panel3.add(new Button("west-2"),BorderLayout.WEST);
        panel3.add(new Button("east-2"),BorderLayout.EAST);
        for (int i = 0; i < 4; i++) {
            panel4.add(new Button("2-center-"+(i+1)));
        }
        panel3.add(panel4,BorderLayout.CENTER);

        frame.add(panel1);
        frame.add(panel3);

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

    }
}

事件监听 按钮、输入框

package com.GUI.action_listener;
//按钮  事件监听
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 ActionListenerDemo01 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(300,300);
        frame.setVisible(true);

        Button button = new Button("button");

        button.addActionListener(new MyActionListener());

        frame.add(button,BorderLayout.CENTER);

        windowClose(frame);

    }

    public static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}
package com.GUI.action_listener;
//按钮事件监听
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 ActionListenerDemo02 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);

        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        button1.addActionListener(new MyActionListener2());
        button2.addActionListener(new MyActionListener2());

        button2.setActionCommand("stop now");

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

        frame.pack();

        windowClose(frame);

    }

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

class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());

    }
}
package com.GUI.action_listener;
//输入框事件监听
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 TextDemo03 {
    public static void main(String[] args) {
        new MyFrame("TextDemo");
    }
}
class MyFrame extends Frame{
    public MyFrame(String title){
        super(title);
        setVisible(true);

        TextField textField = new TextField();
        textField.addActionListener(new MyActionListener3());
        textField.setEchoChar('*');

        add(textField);
        pack();
        windowClose(this);

    }
    public static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyActionListener3 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();
        System.out.println(textField.getText());
        textField.setText("");
    }
}
package com.GUI.action_listener;
//加法计算器  组合
//在一个类里面持有另一个类的引用的用法是一种非常典型的用法
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalculatorDemo04 {
    public static void main(String[] args) {
        new Calculator().launchFrame();
    }
}
class Calculator extends Frame{
    TextField num1,num2,num3;
    public void launchFrame(){

        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        Label label = new Label("+");

        Button button = new Button("=");

        button.addActionListener(new CalcActionListener(this));

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

        setVisible(true);
        pack();

    }
}
class CalcActionListener implements ActionListener{
    //在一个类中组合另外一个类
    Calculator calculator = null;

    public CalcActionListener(Calculator calculator) {
        this.calculator = calculator;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());
        calculator.num3.setText("" + (n1 + n2));
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}
package com.GUI.action_listener;
//加法计算器  内部类
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalculatorDemo05 {
    public static void main(String[] args) {
        new Calculator2().launchFrame();
    }
}
class Calculator2 extends Frame {
    TextField num1,num2,num3;
    public void launchFrame(){

        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        Label label = new Label("+");

        Button button = new Button("=");

        button.addActionListener(new CalcActionListener2());

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

        setVisible(true);
        pack();

    }

    private class CalcActionListener2 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            num3.setText("" + (n1 + n2));
            num1.setText("");
            num2.setText("");
        }
    }

}

other listener

Paint

package com.GUI.other_listener;
//画笔 Paint
import java.awt.*;

public class PaintDemo01 {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}
class MyPaint extends Frame{
    public void loadFrame() {
        setBounds(200,200,600,600);
        setVisible(true);
    }
    @Override
    public void paint(Graphics g) {
//        g.setColor(Color.GRAY);
        g.fillOval(50,50,200,200);
//        g.setColor(new Color(162, 117, 117));
        g.drawRect(150,250,200,200);
    }
}

listner

package com.GUI.other_listener;
//鼠标监听事件 模拟画图工具
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class MouseListenerDemo02 {
    public static void main(String[] args) {
        new MyFrame("画图").loadFrame();
    }
}
class MyFrame extends Frame{
    ArrayList<Point> points;
    public MyFrame(String title){
        super(title);
    }
    public void loadFrame(){
        points = new ArrayList<>();//注意 这里要new一个对象出来
        addMouseListener(new MyMouseListener());

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

    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            points.add( new Point(e.getX(),e.getY()) );
            MyFrame myFrame = (MyFrame) e.getSource();
            myFrame.repaint();
        }
    }
}
package com.GUI.other_listener;
//窗口监听事件
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class WindowListenerDome03 {
    public static void main(String[] args) {
        new WLFrame();
    }
}
class WLFrame extends Frame{
    public WLFrame(){
        setBounds(300,300,400,400);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
                System.exit(0);
            }
            @Override
            public void windowActivated(WindowEvent e) {
                WLFrame frame = (WLFrame)e.getSource();
                frame.setTitle("activated");
                System.out.println("windowActivated");
            }
        });
    }

}
package com.GUI.other_listener;
//键盘监听事件
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyListenerDemo04 {
    public static void main(String[] args) {
        new KLFrame();
    }
}
class KLFrame extends Frame{
    public KLFrame(){
        setBounds(300,300,500,500);
        setVisible(true);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP){
                    System.out.println("你按了上键");
                }
            }
        });

    }
}

Swing

JFrame

package com.GUI.swing.JFrame_01;
//JFrame窗口
import javax.swing.*;

public class JFrameDemo01 {
    public void init(){
        JFrame jFrame = new JFrame("这是一个JFrame窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(300,300,500,500);

        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JFrameDemo01().init();
    }
}
package com.GUI.swing.JFrame_01;
//用继承JFrame的方式编写的代码,并加入Container容器及JLabel标签
import javax.swing.*;
import java.awt.*;

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJFrame().init();
    }
}
class MyJFrame extends JFrame{
    public void init(){
        setBounds(300,300,500,500);
        setVisible(true);
//        setResizable(false);

        JLabel jLabel = new JLabel("这是一个标签");
//        this.add(jLabel);
//        jLabel.setHorizontalAlignment(SwingConstants.CENTER);//两种都可以

        Container container = this.getContentPane();
        container.setBackground(Color.YELLOW);
        container.add(jLabel);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

JDialog

package com.GUI.swing.JDialog_02;
//JDialog弹窗
//note JDialog(Frame owner, String title, boolean modal)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JDialogDemo01 extends JFrame {
    public JDialogDemo01(){
        this.setVisible(true);
        this.setBounds(300,300,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(null);//要有setBounds 不然显示不出

        JButton jButton = new JButton("点击弹出对话框");
        jButton.setBounds(150,225,200,50);

        container.add(jButton);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyJDialog();
            }
        });

    }
    public static void main(String[] args) {
        new JDialogDemo01();
    }
}
class MyJDialog extends JDialog{
    public MyJDialog(){
        setBounds(400,400,300,300);
        setVisible(true);

        Container container = getContentPane();

        JLabel jLabel = new JLabel("你点击了按钮");
        container.add(jLabel);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
    }
}

JLabel

package com.GUI.swing.JLabel_Icon_03;
//图标
import javax.swing.*;
import java.awt.*;

public class IconDemo01 extends JFrame implements Icon {
    public static void main(String[] args) {
        new IconDemo01().init();
    }

    private int width;
    private int height;
    public IconDemo01(){}

    public IconDemo01(int width, int height) {
        this.width = width;
        this.height = height;
    }
    public void init(){
        IconDemo01 iconDemo01 = new IconDemo01(20,20);
        Container container = getContentPane();
        JLabel jLabel = new JLabel("Icon",iconDemo01,SwingConstants.CENTER);
        container.add(jLabel);

        setBounds(300,300,500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

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

    @Override
    public int getIconHeight() {
        return height;
    }
}
package com.GUI.swing.JLabel_Icon_03;
//图片图标
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo02 extends JFrame {
    public void init(){
        URL url = ImageIconDemo02.class.getResource("triangle.png");
        ImageIcon imageIcon = new ImageIcon(url);
        JLabel jLabel = new JLabel("ImageIcon",imageIcon,SwingConstants.CENTER);
//        jLabel.setIcon(imageIcon);
//        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//        jLabel.setOpaque(true);

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

        setBounds(300,300,1000,1000);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);//要放在最后 不然组件显示不出来

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

JPanel

package com.GUI.swing.JPanel_04;
//JPanel
import javax.swing.*;
import java.awt.*;

public class JPanelDemo01 extends JFrame {
    public JPanelDemo01(){
        Container container = getContentPane();
        container.setLayout(new GridLayout(2,2,10,10));

        JPanel jPanel1 = new JPanel(new GridLayout(2,1));
        JPanel jPanel2 = new JPanel(new GridLayout(2,2));
        JPanel jPanel3 = new JPanel(new GridLayout(1,3));
        JPanel jPanel4 = new JPanel(new GridLayout(3,2));

        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("1"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("2"));
        jPanel3.add(new JButton("3"));
        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"));

        container.add(jPanel1);
        container.add(jPanel2);
        container.add(jPanel3);
        container.add(jPanel4);

        setBounds(300,300,500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        new JPanelDemo01();
    }
}
package com.GUI.swing.JPanel_04;
//JScrollPane
import javax.swing.*;
import java.awt.*;

public class JScrollPaneDemo02 extends JFrame {
    public JScrollPaneDemo02(){
        Container container = getContentPane();

        JTextArea jTextArea = new JTextArea(20,50);
        jTextArea.setText("这是多行文字");

        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        container.add(jScrollPane);

        setVisible(true);
        setBounds(300,300,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

JButton

package com.GUI.swing.JButton_05;
//JButton
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01(){
        URL url = JButtonDemo01.class.getResource("triangle.png");
        Icon icon = new ImageIcon(url);
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        jButton.setToolTipText("这是一个带图片的按钮");

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

        setVisible(true);
        setBounds(300,300,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo01();
    }
}
package com.GUI.swing.JButton_05;
//JRadioButton
import javax.swing.*;
import java.awt.*;

public class JRadioButtonDemo02 extends JFrame {
    public JRadioButtonDemo02(){
        Container container = 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);

        setVisible(true);
        setBounds(300,300,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JRadioButtonDemo02();
    }
}
package com.GUI.swing.JButton_05;
//JCheckBox
import javax.swing.*;
import java.awt.*;

public class JCheckBoxDemo03 extends JFrame {
    public JCheckBoxDemo03(){
        Container container = getContentPane();

        JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");

        container.add(jCheckBox1,BorderLayout.NORTH);
        container.add(jCheckBox2,BorderLayout.SOUTH);

        setVisible(true);
        setBounds(300,300,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JCheckBoxDemo03();
    }
}

JComboBox

package com.GUI.swing.JComboBox_06;
//JComboBox   下拉列表
import javax.swing.*;
import java.awt.*;

public class JComboBoxDemo01 extends JFrame {
    public JComboBoxDemo01(){
        Container container = getContentPane();

        JComboBox status = new JComboBox();
        status.addItem("正在热映");
        status.addItem(null);
        status.addItem("即将上映");
        status.addItem("已下架");
        status.addItem("");

        container.add(status);

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

    public static void main(String[] args) {
        new JComboBoxDemo01();
    }
}
package com.GUI.swing.JComboBox_06;
//JList   列表框
import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class JListDemo02 extends JFrame {
    public JListDemo02(){
        Container container = getContentPane();

//        String[] contents = {"1","2","3","4","5"};
        Vector<String> contents = new Vector<>();
        JList jList = new JList(contents);
        contents.add("11");
        contents.add("12");
        contents.add("13");

        container.add(jList);

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

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

JTextComponent

package com.GUI.swing.JTextComponent_07;
//JTextField
import javax.swing.*;
import java.awt.*;

public class JTextFieldDemo01 extends JFrame {
    public JTextFieldDemo01(){
        Container container = getContentPane();

        JTextField jTextField1 = new JTextField("hello");
        JTextField jTextField2 = new JTextField("world",20);

        container.add(jTextField1, BorderLayout.NORTH);
        container.add(jTextField2, BorderLayout.SOUTH);

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

    public static void main(String[] args) {
        new JTextFieldDemo01();
    }
}
package com.GUI.swing.JTextComponent_07;
//JPasswordField
import javax.swing.*;
import java.awt.*;

public class JPasswordFieldDome02 extends JFrame {
    public JPasswordFieldDome02(){
        Container container = getContentPane();

        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setEchoChar('*');

        container.add(jPasswordField);

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

    public static void main(String[] args) {
        new JPasswordFieldDome02();
    }
}
package com.GUI.swing.JTextComponent_07;
//JTextArea
import javax.swing.*;
import java.awt.*;

public class JTextAreaDemo03 extends JFrame {
    public JTextAreaDemo03(){
        Container container = getContentPane();

        JTextArea jTextArea = new JTextArea(20,50);

        JScrollPane jScrollPane = new JScrollPane(jTextArea);

        container.add(jScrollPane);

        setVisible(true);
        setBounds(300,300,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JTextAreaDemo03();
    }
}

snake

package com.GUI.swing.snake;
//启动
import javax.swing.*;

public class StartGame {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(10,10,900,725);

        jFrame.add(new GamePanel());

        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setResizable(false);
        jFrame.setVisible(true);
    }
}
package com.GUI.swing.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;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {
    //蛇的数据结构
    int length;
    int[] snakeX = new int[600];
    int[] snakeY = new int[500];
    String fx;
    //食物坐标
    int foodx;
    int foody;
    Random random = new Random();

    //游戏当前的状态:开始,停止,失败
    boolean isStart = false;
    boolean isFail = false;

    //积分
    int score;

    //定时器  以ms为单位
    Timer timer = new Timer(100,this);

    public GamePanel(){
        init();

        this.setFocusable(true);//获得焦点事件
        this.addKeyListener(this);

        //定时器
        timer.start();
    }
    //初始化
    public void init(){
        length = 3;
        snakeX[0] = 100; snakeY[0] = 100;
        snakeX[1] = 75; snakeY[1] = 100;
        snakeX[2] = 50; snakeY[2] = 100;
        fx = "R";

        foodx = 25 + 25*random.nextInt(34);
        foody = 75 + 25*random.nextInt(24);

        score = 0;
    }

    //绘制面板
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //界面
        this.setBackground(Color.WHITE);
        Data.header.paintIcon(this,g,25,11);
        g.fillRect(25,75,850,600);

        //积分
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度 "+length,750,30);
        g.drawString("积分 "+score,750,55);

        //食物
        Data.food.paintIcon(this,g,foodx,foody);

        //画蛇
        if (fx.equals("R")){
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        } else if (fx.equals("L")) {
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        } else if (fx.equals("U")) {
            Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        } else if (fx.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,40));
            g.drawString("按下空格开始游戏",300,300);
        }
        //失败
        if (isFail){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("失败,按下空格重新开始",250,300);
        }

    }


    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();

        //开始 暂停 失败
        if (keyCode == KeyEvent.VK_SPACE){
            if (!isFail){
                isStart = !isStart;
            }else {
                isFail = false;
                init();
            }
            repaint();
        }
        //蛇头的方向
        if (keyCode == KeyEvent.VK_RIGHT){
            fx = "R";
        }else if (keyCode == KeyEvent.VK_LEFT){
            fx = "L";
        }else if (keyCode == KeyEvent.VK_UP){
            fx = "U";
        }else if (keyCode == KeyEvent.VK_DOWN){
            fx = "D";
        }
    }
    @Override
    public void keyTyped(KeyEvent e) {

    }
    @Override
    public void keyReleased(KeyEvent e) {
    }

    //事件监听---需要通过固定事件来刷新,1s=10次
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && !isFail){
            //吃到食物
            if (snakeX[0] == foodx && snakeY[0] == foody){
                length++;
                score += 10;
                foodx = 25 + 25*random.nextInt(34);
                foody = 75 + 25*random.nextInt(24);
            }

            //灵魂:后面等于前面的!
            for (int i = length-1 ; i > 0 ; i--) {
                snakeX[i] = snakeX[i-1];
                snakeY[i] = snakeY[i-1];
            }
            //头的判断
            if (fx.equals("R")) {
                snakeX[0] = snakeX[0] + 25;
                if (snakeX[0] > 850) {snakeX[0] = 25;}

            } else if (fx.equals("L")) {
                snakeX[0] = snakeX[0] - 25;
                if (snakeX[0] < 25) {snakeX[0] = 850;}

            } else if (fx.equals("U")) {
                snakeY[0] = snakeY[0] - 25;
                if (snakeY[0] < 75) {snakeY[0] = 650;}

            } else if (fx.equals("D")) {
                snakeY[0] = snakeY[0] + 25;
                if (snakeY[0] > 650) {snakeY[0] = 75;}
            }

            //失败的情况
            for (int i = 1; i < length ; i++) {
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){
                    isFail = true;
                }
            }

            repaint();
        }

        timer.start();
    }
}
package com.GUI.swing.snake;
//存放数据
import javax.swing.*;
import java.net.URL;

public class Data {
    public static URL headerURL = Data.class.getResource("statics\\header.png");
    public static ImageIcon header = new ImageIcon(headerURL);

    public static URL upURL = Data.class.getResource("statics\\up.png");
    public static URL downURL = Data.class.getResource("statics\\down.png");
    public static URL leftURL = Data.class.getResource("statics\\left.png");
    public static URL rightURL = Data.class.getResource("statics\\right.png");
    public static URL bodyURL = Data.class.getResource("statics\\body.png");
    public static URL foodURL = Data.class.getResource("statics\\food.png");

    public static ImageIcon up = new ImageIcon(upURL);
    public static ImageIcon down = new ImageIcon(downURL);
    public static ImageIcon left = new ImageIcon(leftURL);
    public static ImageIcon right = new ImageIcon(rightURL);
    public static ImageIcon body = new ImageIcon(bodyURL);
    public static ImageIcon food = new ImageIcon(foodURL);


}
1.定义数据
2.画上去
3.监听  键盘 事件

标签:java,GUI,add,frame,import,new,public
From: https://www.cnblogs.com/799rijiyuelei/p/16980905.html

相关文章

  • Unity UGUI图文混排源码(一)
    我从一开始想到的图文混排的概念都是通过文字间的空隙去粘贴一张图片,这样确定图片前面文字的最后一个位置变成了最主要的参数,接下来就给出两种解决方案首先,先发UGUI源码的一......
  • Unity UGUI基础之Text
    Text作为UGUI最基础的控件以及最常用的控件,它在项目中的应用绝对可以算是最多的,任何一个UI界面可以说都离不开它,它的基本属性如下:一、recttransform组件:recttransform(矩形......
  • Unity UGUI实现图文混排
    目前在unity实现图文混排的好像都是通过自定义字体然后在文本获取字符的位置,用图片替换掉图片标签,这样对于支持英文来说,并没有什么影响。然后对于中文来说就是一个相当麻烦......
  • Unity UGUI无限列表(Infinite List)
    更新日期:2020年10月16日。Github源码:​​​[点我获取源码]​​索引​​InfiniteList​​​​使用​​​​创建InfiniteListScrollRect​​​​InfiniteListScrollRect参数......
  • Unity UGUI图文混排源码(三) -- 动态表情
    这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章1.首先来一个好消息,在最新版本的图文混排中,终于......
  • Unity UGUI
    超详细的基础教程传送门:(持续更新中)UnityUGUI的教程好少,幸亏找到一个UGUI的Demo,看了几个例子,以下是一些简单的学习笔记:1.导入UI图片资源2.设置参数:            ......
  • The Complete Guide to C++ Strings, Part II - String Wrapper Classes
    TheCompleteGuidetoC++Strings,PartII-StringWrapperClasses IntroductionSinceC-stylestringscanbeerror-proneanddifficulttomanage,......
  • Unity UGUI图文混排(五) -- 一张图集对应多个Text
    继上一篇说的更新了一张图集对应多个Text的功能,为了节省资源嘛这里,但是也没有舍弃之前的一个Text一个图集,因为我感觉应该两个都有用,于是我重新写了一个脚本1.其实大体跟前面......
  • Unity UGUI实现分段式血条
    我们可以看到像英雄联盟等游戏里英雄头顶的血条显示并非是纯色的,而是根据血量的多少而显示一定量的格子,这种方式明显是比较友好、比较美观的,事实上我们的游戏里面也想实现这......
  • Unity Editor 自定义属于你的DefaultHeaderGUI
    DefaultHeaderGUI默认页眉GUI,是Unity编辑器中的所有对象被选中后在Inspector界面显示的页眉GUI,如下图红框区域:在这个区域加点自己的东西。finishedDefaultHeaderGUI只需要......