GUI编程

GUI编程

一、Awt

GUI Awt Swing

1.frame

Frame frame = new Frame("窗口");

frame.setVisible(true);

frame.setSize(400,400);

frame.setBackground(new Color(1,1,1));

frame.setLocation(200,200);

frame.setResizable(false);
new frames(100,100,100,100,Color.blue);
        new frames(300,100,100,100,Color.black);
        new frames(100,300,100,100,Color.yellow);
        new frames(300,300,100,100,Color.blue);
    }
}
    class  frames extends Frame {
       static int id=0;
        public frames(int x, int y ,int w, int h, Color color) {
            super("frames"+(++id));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);
    }

```java
Frame frame = new Frame("窗口");

    frame.setVisible(true);

    frame.setSize(400,400);

    frame.setBackground(new Color(1,1,1));

    frame.setLocation(200,200);

    frame.setResizable(false);
}

3.面板 panel

    Frame frame = new Frame("窗口");
    Panel panel=new Panel();

    frame.setLayout(null);

    frame.setBounds(300,300,500,500);
    frame.setBackground(Color.blue);

    panel.setBounds(50,50,400,400);
    panel.setBackground(Color.MAGENTA);

    frame.add(panel);
     frame.setVisible(true);

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

3.布局管理器

Frame frame = new Frame("窗口");
Button button1=new Button("button1");
Button button2=new Button("button2");
Button button3=new Button("button3");

frame.setLayout(new FlowLayout(FlowLayout.LEADING));
frame.add(button1);
frame.add(button2);
frame.add(button3);

4.东西南北中

Frame frame = new Frame("窗口");
Button button1=new Button("button1");
Button button2=new Button("button2");
Button button3=new Button("button3");


frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.NORTH);
frame.add(button3);


frame.setBounds(300,300,500,500);
frame.setBackground(Color.blue);
 frame.setVisible(true);

5.表格布局

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

在这里插入图片描述

        Frame frame = new Frame("窗口");
        Panel pane1=new Panel(new BorderLayout());
        Panel pane2=new Panel(new GridLayout(2,1));

        Panel pane3=new Panel(new BorderLayout());
        Panel pane4=new Panel(new GridLayout(2,2));

        frame.setLayout(new GridLayout(2,1));
        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");
        Button button7=new Button("button7");
        Button button8=new Button("butto8");
        Button button9=new Button("button9");
        Button button10=new Button("butto10");
 pane1.add(button1,BorderLayout.EAST);
 pane1.add(button2,BorderLayout.WEST);

 pane2.add(button3);
 pane2.add(button4);
        pane2.add(button7);
        pane2.add(button8);
pane1.add(pane2,BorderLayout.CENTER);
        pane3.add(button5,BorderLayout.EAST);
        pane3.add(button6,BorderLayout.WEST);

        pane4.add(button9);
        pane4.add(button10);
        pane3.add(pane4,BorderLayout.CENTER);

frame.add(pane3);
frame.add(pane1);
        frame.setBounds(300,300,500,500);
        frame.setBackground(Color.blue);
         frame.setVisible(true);

    }

6.监听器

  public static void main(String[] args) {
        Frame frame = new Frame("窗口");
        Button button = new Button();
        MyActionListener my = new MyActionListener();
        button.addActionListener(my);
        frame.add(button);
        frame.setVisible(true);

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


        public void actionPerformed(ActionEvent e) {
            System.out.println("aaa");
        }
    }

7.输入框


```java
```java
public class panel {
    public static void main(String[] args) {
        myframe my  = new myframe();


    }
}

class myframe extends Frame {
    public myframe() {
        TextField textField = new TextField();
        
        myActionListener my=new myActionListener();
        
        add(textField);
        
    textField.addActionListener(my);
    
    setVisible(true);
    setBounds(100,100,100,100);
    }
}

class myActionListener implements ActionListener{


    public void actionPerformed(ActionEvent e) {
        TextField t =(TextField)  e.getSource();
        System.out.println(t.getText());
           t.setText("");
    }
}

8.计算器

public class panel {
    public static void main(String[] args) {
     new cal().loadFrame();

    }
}

class  cal extends Frame{
    TextField textField1=new TextField(10);
    TextField textField2=new TextField(10);
    TextField textField3=new TextField(10);
  public void loadFrame() {
      textField1 = new TextField(10);
      textField2 = new TextField(10);
      textField3 = new TextField(10);
      Button button = new Button("=");

      button.addActionListener(new mycallistener());

      Label label = new Label("+");
      setLayout(new FlowLayout());
      add(textField1);
      add(label);
      add(textField2);
      add(button);
      add(textField3);
      setBounds(500, 500, 500, 500);
      setVisible(true);
  }
    private  class mycallistener implements ActionListener {

          public void actionPerformed(ActionEvent e) {
              int n1=Integer.parseInt(textField1.getText());
              int n2=Integer.parseInt(textField2.getText());
              textField3.setText(""+(n1+n2));
             textField1.setText("");
              textField2.setText("");
          }
    }
}

## 9.画笔 鼠标监听


```java
ackage test;

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

public class panel {
    public static void main(String[] args) {
     new cal("画图");
    }
}
class  cal extends Frame{
    ArrayList points;
    public cal  (String title) {
        setBounds(300,300,300,310);
        setVisible(true);
      points=new ArrayList();
        this.addMouseListener(new mouselistener());
    }
    
    @Override
    public void paint(Graphics g){
        Iterator iterator=points.iterator();
        while(iterator.hasNext()){
            Point point=(Point) iterator.next();
            g.setColor(Color.MAGENTA);
            g.fillOval(point.x,point.y,10,10);
        }
    }
public void addPoint(Point point){
points.add(point);
}

private class  mouselistener extends MouseAdapter{
    @Override
    public void mousePressed(MouseEvent e) {
       cal cal=(cal) e.getSource();
cal.addPoint( new Point(e.getX(),e.getY()));
cal.repaint();
    }
}

}

9.窗口监听

public class panel {
    public static void main(String[] args) {
     new cal("画图");
    }
}
class  cal extends Frame{
    public cal  (String title) {
        setBounds(300,300,300,310);
        setVisible(true);
addWindowListener(new windowListener());
    }


    class  windowListener extends WindowAdapter{

        @Override
        public void windowClosing(WindowEvent e) {
            //setVisible(false  );
            System.exit(0);
        }
    }
}
public class panel {
    public static void main(String[] args) {
     new cal("画图");
    }
}
class  cal extends Frame{
    public cal  (String title) {
        setBounds(300,300,300,310);
        setVisible(true);

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

10.键盘监听

public class panel {
    public static void main(String[] args) {
     new cal();
    }
}
class  cal extends Frame{
    public cal  () {
        setBounds(300,300,300,310);
        setVisible(true);


        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keycode=e.getKeyCode();
                if(keycode==KeyEvent.VK_UP){
                    System.out.print("你按下了0");
                }
            }
        });

      
    }
}

二、Swing

1.JFrame

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


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

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

}

2.弹窗

public class panel extends JFrame {
    public static void main(String[] args) {
 new panel();
}

    public panel(){
        this.setVisible(true);
        this.setBounds(300,300,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container=this.getContentPane();
        container.setLayout(null);

        JButton button=new JButton("点击弹出对话框");
        button.setBounds(100,100,100,100);
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
new mydialog();
    }
});
        container.add(button);
    }
    class mydialog extends JDialog{
        public mydialog() {
            this.setVisible(true);
            this.setBounds(100,100,100,100);
        //    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            Container container=this.getContentPane();
            container.setLayout(null);
            container.add(new Label("芩大撒大苏打"));
        }
    }
}

3.ICON、image

图标

public class frame extends JFrame implements Icon {
    private  int width;
    private  int height;
    public static void main(String[] args) {
 new frame().init();
}

    public frame(){

    }
    public frame( int width, int height){
this.width=width;
        this.height=height;
    }

   public void init(){
        frame framea=new frame(15,15);
   JLabel label= new JLabel("icontesta",framea,SwingConstants.CENTER );
Container container=getContentPane();
container.add(label);
this.setVisible(true);
setBounds(300,330,300,300);
   }


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

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

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

图片

public class frame extends JFrame {
    public static void main(String[] args) {
 new frame();
}

    public frame(){
JLabel label=new JLabel("ImageIcon");
URL url=frame.class.getResource("tx.jpg");

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

Container container=getContentPane();
container.add(label);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(300,300,300,300);

    }
}

Jpanel面板

public class Jpaneldemo extends JFrame {
    public static void main (String args[]){
new Jpaneldemo();
    }

    public Jpaneldemo(){
        Container container=this.getContentPane();

        container.setLayout(new GridLayout(2,1,10,10));

JPanel jPanel=new JPanel(new GridLayout(1,3));

jPanel.add(new Button("button1"));
        jPanel.add(new Button("button1"));
        jPanel.add(new Button("button1"));

        container.add(jPanel);
        this.setVisible(true);
        this.setBounds(300,300,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

文本域Scroll面板


```java
```java
public class Jpaneldemo extends JFrame {
    public static void main (String args[]){
new Jpaneldemo();
    }

    public Jpaneldemo(){
        Container container=this.getContentPane();

       JTextArea jTextArea=new JTextArea(20,50);
       jTextArea.setText("黄i永固了");
JScrollPane jScrollPane=new JScrollPane(jTextArea);
       container.add(jScrollPane);

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

按钮

单选框 分组

public class Jpaneldemo extends JFrame {
    public static void main (String args[]){
new Jpaneldemo();
    }

    public Jpaneldemo(){
  Container container=this.getContentPane();
  
  JRadioButton Button1=new JRadioButton("1");
        JRadioButton Button2=new JRadioButton("2");
        JRadioButton Button3=new JRadioButton("3");

        ButtonGroup buttonGroup=new ButtonGroup();
    buttonGroup.add(Button1);
        buttonGroup.add(Button2);
        buttonGroup.add(Button3);

        container.add(Button1,BorderLayout.NORTH);
        container.add(Button2,BorderLayout.CENTER);
        container.add(Button3,BorderLayout.SOUTH);
  this.setVisible(true);
  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(300,300);
    }
}

复选框

public class Jpaneldemo extends JFrame {
    public static void main (String args[]){
new Jpaneldemo();
    }

    public Jpaneldemo(){
  Container container=this.getContentPane();
  
 JCheckBox checkBox1=new JCheckBox("checkBox1");
        JCheckBox checkBox2=new JCheckBox("checkBox1");
        JCheckBox checkBox3=new JCheckBox("checkBox1");

        container.add(checkBox1,BorderLayout.NORTH);
        container.add(checkBox2,BorderLayout.CENTER);
        container.add(checkBox3,BorderLayout.SOUTH);
  this.setVisible(true);
  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(300,300);
    }
}

下拉框

JComboBox status=new JComboBox();
status.addItem("null");
        status.addItem("1");
        status.addItem("2");
        status.addItem("3");

列表框

public class Jpaneldemo extends JFrame {
    public static void main (String args[]){
new Jpaneldemo();
    }

    public Jpaneldemo(){
  Container container=this.getContentPane();
        Vector content=new Vector();
//String [] content={",",",2","3"};

JList jList=new JList(content);

        content.add("1");
        content.add("1");
        content.add("1");
container.add(jList);

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

文本框

public class Jpaneldemo extends JFrame {
    public static void main (String args[]){
new Jpaneldemo();
    }

    public Jpaneldemo(){
  Container container=this.getContentPane();
JTextField jTextField=new JTextField("sdsadsa");
        JTextField jTextField2=new JTextField("sdsaddsdsadsadsa");


        container.add(jTextField,BorderLayout.SOUTH);
        container.add(jTextField2,BorderLayout.NORTH);
  this.setVisible(true);
  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(300,300);
    }
}

密码框

  Container container=this.getContentPane();

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

container.add(passwordField);

三、贪吃蛇

package kuang;

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

public class startgame  extends  JFrame{
    public static void main (String args[]){
        new startgame();
    }
    public startgame(){
  Container container=this.getContentPane();
container.add(new GamePanel());
  this.setVisible(true);
  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(10,10,900,720);
this.setResizable(false);

    }
}
package kuang;

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

public class Data {


    public static URL headUrl=Data.class.getResource("/static/h.png");
    public  static ImageIcon header=new ImageIcon(headUrl);

    public static URL upURL=Data.class.getResource("/static/s.png");
    public static URL downURL=Data.class.getResource("/static/x.png");
    public static URL zURL=Data.class.getResource("/static/z.png");
    public static URL yURL=Data.class.getResource("/static/y.png");
    public  static ImageIcon up=new ImageIcon(upURL);
    public  static ImageIcon down=new ImageIcon(downURL);
    public  static ImageIcon z=new ImageIcon(zURL);
    public  static ImageIcon y =new ImageIcon(yURL);
    public static URL bodyURL=Data.class.getResource("/static/sz.png");
    public  static ImageIcon body=new ImageIcon(bodyURL);
    public static URL foodURL=Data.class.getResource("/static/sw.png");
    public  static ImageIcon food=new ImageIcon(foodURL);
}
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int [] snakeX=new int[600];
  int [] snakeY=new int[500];
    String fx="r";
    boolean isStart=false;
    boolean isFail=false;
    int foodx;
    int foody;
    Random random=new Random();
    int score =0;
 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 );
}
//画笔
    @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.BLUE);
        g.setFont(new Font("",Font.BOLD,18));
        g.drawString("长度"+length,700,35);
        g.drawString("分数"+score,700,55);

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

        if(fx.equals("r")) {
            Data.y.paintIcon(this, g, snakeX[0], snakeY[0]);
        }else if(fx.equals("L")){
            Data.z.paintIcon(this, g, snakeX[0], snakeY[0]);
        }else if(fx.equals("s")){
            Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
        }else if(fx.equals("x")){
            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==false){
            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("游戏失败,按下空格重新开始",300,300);
        }
    }
    //键盘监听
    public void keyPressed(KeyEvent e) {
 int KeyCode=e.getKeyCode();
 if(KeyCode==KeyEvent.VK_SPACE){
     if(isFail){
         init();
     }else{
         isStart=!isStart;
         repaint();
     }
 }

        if(KeyCode==KeyEvent.VK_UP){
            fx="s";
        }else if(KeyCode==KeyEvent.VK_RIGHT){
            fx="r";
        }else if(KeyCode==KeyEvent.VK_LEFT){
            fx="L";
        }else if(KeyCode==KeyEvent.VK_DOWN){
            fx="x";
        }

    }

//事件监听
    public void actionPerformed(ActionEvent e) {
        if (isStart&&isFail==false) {
            if(snakeX[0]==foodx && snakeY[0]==foody)//失败
            {
                length++;
score=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]>825) {
        snakeX[0] = 25;
    }
}else  if(fx.equals("L")) {
                snakeX[0] = snakeX[0] - 25;
                if (snakeX[0] < 25) { 
                    snakeX[0] = 825;
  } }else if (fx.equals("s")) {
                    snakeY[0] = snakeY[0] - 25;
                    if (snakeY[0] < 75) {
                        snakeY[0] = 625;
      }} else if (fx.equals("x")) {
                        snakeY[0] = snakeY[0] + 25;
                        if (snakeY[0] > 625) {
                            snakeY[0] = 75;
                        }

            }

            for(int i=1;i<length;i++){
                if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                    isFail=true;
                }
            }
            repaint();
        }
        timer.start();//计时器
    }
    
    public void keyReleased(KeyEvent e) {
    }
    public void keyTyped(KeyEvent e) {
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值