和JTextField的差別就是他可以輸入多行文字,我們也可以用'\n'當作換行符號,或是設定自動換行
JTextArea(String str, int rows, int columns)
=> 設定預設文字和行列數
這個卷軸元件主要就是搭配大型元件的一個容器,讓超出範圍的內容可以用卷軸式去滾動我們的內容
JScrollPane(Component view, int vsb, int hsb)
vsb(垂直捲動軸):
JList<Sring>(Object[] data) // data是指定清單內的選項資料
類似JTextField和JList的結合,但JComboBox只能單選,還有他可以新增項目
JComboBox<String>(Object[] items) // item式下拉選單的選項資料
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
class MyJFrame extends JFrame implements ListSelectionListener, ActionListener, ItemListener {
Border borderLine = BorderFactory.createLineBorder(Color.BLACK);
JRadioButton[] rdb = new JRadioButton[3];
JCheckBox[] jck = new JCheckBox[3];
JLabel hintText = new JLabel(" 請填寫你的基本資料");
JLabel jobText = new JLabel();
String[] city = {"台北", "新北", "桃園", "台中", "台南", "高雄"};
String[] identity = {"學生", "正職", "兼職", "待業"};
JComboBox<String> comboBox = new JComboBox<>(identity);
MyJFrame() {
setTitle("身家調查");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(700, 400, 300, 800);
setLayout(null);
JPanel genderPanel = new JPanel();
add(genderPanel);
genderPanel.setBounds(20, 20, 120, 120);
genderPanel.setBorder(BorderFactory.createTitledBorder(borderLine, "性別"));
genderPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
ButtonGroup genderGroup = new ButtonGroup();
rdb[0] = new JRadioButton("男生", true);
rdb[1] = new JRadioButton("女生");
rdb[2] = new JRadioButton("其他");
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, 80, 120);
jobPanel.setBorder(BorderFactory.createTitledBorder(borderLine, "工作"));
jobPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
// jck[0] = new JCheckBox("待業", true);
// jck[1] = new JCheckBox("學生");
// jck[2] = new JCheckBox("就業中");
// for (int j=0; j< jck.length; j++){
// jobPanel.add(jck[j]);
// jck[j].addActionListener(this);
// }
jobPanel.add(comboBox);
comboBox.addItemListener(this);
add(hintText);
hintText.setBounds(60, 160, 150, 30);
hintText.setBorder(borderLine);
add(jobText);
jobText.setBounds(175, 160, 50, 30);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String result = " ";
if (rdb[0].isSelected())
result += "先生,你的工作是";
else if (rdb[0].isSelected())
result += "小姐,你的工作是";
else
result += "你好,你的工作是";
// if (!(jck[0].isSelected() || jck[1].isSelected() || jck[2].isSelected()))
// result += "甚麼呢,記得填寫問卷";
// 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);
}
@Override
public void valueChanged(ListSelectionEvent e) {
}
@Override
public void itemStateChanged(ItemEvent e) {
String str = (String) comboBox.getSelectedItem();
jobText.setText(str);
}
}
public class Main {
public static void main(String[] args) {
new MyJFrame();
}
}