iT邦幫忙

2022 iThome 鐵人賽

DAY 20
0
Software Development

大學耍廢的我要學Java翻身系列 第 20

Day20: Swing元件(中)

  • 分享至 

  • xImage
  •  

JPanel元件 => 小型的JFrame

基本功能和JFrame幾乎一樣
JPanel(LayoutManager layout)

常用方法

  1. setBounds(x, y, width, height) => 設定物件在視窗的位置大小,在setLayout(null)才有效
  2. setBackground(Color rgb) => 設定背景色,預設為透明
  3. setBorder(Border bdr) => 設定邊框線條顏色、厚度和樣式
  4. setLayout(LayoutManager layout) => 設定物件版面配置

JRadioButton 圓鈕選項元件 => 選項的圓點確認功能

這個元件只有true和false,而且具有唯一性,只能選擇一個選項,適合只能選擇一個的選單
ButtonGroup() => 管理多個圓鈕選項為一組(只能一個true)

常用方法

  1. add(JRadioButton rdb) => 將圓鈕選項加入群組
  2. remove(JRadioButton rdb) => 將圓鈕選項踢出群組
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

class MyJFrame extends JFrame {
    MyJFrame() {
        setTitle("身家調查");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(700, 400, 300, 200);
        setLayout(null);

        JPanel genderPanel = new JPanel();
        add(genderPanel);
        genderPanel.setBounds(20, 20, 80, 120);
        Border genderLine = BorderFactory.createLineBorder(Color.BLACK);
        genderPanel.setBorder(BorderFactory.createTitledBorder(genderLine, "Gender"));
        genderPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        ButtonGroup genderGroup = new ButtonGroup();
        JRadioButton[] rdb = new JRadioButton[3];
        rdb[0] = new JRadioButton("Male", true);
        rdb[1] = new JRadioButton("Female");
        rdb[2] = new JRadioButton("Other");
        for (int i=0; i<rdb.length; i++){
            genderGroup.add(rdb[i]);
            genderPanel.add(rdb[i]);
        }

        setVisible(true);
    }

}

public class Main {
    public static void main(String[] args) {
        MyJFrame f = new MyJFrame();
    }
}

JCheckBox 選項方塊元件 => 選單裡面方塊勾選的那個

這個元件只有true和false,它不具有唯一性,它可以同時選擇多個,適合複選的表單
JCheckBox(String Text, boolean b) => text:選項名稱, b是否被打勾

常用方法

  1. setSelected(boolean b) => 設定物件是否是否被選取
  2. boolean isSelected() => 判斷方塊被選取
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

class MyJFrame extends JFrame {
    MyJFrame() {
        setTitle("身家調查");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(700, 400, 300, 200);
        setLayout(null);

        JPanel genderPanel = new JPanel();
        add(genderPanel);
        genderPanel.setBounds(20, 20, 80, 120);
        Border genderLine = BorderFactory.createLineBorder(Color.BLACK);
        genderPanel.setBorder(BorderFactory.createTitledBorder(genderLine, "Gender"));
        genderPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        ButtonGroup genderGroup = new ButtonGroup();
        JRadioButton[] rdb = new JRadioButton[3];
        rdb[0] = new JRadioButton("Male", true);
        rdb[1] = new JRadioButton("Female");
        rdb[2] = new JRadioButton("Other");
        for (int i=0; i<rdb.length; i++){
            genderGroup.add(rdb[i]);
            genderPanel.add(rdb[i]);
        }
        JPanel jobPanel = new JPanel();
        add(jobPanel);
        jobPanel.setBounds(130, 20, 120, 120);
        Border jobLine = BorderFactory.createLineBorder(Color.BLACK);
        jobPanel.setBorder(BorderFactory.createTitledBorder(jobLine, "Job"));
        jobPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        JCheckBox[] jck = new JCheckBox[3];
        jck[0] = new JCheckBox("unemployed", true);
        jck[1] = new JCheckBox("Student");
        jck[2] = new JCheckBox("office worker");
        for (int j=0; j< jck.length; j++){
            jobPanel.add(jck[j]);
        }
        setVisible(true);
    }

}

public class Main {
    public static void main(String[] args) {
        MyJFrame f = new MyJFrame();
    }
}

處理選擇元件的事件

這時我們有兩個重要步驟

  1. import java.awt.event.*;
  2. implement ActionListener
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;

class MyJFrame extends JFrame implements ActionListener {
    Border borderLine = BorderFactory.createLineBorder(Color.BLACK);
    JRadioButton[] rdb = new JRadioButton[3];
    JCheckBox[] jck = new JCheckBox[3];
    JLabel hintText = new JLabel("    Fill in your gender and job");
    MyJFrame() {
        setTitle("身家調查");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(700, 400, 350, 250);
        setLayout(null);

        JPanel genderPanel = new JPanel();
        add(genderPanel);
        genderPanel.setBounds(20, 20, 120, 120);
        genderPanel.setBorder(BorderFactory.createTitledBorder(borderLine, "Gender"));
        genderPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        ButtonGroup genderGroup = new ButtonGroup();

        rdb[0] = new JRadioButton("Male", true);
        rdb[1] = new JRadioButton("Female");
        rdb[2] = new JRadioButton("Other");
        for (int i=0; i<rdb.length; i++){
            genderGroup.add(rdb[i]);
            genderPanel.add(rdb[i]);
            rdb[i].addActionListener(this);
        }
        JPanel jobPanel = new JPanel();
        add(jobPanel);
        jobPanel.setBounds(170, 20, 150, 120);
        jobPanel.setBorder(BorderFactory.createTitledBorder(borderLine, "Job"));
        jobPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        jck[0] = new JCheckBox("Unemployed", true);
        jck[1] = new JCheckBox("Student");
        jck[2] = new JCheckBox("Office worker");
        for (int j=0; j< jck.length; j++){
            jobPanel.add(jck[j]);
            jck[j].addActionListener(this);
        }
        add(hintText);
        hintText.setBounds(20, 160, 300, 30);
        hintText.setBorder(borderLine);

        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String result = "    ";
        if (rdb[0].isSelected())
            result += "Sir, your job is ";
        else if (rdb[0].isSelected())
            result += "Miss, your job is ";
        else
            result += "Hi, your job is ";
        if (!(jck[0].isSelected() || jck[1].isSelected() || jck[2].isSelected()))
            result += " ? Please tick the option";
        else {
            String job = "";
            if (jck[0].isSelected()) {
                job += jck[0].getText() + " ";
            }
            if (jck[1].isSelected()) {
                job += jck[1].getText() + " ";
            }
            if (jck[2].isSelected()) {
                job += jck[2].getText();
            }
            result += job;
        }
        hintText.setText(result);
    }
}

public class Main {
    public static void main(String[] args) {
        new MyJFrame();
    }
}


上一篇
Day19: Swing元件(上)
下一篇
Day21: Swing元件(下)
系列文
大學耍廢的我要學Java翻身30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言