Java GUI程式設計之Swing
阿新 • • 發佈:2021-06-29
-
視窗(Jframe)
-
面板
Jframe視窗 :
public class JframeTest {
public static void main(String[] args) {
new JframeTest().init();
}
public void init() {
JFrame jFrame = new JFrame();
jFrame.setBounds(200, 200, 400, 400);
//jFrame.setBackground(Color.CYAN); 這樣直接設定背景顏色沒有用
jFrame.setVisible(true);
//設定容器顏色才有用
Container contentPane = jFrame.getContentPane();
contentPane.setBackground(Color.CYAN);
JLabel jLabel = new JLabel("J標籤");
jFrame.add(jLabel);
//設定標籤居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
}
Dialog彈窗 : 彈出的視窗預設有關閉事件
package com.shanqiu24.lession2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogTest extends JFrame {
public DialogTest() {
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//設定關閉
Container container = this.getContentPane();
container.setLayout(null);//絕對定位
JButton jButton = new JButton("點選彈出一個對話方塊");
jButton.setBounds(100, 100, 100, 100);
//點選按鈕時彈出,需要監聽事件
jButton.addActionListener(new ActionListener() {
Icon圖示:
普通圖示:
package com.shanqiu24.lession2;
import javax.swing.*;
import java.awt.*;
public class IconTest extends JFrame implements Icon {
private int width;
private int height;
public IconTest() {
}
public IconTest(int width, int height) {
this.width = width;
this.height = height;
}
圖片圖示:
package com.shanqiu24.lession2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconTest extends JFrame {
public ImageIconTest() {
JLabel jLabel = new JLabel();
URL url = ImageIconTest.class.getResource("test1.png");
ImageIcon imageIcon1 = new ImageIcon(url);
jLabel.setIcon(imageIcon1);
Container container = this.getContentPane();
container.add(jLabel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new ImageIconTest();
}
}
JPanel面板:
scroll(滾動,滾輪):
單選:
package com.shanqiu24.lession2;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonTest1 extends JFrame {
public JButtonTest1() {
Container container = getContentPane();
//單選
JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
URL url = JButtonTest1.class.getResource("test1.png");
ImageIcon icon = new ImageIcon(url);
//分組,一個組裡只能選一個,故為單選
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
container.setLayout(new FlowLayout());
container.add(jRadioButton1);
container.add(jRadioButton2);
container.add(jRadioButton3);
this.setBounds(200,200,500,500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonTest1();
}
}
多選:
public class JButtonTest1 extends JFrame {
public JButtonTest1() {
Container container = getContentPane();
//多選
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");
container.setLayout(new FlowLayout());
container.add(jCheckBox1);
container.add(jCheckBox2);
container.add(jCheckBox3);
this.setBounds(200,200,500,500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonTest1();
}
}
下拉框(Combobox),一般用來選擇一個資訊:
package com.shanqiu24.lession2;
import javax.swing.*;
import java.awt.*;
public class comboboxTest extends JFrame {
public comboboxTest() {
Container container = getContentPane();
//下拉框
JComboBox jComboBox = new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("語文");
jComboBox.addItem("數學");
jComboBox.addItem("英語");
container.add(jComboBox);
this.setBounds(200,200,500,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new comboboxTest();
}
}
列表(Jlist),用來展示資訊,一般可擴容:
package com.shanqiu24.lession2;
import javax.swing.*;
import java.awt.*;
public class ListCombobox extends JFrame{
public ListCombobox() {
Container container = getContentPane();
String[] thing = {"1", "2", "3"};
JList<String> stringJList = new JList<>(thing);
container.add(stringJList);
this.setBounds(200,200,500,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new ListCombobox();
}
}
文字框:
TextField : 單行
TextArea :可換行