1. 程式人生 > 其它 >JAVA學習-GUI程式設計之Swing

JAVA學習-GUI程式設計之Swing

GUI程式設計之Swing

JFrame

package com.jframe.demo01;

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

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

    }
}
class MyNewJFrame{
    public void init(){
        JFrame jFrame = new JFrame();
        Container contentPane = jFrame.getContentPane();//設定容器
        contentPane.setBackground(Color.blue);//設定顏色,必須要設定容器的顏色才會生效
        jFrame.setVisible(true);
        jFrame.setBounds(100,100,400,400);
        JLabel jLabel = new JLabel("歡迎來到我的Java");
        jFrame.add(jLabel);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);//設定標籤居中
        //設定關閉事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

JDialog

package com.jframe.demo02;

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

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

    }
}
///主視窗
class MyJFrame{
    public void init() {
        JFrame jf = new JFrame();
        Container container = jf.getContentPane();
        container.setBackground(Color.blue);
        jf.setBounds(100,100,500,600);
        jf.setVisible(true);
        JButton jButton = new JButton("????????????");
        jButton.setBounds(100,100,100,50);
        JLabel jLabel = new JLabel("你是個啥");
        jLabel.setVisible(true);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(jButton);
        container.add(jLabel);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();

            }
        });
  }
}
///彈窗
class MyDialog extends JDialog{
    public MyDialog() {
        setVisible(true);
        setBounds(100,100,100,100);
        JLabel jLabel = new JLabel("我是彈窗俠");
        Container container = getContentPane();
        container.setBackground(Color.red);
        container.add(jLabel);

    }
}

ImageIcon

package com.jframe.demo03;

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

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

    }
}
class ImageIconDem{
    public void init(){
        JFrame jf = new JFrame();
        Container container = jf.getContentPane();
        jf.setVisible(true);
        jf.setBounds(100,100,600,600);

        JLabel jLabel = new JLabel("imagicon");
        URL url = MyICON.class.getResource("1.jpg");//找到照片的url 利用getResource
        ImageIcon imageIcon = new ImageIcon(url);
        jLabel.setIcon(imageIcon);

        container.add(jLabel);


    }
}

JScrollPane

有滾動條的面板

package com.jframe.demo04;

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

public class Demo01 {
    public static void main(String[] args) {
        new MyText().LoadMyJFrame();
    }

}
class MyText extends JFrame {
    public MyText() {}
    public void LoadMyJFrame(){
        setVisible(true);
        setBounds(100,100,500,500);
        Container container = getContentPane();
        container.setBackground(Color.cyan);
        container.setLayout(new GridLayout(2,1));

        TextArea textArea = new TextArea(20,50);
        textArea.setText("我是你爹");

        JScrollPane jScrollPane = new JScrollPane(textArea);//這個文字框必須直接放在這裡?我也不知道為什麼
        container.add(jScrollPane);

    }
}

按鈕

圖片按鈕

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

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

    }
}
class MyButton extends JFrame {
    public void init(){
        JButton button = new JButton();
        URL url = MyICON.class.getResource("1.jpg");
        Icon icon = new ImageIcon(url);
        Container container = getContentPane();
        container.setLayout(new GridLayout(2,1));
        button.setIcon(icon);
        Panel panel = new Panel();
        container.add(panel);
        panel.add(button);
        panel.setVisible(true);
        setVisible(true);
        setSize(600,600);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

單選框

JRadioButton

單選框不要忘記把按鈕放在group裡面去

package com.jframe.demo05;

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

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

    }
}
class MyButtonTest extends JFrame {
    public void init(){
        JRadioButton radioButton1 = new JRadioButton("1");
        JRadioButton radioButton2 = new JRadioButton("2");
        JRadioButton radioButton3 = new JRadioButton("3");
        Container container = getContentPane();
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);
        container.setLayout(new FlowLayout(FlowLayout.LEFT));
        container.add(radioButton1);
        container.add(radioButton2);
        container.add(radioButton3);

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

多選框

CheckBox

package com.jframe.demo05;

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

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

    }
}
class MyButtonTest extends JFrame {
    public void init(){
        Checkbox checkbox1 = new Checkbox("1");
        Checkbox checkbox2 = new Checkbox("2");
        Checkbox checkbox3 = new Checkbox("3");

        Container container = getContentPane();
        container.setLayout(new FlowLayout(FlowLayout.LEFT));
        container.add(checkbox1);
        container.add(checkbox2);
        container.add(checkbox3);

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

列表

JList 可以放集合進去 實現動態新增

下拉框

JComboBox

可以addItem() 實現下拉框

文字框

JTextField

密碼框

JPasswordField