1. 程式人生 > 其它 >Java GUI程式設計之Swing

Java GUI程式設計之Swing

Java GUI程式設計之Swing

  • 視窗(Jframe)

  • 面板

Jframe視窗 : 用法於Frame類似,有些屬性需要new Container去設定,有些東西需要新增到容器中。

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() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
container.add(jButton);
}

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

class MyDialog extends JDialog {
public MyDialog() {
this.setVisible(true);
this.setBounds(100, 100, 300, 300);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("你好嗎"));
}
}

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;
}

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

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

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

public void init() {
//圖示可放在標籤上,按鈕上
IconTest iconTest = new IconTest(20, 20);
JLabel jLabel = new JLabel("IconTest", iconTest, SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

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

圖片圖示:

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 :可換行

JpasswordField : 不可見