?? userjframe.java
字號:
//【例6.4】 輸入用戶信息。
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JList;
/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
*/
public class UserJFrame extends JFrame implements ActionListener, ItemListener
{
private int number=1; //編號
private JTextField text_number, text_name; //編號、姓名文本行
private JRadioButton radiobutton_male, radiobutton_female; //性別按鈕
private JComboBox combobox_province, combobox_city; //省份、城市組合框
private JButton button_add; //添加按鈕
private JTextArea text_user; //文本區(qū)
public UserJFrame()
{
super("輸入用戶信息");
this.setSize(360,200);
this.setLocation(300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //單擊窗口關閉按鈕時,結束程序運行
this.setLayout(new GridLayout(1,2)); //網格布局,1行2列,左右分隔窗口
text_user = new JTextArea(); //創(chuàng)建文本區(qū)
this.add(text_user); //占據窗口左半部分
JPanel panel = new JPanel(new GridLayout(6,1)); //面板網格布局,6行1列
this.add(panel); //占據窗口右半部分
text_number = new JTextField("1"); //編號文本行
text_number.setEditable(false); //不可編輯,編號自動生成
panel.add(text_number);
text_name = new JTextField("姓名");
panel.add(text_name);
JPanel panel_radiobutton = new JPanel(new GridLayout(1,2)); //單選按鈕子面板,網格布局,1行2列
panel.add(panel_radiobutton);
ButtonGroup buttongroup = new ButtonGroup(); //按鈕組
radiobutton_male = new JRadioButton("男",true); //創(chuàng)建單選按鈕
buttongroup.add(radiobutton_male); //單選按鈕添加到按鈕組
panel_radiobutton.add(radiobutton_male); //單選按鈕添加到子面板
radiobutton_female = new JRadioButton("女");
buttongroup.add(radiobutton_female);
panel_radiobutton.add(radiobutton_female);
Object province[]={"江蘇省", "浙江省"};
combobox_province = new JComboBox(province); //省份組合框
// combobox_province.setEditable(true); //設置可編輯,默認不可編輯
combobox_province.addItemListener(this); //注冊組合框的選擇事件監(jiān)聽器
panel.add(combobox_province);
Object city[]={"南京市", "蘇州市", "無錫市"};
combobox_city = new JComboBox(city); //城市組合框
panel.add(combobox_city);
button_add = new JButton("添加");
button_add.addActionListener(this);
panel.add(button_add);
this.setVisible(true);
}
public void itemStateChanged(ItemEvent e) //在組合框的下拉列表中選擇數據項時觸發(fā)執(zhí)行
{ //實現ItemListener接口中的方法
if (combobox_province.getSelectedIndex()==0) //在省份組合框中選擇了"江蘇省"
{
combobox_city.removeAllItems(); //清除地區(qū)組合框中原所有內容
combobox_city.addItem("南京市"); //地區(qū)組合框添加數據項
combobox_city.addItem("蘇州市");
combobox_city.addItem("無錫市");
}
if (combobox_province.getSelectedIndex()==1) //選擇了"浙江省"
{
combobox_city.removeAllItems();
combobox_city.addItem("杭州市");
combobox_city.addItem("寧波市");
combobox_city.addItem("溫州市");
}
}
public void actionPerformed(ActionEvent e) //單擊按鈕時觸發(fā)執(zhí)行
{
if (e.getSource() == button_add)
{
String aline="";
aline = number+", "+text_name.getText();
if (radiobutton_male.isSelected()) //指定單選按鈕選中時
aline += ", "+radiobutton_male.getText(); //獲得單選按鈕表示的性別字符串
if (radiobutton_female.isSelected())
aline += ", "+radiobutton_female.getText();
aline += ", "+combobox_province.getSelectedItem(); //獲得組合框選中數據項的字符串
aline += ", "+combobox_city.getSelectedItem();
text_user.append(aline+"\n"); //文本區(qū)添加一行字符串
this.number++; //編號自動加1
text_number.setText(""+this.number);
text_name.setText("姓名");
}
}
public static void main(String arg[])
{
new UserJFrame();
}
}
/*
程序程序設計說明如下。
1、JFrame類調用下列方法,當單擊窗口關閉按鈕時,將自動結束程序運行。
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //單擊窗口關閉按鈕時,結束程序運行
因此,不需要再寫實現WindowListener接口中的windowClosing(WindowEvent e)方法。
2、使用JComboBox代替AWT中的Choice組件,用法完全一樣。
1、JRadioButton使用問題
與AWT中用CheckBox表示單選按鈕,相同點是,都需要一個組Group,這樣在一個邏輯組中的多個按鈕之間可以實現單項選擇。
可以調用getText()方法獲得指定單選按鈕的文本。例如,
aline += ", "+radiobutton_male.getText(); //行
但是,不能判斷單選按鈕是否選中的狀態(tài),即不能像CheckBox調用getState()方法判斷狀態(tài)
if (radiobutton_male.getState()) //不行
所以,程序中只好聲明了一個私有變量sex,當單擊單選按鈕時,自己記住單選按鈕的文本。這算是一個笨辦法。
為什么?怎樣判斷單選按鈕的狀態(tài)?或者不需要判斷狀態(tài)了嗎?
解決,用AbstractButton類中的isSelected()方法。
2、單選按鈕可以注冊單擊事件監(jiān)聽器:
radiobutton_male.addActionListener(this); //注冊單選按鈕的單擊事件監(jiān)聽器
實現方法如下。
public void actionPerformed(ActionEvent e) //單擊按鈕、單擊單選按鈕時觸發(fā)執(zhí)行
{
if (e.getSource() == radiobutton_male || e.getSource() == radiobutton_female)
sex = e.getActionCommand(); //獲得單選按鈕表示的性別字符串
}
3、單選按鈕可以注冊選擇事件監(jiān)聽器:
radiobutton_male.addItemListener(this); //注冊復選框的選擇事件監(jiān)聽器
public void itemStateChanged(ItemEvent e) //在組合框的下拉列表中選擇數據項時觸發(fā)執(zhí)行
{ //選擇單選按鈕時觸發(fā)執(zhí)行
if (e.getSource() == radiobutton_male || e.getSource() == radiobutton_female) //選擇單選按鈕時
sex = ((JRadioButton)e.getSource()).getText(); //獲得單選按鈕表示的性別字符串
}
程序存在問題:
3、JList沒有使用成功,不能像AWT中的List一樣,調用add()方法增加一個數據項,只能創(chuàng)建對象時,給定一個死數據。
以下語句可行。
String data[] = {"南京市","蘇州市","無錫市"};
list_area = new JList(data); //地區(qū)列表框
panel.add(list_area);
public void itemStateChanged(ItemEvent e)
{
if (combobox_province.getSelectedIndex()==0) //選擇組合框中省份數據項時
{
String data[] = {"南京市","蘇州市","無錫市"};
list_area = new JList(data);
}
if (combobox_province.getSelectedIndex()==1)
{
String data[] = {"杭州市","寧波市","溫州市"};
list_area = new JList(data);
}
}
但最終此例沒有使用JList,因為布局問題?,F采用網格布局,每個網格大小相等,所以,省份和地區(qū)使用了兩個組合框。
如果地區(qū)使用列表框,則僅占用一個網格不夠,需要占用多個網格,此時,必須使用GridBagLayout布局,而該布局太復雜,
程序太長,兩句話說不清。
GridBagLayout是最復雜的一種布局管理器,但給出了最靈活的排列控制組件的方式。它能在每個大小不同的矩形格子中精確地
指定組件之間的關系,從而有效地布置組件。
3、JTable也沒有使用成功,只好采用JTextArea
與JList類似,JTable也無法直接動態(tài)增加一行數據,如調用add()方法之類,只能在創(chuàng)建時以一個指定二維數組的值構造。
適用于貸款計算的例題。
構造JList、JTable對象時,可以通過集合類對象,還沒時間仔細研究。
table = new JTable(10,5); //創(chuàng)建表格,6行10列
this.add(table); //占據窗口左半部分
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -