基本功能和JFrame幾乎一樣
JPanel(LayoutManager layout)
這個元件只有true和false,而且具有唯一性,只能選擇一個選項,適合只能選擇一個的選單
ButtonGroup() => 管理多個圓鈕選項為一組(只能一個true)
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();
}
}
這個元件只有true和false,它不具有唯一性,它可以同時選擇多個,適合複選的表單
JCheckBox(String Text, boolean b) => text:選項名稱, b是否被打勾
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();
}
}
這時我們有兩個重要步驟
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();
}
}