太強了!所有GUI編程筆記裏面我願稱你爲最強,建議先收藏再看! 簡介 AWT 組件和容器 Swing 最後

簡介

Gui的核心技術:Swing AWT

因爲界面不美觀。

需要jre環境!

爲什麼我們要學習?

   1. 可以寫出自己心中想要的一些小工具
   2. 工作時候,也可能需要維護到swing界面,概率極小!
   3. 瞭解MVC架構,瞭解監聽!

AWT

組件和容器

Frame

public class newawttest {
    public static void main(String[] args) {
        //創建一個Frame類
        Frame frame = new Frame("這是個測試");

        //設置窗口可見化
        frame.setVisible(true);

        //設置窗口尺寸
        frame.setSize(400,300);

        //設置背景顏色
        frame.setBackground(new Color(203, 33, 33));

        //設置初始位置
        frame.setLocation(200,200);

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

    }


}

import java.awt.*;

public class MyawtText_2 {
    public static void main(String[] args) {
        Newawt newawt1 = new Newawt(100,100,200,200,Color.BLUE);
        Newawt newawt2 = new Newawt(300,100,200,200,Color.YELLOW);
        Newawt newawt3 = new Newawt(100,300,200,200,Color.RED);
        Newawt newawt4 = new Newawt(300,300,200,200,Color.PINK);
    }
}

class Newawt extends Frame {
    static int id = 0;
    public Newawt(int x,int y,int w,int h,Color color) {
        super("窗口"+(++id));
        setVisible(true);
        setBounds(x,y,w,h);
        setBackground(color);

    }
}


面板Panel

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

public class newawttest {
    public static void main(String[] args){
        Frame frame=new Frame();
        Panel panel=new Panel();
        Panel panel2 = new Panel();
        //設置佈局
        frame.setLayout(null);
        //座標
        frame.setBounds(300,300,500,500);
//        frame.setBounds(0,0,0,0);
        frame.setBackground(new Color(40,161,35));

        panel2.setBounds(0,0,200,200);
        panel2.setBackground(new Color(255, 241, 190));
        frame.add(panel2);
        //paneL 設置座標,相對於frame
        panel.setBounds(0,0,300,300);
        panel.setBackground(new Color(193,15,60));
        //frame.add(panel) ;
        frame.add(panel);


        //窗口可視化
        frame.setVisible(true);

        //適配器模式,把原有的接口換成繼承類
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

}

佈局管理器

流式佈局

import java.awt.*;


public class newawttest {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //按鈕
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        //設置流式佈局
        frame.setLayout(new FlowLayout());        //默認居中
//        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
//        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//        frame.setLayout(new FlowLayout(FlowLayout.LEADING));
//        frame.setLayout(new FlowLayout(FlowLayout.TRAILING));


        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setSize(400,400);
        frame.setVisible(true);


    }
}

東西南北中

import java.awt.*;


public class newawttest {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //按鈕
        Button west = new Button("West");
        Button East = new Button("East");
        Button North = new Button("North");
        Button South = new Button("South");
        Button Center = new Button("Center");

        frame.add(west,BorderLayout.WEST);
        frame.add(East,BorderLayout.EAST);
        frame.add(North,BorderLayout.NORTH);
        frame.add(South,BorderLayout.SOUTH);
        frame.add(Center,BorderLayout.CENTER);

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


表格佈局

import java.awt.*;


public class newawttest {
   public static void main(String[] args) {
       Frame frame = new Frame();
       //按鈕
       Button but1 = new Button("but1");
       Button but2 = new Button("but2");
       Button but3 = new Button("but3");
       Button but4 = new Button("but4");
       Button but5 = new Button("but5");
       Button but6 = new Button("but6");


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

       frame.add(but1);
       frame.add(but2);
       frame.add(but3);
       frame.add(but4);
       frame.add(but5);
       frame.add(but6);

       frame.pack();   //Java語法,自動分佈大小
       frame.setVisible(true);


   }
}


練習題

import java.awt.*;


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

        frame.setVisible(true);
        frame.setBounds(400,300,300,300);
//        frame.pack();
        frame.setBackground(Color.BLUE);
        frame.setLayout(new GridLayout(2, 1));

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

        //上
        panel3.add(new Button("East-1"),BorderLayout.EAST);
        panel3.add(new Button("West-1"),BorderLayout.WEST);
        panel1.add(new Button("top"));
        panel1.add(new Button("down"));
        panel3.add(panel1,BorderLayout.CENTER);

        //下
        panel4.add(new Button("East-2"),BorderLayout.EAST);
        panel4.add(new Button("West-2"),BorderLayout.WEST);
        panel2.add(new Button("top-1"));
        panel2.add(new Button("top-1"));
        panel2.add(new Button("down-1"));
        panel2.add(new Button("down-2"));
        panel4.add(panel2,BorderLayout.CENTER);

        frame.add(panel3);
        frame.add(panel4);
        

    }
}

事件監聽

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


public class newawttest {
    public static void main(String[] args) {
        //按下按鈕,觸發一些事件
        Frame frame = new Frame();
        Button button = new Button("button");

        //因爲addActionListener()需要一個ActionListener,所以需要構造一個ActionListener。
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

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

        frame.setVisible(true);

        ExitWindows(frame);

        

    }



    private static void ExitWindows(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("Hello,World!");
    }
}


多個按鈕共享一個事件

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 newawttest {
    public static void main(String[] args) {
        //按下按鈕,觸發一些事件
        Frame frame = new Frame("開始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        // 可以顯示的定義觸發會返回的命令,如果不顯示定義,則會走默認的值!
        // 可以多個按鈕只寫一個臨聽類
        button1.addActionListener(new MyAction());
        button2.addActionListener(new MyAction());

        button2.setActionCommand("This is stop!");

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.pack();

        ExitWindows(frame);

    }



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

//關閉窗體事件
class MyAction implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("輸出:msg=>"+e.getActionCommand());//獲得按鈕信息
    }
}


輸入框 TextField 監聽

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

public class TextFieldaaa {
    public static void main(String[] args) {
        //啓動
        new MyFrame();
        new newawttest().ExitWindows(new MyFrame());

    }
}

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

        //監聽這個文本框輸入的文字
        MyAction2 myAction2 = new MyAction2();
        //按下Enter就會觸發輸入框的事件
        textField.addActionListener(myAction2);

        textField.setEchoChar('*');     //設置替換編碼
        pack();
        setVisible(true);
    }
}

class MyAction2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();      //獲得一些資源,返回一個對象
        System.out.println(field.getText());                //得到輸入框文本
        field.setText("");                  //每次輸入完清空
    }
}

簡易計算器,組合和內部類

//初代碼
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 TextCalc {
    public static void main(String[] args) {
        new CalcLater();
    }
}
class CalcLater extends Frame {
    public CalcLater(){
        //三個文本框
        TextField field1 = new TextField(10);
        TextField field2 = new TextField(10);
        TextField field3 = new TextField(20);
        //一個按鈕
        Button button = new Button("=");
        button.addActionListener(new MyCalcLaterListener(field1,field2,field3));
        //一個標籤
        Label label = new Label("+");
        //佈局
        setLayout(new FlowLayout());

        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);

        setVisible(true);
        pack();

//        ExitWindows(new CalcLater());

    }

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

class MyCalcLaterListener implements ActionListener {
    private TextField num1,num2,num3;

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

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


//改造完全面向對象
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 TextCalc {
    public static void main(String[] args) {
        new CalcLater().LoadFrame();
    }
}
class CalcLater extends Frame {

    TextField num1,num2,num3;
    Button button;
    Label label;

    //加載計算器
    public void LoadFrame(){
        //三個文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //一個按鈕
        button = new Button("=");
        button.addActionListener(new MyCalcLaterListener(this));
        //一個標籤
        label = new Label("+");

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

        setVisible(true);
        pack();

        this.ExitWindows(this);
    }

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

//監聽類
class MyCalcLaterListener implements ActionListener {
    CalcLater calcLater = null;
    public MyCalcLaterListener(CalcLater calcLater) {
        this.calcLater = calcLater;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //傳三個數進來
        int n1 = Integer.parseInt(calcLater.num1.getText());
        int n2 = Integer.parseInt(calcLater.num2.getText());
        //將運算結果放到第三個框
        calcLater.num3.setText(""+(n1+n2));
        //將前兩個數清空
        calcLater.num1.setText("");
        calcLater.num2.setText("");
    }
}


內部類最大的好處,就是可以暢通無阻地訪問外部類的屬性和方法

內部類寫法↓

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 CalcLater extends Frame {

    public static void main(String[] args) {
        new CalcLater().LoadFrame();
    }
    
    TextField num1,num2,num3;
    Button button;
    Label label;

    //加載計算器
    public void LoadFrame(){
        //三個文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //一個按鈕
        button = new Button("=");
        button.addActionListener(new MyCalcLaterListener());
        //一個標籤
        label = new Label("+");

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

        setVisible(true);
        pack();

        this.ExitWindows(this);

    }

    private class MyCalcLaterListener 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("");
        }
    }
    public void ExitWindows(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

畫筆

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

public class TestMyPrints {
    public static void main(String[] args) {
        new MyPrints().LoadFrame();
    }
}

class MyPrints extends Frame{

    public void LoadFrame(){
        setVisible(true);
        setBounds(200,200,600,500);
        
    }
    
    @Override
    public void paint(Graphics g) {
        //super.paint(g);

        //設置畫筆顏色
        g.setColor(Color.CYAN);
        //填充一個實心圓
        g.fillOval(100,100,100,100);

        //畫筆用完還原爲最初的顏色
        g.setColor(Color.BLACK);     

    }

}

鼠標監聽

//畫板實例
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;

public class MorseListenerTest {


    public static void main(String[] args) {
        new MyFrame("畫圖");
    }
}

class MyFrame extends Frame{
    //需要畫筆,需要監聽鼠標,需要集合存點
    ArrayList points ;

    public MyFrame(String title){
        super(title);
        setBounds(200,200,600,400);
        setVisible(true);

        //存鼠標的點
        points = new ArrayList();
        //鼠標監聽器
        this.addMouseListener(new MyListener());
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    //把點添加進去
    public void addPoint(Point point){
        points.add(point);
    }


    @Override
    public void paint(Graphics g) {
        //需要監聽鼠標事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }

    //鼠標監聽
    private class MyListener extends MouseAdapter{
        //鼠標點擊

        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame = (MyFrame)e.getSource();
            //這裏我們點擊的時候就會產生一個點
            myFrame.addPoint(new Point(e.getX(),e.getY()));

            //每次點擊鼠標需要重畫一遍
            myFrame.repaint();
        }
    }
}

窗口監聽

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

public class TestWindows {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends Frame{
    public MyFrame(){

        setVisible(true);
        setBackground(Color.CYAN);
        setBounds(200,200,400,400);


    }

    private class MyWindowsListenter extends WindowAdapter{
        //關閉窗口
        @Override
        public void windowClosing(WindowEvent e) {
            super.windowClosing(e);
        }
        //窗口被激活
        @Override
        public void windowActivated(WindowEvent e) {
            super.windowActivated(e);
        }
    }


}



鍵盤監聽


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

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

class KeyFrame extends Frame{
    public KeyFrame(){
        setBackground(Color.blue);
        setBounds(100,100,200,400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //獲取輸入鍵的編碼
                System.out.println(e.getKeyCode());
            }
        });
    }
}

Swing

窗口,面板

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

public class JFrameDemo {

    //init();   初始化
    public void init(){
        JFrame jFrame = new JFrame("這是一個JFrame窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(100,100,200,200);

        //獲得一個容器
        Container contentPane = jFrame.getContentPane();
        contentPane.setBackground(Color.YELLOW);

        JLabel jLabel = new JLabel("歡迎來到笨蛋的窗口界面");
        //設置文本居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        jFrame.add(jLabel);
        //關閉事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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


彈窗

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

public class DialogDemo extends JFrame {
    public DialogDemo(){
        this.setVisible(true);
        this.setBounds(100,100,700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //Jframe 放東西,容器
        Container contentPane = this.getContentPane();
        //絕對佈局
        contentPane.setLayout(null);

        contentPane.setBackground(Color.lightGray);

        //按鈕
        JButton button = new JButton("這是個按鈕");
        button.setBounds(30,30,100,100);
        button.setBackground(Color.BLUE);
        //點擊這個按鈕的時候彈出一個彈窗
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //彈窗
                new MyDiolog();
            }
        });
        contentPane.add(button);
    }

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

}


class MyDiolog extends JDialog{
    public MyDiolog() {
        this.setVisible(true);
        this.setBounds(100,100,300,300);
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//        默認有關閉事件

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

        JLabel label = new JLabel("這是個彈窗");
        label.setBounds(10,10,400,400);
        container.add(label);
    }
    
}

標籤

package com.Miotsuki.SwingTest;

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

public class ImageIconDemo extends JFrame{
    public ImageIconDemo(){


        //獲取圖片地址
        JLabel label = new JLabel("");
        URL url = ImageIconDemo.class.getResource("蛋糕.PNG");
        ImageIcon imageIcon = new ImageIcon(url);

        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = this.getContentPane();
        container.setBackground(new Color(196, 183, 255));
        container.add(label);

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

    }

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


面板

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


public class JPanelDemo2 extends JFrame {
    public JPanelDemo2() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2, 1, 10, 10));
        //後面的參數的意想,間原
        JPanel panel1 = new JPanel(new GridLayout(1, 3));
        JPanel panel2 = new JPanel(new GridLayout(1, 2));
        JPanel panel3 = new JPanel(new GridLayout(2, 1));
        JPanel panel4 = new JPanel(new GridLayout(3, 2));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));

        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

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

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

JScrollPanel

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

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container contentPane = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 20);
        textArea.setText("歡迎使用:");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        contentPane.add(scrollPane);
        
        this.setVisible(true);
        this.setBounds(100,100,300,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

}

按鈕

普通按鈕

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

public class JButtonDemo1 extends JFrame {
    public JButtonDemo1(){
        //容器
        Container container = this.getContentPane();
        
        //獲取圖片位置
        URL resource = JButtonDemo1.class.getResource("蛋糕.png");
        Icon icon = new ImageIcon(resource);
        
        //設置一個按鈕
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("這是個按鈕,快按吧");
        
        container.add(button);
        setBounds(100,100,500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

單選按鈕

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

public class JButtonDemo2 extends JFrame {
    public JButtonDemo2(){
        //容器
        Container container = this.getContentPane();
        //獲取圖片位置
        URL resource = JButtonDemo1.class.getResource("蛋糕.png");
        Icon icon = new ImageIcon(resource);

        JRadioButton button1 = new JRadioButton("JRadioButton1");
        JRadioButton button2 = new JRadioButton("JRadioButton2");
        JRadioButton button3 = new JRadioButton("JRadioButton3");

        ButtonGroup group = new ButtonGroup();
        group.add(button1);
        group.add(button2);
        group.add(button3);

        container.add(button1,BorderLayout.NORTH);
        container.add(button2,BorderLayout.CENTER);
        container.add(button3,BorderLayout.SOUTH);


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

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


複選按鈕

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

public class JButtonDemo3 extends JFrame {
    public JButtonDemo3(){
        //容器
        Container container = this.getContentPane();
        //獲取圖片位置
        URL resource = JButtonDemo1.class.getResource("蛋糕.png");
        Icon icon = new ImageIcon(resource);

        //多選按鈕
        JCheckBox checkBox1 = new JCheckBox("JCheckBox1");
        JCheckBox checkBox2 = new JCheckBox("JCheckBox2");

        container.add(checkBox1,BorderLayout.NORTH);
        container.add(checkBox2,BorderLayout.SOUTH);


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

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


列表

下拉菜單

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

public class ComboBosDemo01 extends JFrame{
    public ComboBosDemo01(){
        //容器
        Container container = this.getContentPane();

        JComboBox comboBox = new JComboBox();
        comboBox.addItem(null);
        comboBox.addItem("聯繫人");
        comboBox.addItem("好友");
        comboBox.addItem("黑名單");

        container.add(comboBox);

        //可視化,窗口大小,關閉事件
        setVisible(true);
        setBounds(100,100,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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




列表框

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

public class ComboBosDemo02 extends JFrame {
    public ComboBosDemo02(){
        //容器
        Container container = this.getContentPane();

//        String[] contents = {"1","2","3"};

        Vector contents = new Vector();
        JList jlist = new JList(contents);

        contents.add("zhangsan");
        contents.add("lisi");
        contents.add("wangwu");


        container.add(jlist);
        //可視化,窗口大小,關閉事件
        setVisible(true);
        setBounds(100,100,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

文本框

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

public class TextTestDemo01 extends JFrame {
    public TextTestDemo01(){
        //容器
        Container container = this.getContentPane();
        //文本框
        JTextField textField = new JTextField("Hello",20);
        JTextField textField2 = new JTextField("World");
        //密碼框
        JPasswordField passwordField = new JPasswordField();

        container.add(textField,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);
        container.add(passwordField,BorderLayout.CENTER);

        //可視化,窗口大小,關閉事件
        setVisible(true);
        setBounds(100,100,500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

文本域

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

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container contentPane = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 20);
        textArea.setText("歡迎使用:");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        contentPane.add(scrollPane);
        
        this.setVisible(true);
        this.setBounds(100,100,300,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

} 
1(){
//容器
Container container = this.getContentPane();
//文本框
JTextField textField = new JTextField(“Hello”,20);
JTextField textField2 = new JTextField(“World”);
//密碼框
JPasswordField passwordField = new JPasswordField();

    container.add(textField,BorderLayout.NORTH);
    container.add(textField2,BorderLayout.SOUTH);
    container.add(passwordField,BorderLayout.CENTER);

    //可視化,窗口大小,關閉事件
    setVisible(true);
    setBounds(100,100,500,500);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new TextTestDemo01();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
}


- 文本域

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

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container contentPane = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 20);
        textArea.setText("歡迎使用:");

        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        contentPane.add(scrollPane);
        
        this.setVisible(true);
        this.setBounds(100,100,300,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

} 

最後

在文章的最後作者爲大家整理了很多資料!包括java核心知識點+全套架構師學習資料和視頻+一線大廠面試寶典+面試簡歷模板+阿里美團網易騰訊小米愛奇藝快手嗶哩嗶哩面試題+Spring源碼合集+Java架構實戰電子書等等!
歡迎關注公衆號:前程有光,領取!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章