Swing
-
Swing 是一個為Java設計的GUI工具包。
-
Swing是JAVA基礎類的一部分。
-
Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。
-
Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平台運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統默認的特有主題),然而不是真的使用原生平台提供的設備,而是僅僅在表面上模仿它們。這意味着你可以在任意平台上使用JAVA支持的任意面板。輕量級組件的缺點則是執行速度較慢,優點就是可以在所有平台上采用統一的行為。
界面
package com.ar.chapter03;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame02().init();
}
}
class MyJFrame02 extends JFrame{
public void init(){
this.setBounds(100,100,400,200);
this.setVisible(true);
JLabel jLabel=new JLabel("阿嬈");
this.add(jLabel);
//文本居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container container= this.getContentPane();
container.setBackground(Color.red);
}
}
package com.ar.chapter03;
import javax.swing.*;
import java.awt.*;
public class JFrameDome {
//初始化
public void init(){
JFrame jFrame=new JFrame("Swing窗口");
jFrame.setVisible(true);
jFrame.setBounds(100,100,400,200);
//設置文字
JLabel jLabel=new JLabel("ar");
jFrame.add(jLabel);
//容器實例化
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDome().init();
}
}
彈窗
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialongDemo extends JFrame {
//
public DialongDemo(){
this.setVisible(true);
this.setSize(700,1500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//放入新窗口
Container container = this.getContentPane();
//絕對布局
container.setLayout(null);
//按鈕
JButton button = new JButton("彈出窗口");
button.setBounds(30,30,200,50);
//監聽事件,點擊是彈出一個彈窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//彈窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialongDemo();
}
}
//彈窗的窗口
class MyDialogDemo extends JDialog {
public MyDialogDemo(){
this.setVisible(true);
this.setSize(100,100);
this.setBounds(100,100,500,500);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = new Container();
container.setLayout(null);
container.add(new Label("阿嬈"));
}
}
點擊按鈕,彈出彈窗
標簽
label
new label("阿嬈")
圖標Icon
import javax.swing.*;
import java.awt.*;
//圖標是個接口,需要實現類,Frame繼承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
//無參構造
public IconDemo() {
}
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public void init() {
IconDemo iconDemo = new IconDemo(15, 15);
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.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;
}
public static void main(String[] args) {
new IconDemo().init();
}
}
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class IconDemo0 extends JFrame {
public IconDemo0() {
//獲取圖片地址
JLabel label = new JLabel("ImageIcon");
URL url = IconDemo0.class.getResource("某個圖片的位置");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo0();
}
}
面板
JPanel
import javax.swing.*;
import java.awt.*;
public class Panel extends JFrame
{
public Panel() {
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("1"));
panel2.add(new JButton("1"));
panel3.add(new JButton("1"));
panel3.add(new JButton("1"));
panel4.add(new JButton("1"));
panel4.add(new JButton("1"));
panel4.add(new JButton("1"));
panel4.add(new JButton("1"));
panel4.add(new JButton("1"));
panel4.add(new JButton("1"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(200, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Panel();
}
}
import javax.swing.*;
import java.awt.*;
public class Panel02 extends JFrame{
public Panel02() {
Container container = this.getContentPane();
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("JTextArea");
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100, 100, 300, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Panel02();
}
}
按鈕
圖片按鈕
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
URL url = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(url);
//把圖標放到按鈕上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("圖片按鈕");
container.add(button);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
單選按鈕
import javax.swing.*;
import java.awt.*;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container container = this.getContentPane();
//單選框
JRadioButton jRadioButton1 = new JRadioButton("01");
JRadioButton jRadioButton2 = new JRadioButton("02");
JRadioButton jRadioButton3 = new JRadioButton("03");
//由於單選框只能選擇一個,分組一個組中只能
// 剛掉選一個可以多選啦
// ButtonGroup buttonGroup = new ButtonGroup();
// buttonGroup.add(jRadioButton1);
// buttonGroup.add(jRadioButton2);
// buttonGroup.add(jRadioButton3);
container.add(jRadioButton1, BorderLayout.CENTER);
container.add(jRadioButton2, BorderLayout.NORTH);
container.add(jRadioButton3, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
多線按鈕
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container container = this.getContentPane();
Checkbox checkbox1 = new Checkbox("01");//單詞
Checkbox checkbox2 = new Checkbox("02");
container.add(checkbox1, BorderLayout.NORTH);
container.add(checkbox2, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
列表
import javax.swing.*;
import java.awt.*;
class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("阿嬈就很漂亮");
status.addItem("阿嬈太漂亮了");
pack();
container.add(status);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
列表框
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = this.getContentPane();
//生成列表的內容
//String[] contents = {"1", "2", "3", "4"};
//列表中需要放入內容
JList jList = new JList(contents);
//container.add(jList);
container.add("某某1");
container.add("某某2");
container.add("某某3");
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
-
應用場景
-
選自地區,或一些單個選項
-
列表,展示信息,一般是動態擴容
-
文本框