java復選框
① java澶嶉夋嗘庝箞娣誨姞浜嬩歡
鍙浠ラ氳繃瀹炵幇ItemListener鎺ュ彛錛屽備笅
{
JCheckBoxchin;
publicJcheckBoxtest(){
super(newBorderLayout());
//鍒涘緩澶嶉夋嗘寜閿錛屽苟璁劇疆蹇鎹烽敭錛屽拰閫夊畾
chin=newJCheckBox("chin");
//璁劇疆涓涓猵anel錛屽皢澶嶉夋嗘斁鍏ュ悓涓涓猵anel
JPanelcheckPanel=newJPanel(newGridLayout(0,1));
checkPanel.add(chin);
//娣誨姞澶嶉夋嗙殑鐩戝惉浜嬩歡
chin.addItemListener(this);
}
//鎺ュ彈澶勭悊澶嶉夋嗙偣鍑諱簨浠
@Override
publicvoiditemStateChanged(ItemEvente){
//鑾峰彇鏀瑰彉鐨勫嶉夋寜閿
Objectsource=e.getItemSelectable();
charc='-';
if(source==chin){
//..榪涜屽勭悊
}
}
}
② JAVA編寫一個窗口(frame),要求窗口中有文本框,按鈕,標簽,單選框,復選框,
import javax.swing.*;
import java.awt.*;
public class test extends JFrame{
public test(){
JButton button; //按鈕
JLabel label; //標簽
JComboBox combobox;//下拉菜單
JCheckBox checkbox;//復選框
JRadioButton radiobutton;//單選框
JTextField textfield;//文本框
button = new JButton("按鈕");
label = new JLabel("標簽:");
checkbox = new JCheckBox("復選框一");
radiobutton = new JRadioButton("單選框一");
combobox = new JComboBox();
textfield = new JTextField(100);
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
c.add(button);
c.add(label);
c.add(checkbox);
c.add(radiobutton);
combobox.addItem("1");
combobox.addItem("2");
c.add(combobox);
c.add(textfield);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
// TODO 自動生成方法存根
test mytest = new test();
}
}
③ java swing濡備綍鍦↗Table涓涓鍗曞厓鏍兼坊鍔犲氫釜澶嶉夋
java swing涓鍦╦Table涓娣誨姞澶氫釜澶嶉夋嗙殑鏂瑰紡濡備笅錛
importjava.awt.Component;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.*;
importjavax.swing.table.*;
{
//浣滀負嫻嬭瘯鐨刴ain鏂規硶
publicstaticvoidmain(String[]args){
newMyFirstJFrame().setVisible(true);
}
/**
*鏋勯犳柟娉
*/
publicMyFirstJFrame(){
InitialComponent();
}
/**
*鍒濆嬪寲緇勪歡鐨勬柟娉
*/
privatevoidInitialComponent(){
//璁劇疆紿椾綋鍙傛暟
//璁劇疆甯冨矓妯″紡
setLayout(null);
//璁劇疆紿椾綋澶у皬
setSize(480,360);
//璁劇疆紿椾綋灞呬腑錛堥潪甯歌勬柟娉曪級
setLocationRelativeTo(null);
//鍏抽棴紿椾綋閫鍑虹▼搴
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//鍒濆嬪寲闈㈡澘
panel=newJPanel();
panel.setSize(this.getWidth(),this.getHeight());
panel.setLocation(0,0);
panel.setLayout(null);
//鍒濆嬪寲琛ㄦ牸
table=newJTable(newDefaultTableModel(newObject[][]{{"絎涓琛"},{"絎浜岃"},{"絎涓夎"},{"絎鍥涜"}},newString[]{"嫻嬭瘯琛1","嫻嬭瘯琛2"}){
/*(non-Javadoc)
*閲嶅啓鏂規硶錛屽垽鏂琛ㄥ崟鍏冩牸鏄鍚﹀彲緙栬緫
*鍙浠ラ氳繃row鍜宑olumn緔㈠紩鍒ゆ柇鏌愪竴涓鍗曞厓鏍兼槸鍚﹀彲緙栬緫
*姝ゅ勮句負閮戒笉鍙緙栬緫
*@seejavax.swing.table.DefaultTableModel#isCellEditable(int,int)
*/
@Override
publicbooleanisCellEditable(introw,intcolumn){
returnfalse;
}
});
//寮濮嬪悜琛ㄦ牸涓娣誨姞澶嶉夋嗭紙娉ㄦ剰錛氭ょず渚嬭緝涓虹畝鍗曪紝緙虹渷寰堝氬垽鏂錛屼篃娌℃湁鍔ㄦ佷唬鐮佹敮鎸侊級
//閫氳繃璁劇疆鍒楁覆鏌
//鏂規硶涓錛氱洿鎺ユ柟寮浣跨敤TableColumn鐨剆etCellRenderer鏂規硶錛堟帹鑽愶級
//姝ゆ柟娉曞彲浠ヨ劇疆鏌愪竴鍒楃殑娓叉煋錛堝嵆浣跨敤鏌愪竴涓緇勪歡--鍗蟲帶浠舵潵鏄劇ず鍗曞厓鏍兼暟鎹錛
table.getColumnModel().getColumn(1).setCellRenderer(newTableCellRenderer(){
/*(non-Javadoc)
*姝ゆ柟娉曠敤浜庡悜鏂規硶璋冪敤鑰呰繑鍥炴煇涓鍗曞厓鏍肩殑娓叉煋鍣錛堝嵆鏄劇ず鏁版嵁鐨勭粍寤--鎴栨帶浠訛級
*鍙浠ヤ負JCheckBoxJComboBoxJTextArea絳
*@seejavax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,java.lang.Object,boolean,boolean,int,int)
*/
@Override
(JTabletable,
Objectvalue,booleanisSelected,booleanhasFocus,
introw,intcolumn){
//鍒涘緩鐢ㄤ簬榪斿洖鐨勬覆鏌撶粍浠
JCheckBoxck=newJCheckBox();
//浣垮叿鏈夌劍鐐圭殑琛屽瑰簲鐨勫嶉夋嗛変腑
ck.setSelected(isSelected);
//璁劇疆鍗曢塨ox.setSelected(hasFocus);
//浣垮嶉夋嗗湪鍗曞厓鏍煎唴灞呬腑鏄劇ず
ck.setHorizontalAlignment((int)0.5f);
returnck;
}});
//鏂規硶浜岋細鍏堣劇疆鍒楃紪杈戝櫒錛岀劧鍚庤劇疆鍗曞厓鏍兼覆鏌
//璁劇疆鍒楃紪杈戝櫒
//鍦ㄤ互澶嶉夋嗕負瀵硅薄璁劇疆鍒楃紪杈戝櫒鏃訛紝蹇呴』淇濊瘉璇ュ垪鑳藉熻緙栬緫錛屽惁鍒欐棤娉曟洿鏀圭姸鎬
//錛堟ゆラゅ彲浠ョ渷鐣ワ紝鐪佺暐鏃朵笉瑕佸繕璁板皢鍒楄句負涓嶅彲緙栬緫錛
//table.getColumnModel().getColumn(1).setCellEditor(newDefaultCellEditor(newJCheckBox()));
//璁劇疆鍗曞厓鏍兼覆鏌擄紙榪欓噷鏄璁劇疆琛ㄦ牸綰у埆鐨勬覆鏌擄級
/*table.setDefaultRenderer(Object.class,newTableCellRenderer(){
@Override
(JTabletable,
Objectvalue,booleanisSelected,booleanhasFocus,
introw,intcolumn){
//鍒ゆ柇鏄鍚︿負闇瑕佹覆鏌撶殑鍒
if(column==1){
//鍜屾柟娉曚竴鍩烘湰涓鑷
JCheckBoxbox=newJCheckBox();
box.setSelected(isSelected);
//璁劇疆鍗曢塨ox.setSelected(hasFocus);
box.setHorizontalAlignment((int)CENTER_ALIGNMENT);//0.5f
returnbox;
}
//濡傛灉涓嶆槸闇瑕佹覆鏌撶殑鍒楋紝灝佽呮枃鏈鍩熸樉紺烘暟鎹
returnnewJTextArea(value.toString());
}});*/
//鍦ㄥ氶夋槸闇瑕佹寜浣廋trl閿鎴栬呴紶鏍囨寜浣忔嫋榪囪繛緇鐨勯渶瑕侀変腑鐨勮岋紝搴旇ョ粰鐢ㄦ埛璇存槑
//絎涓縐嶆柟娉曟槸琚鎺ㄨ崘鐨勶紝鍥犱負瀹冨叿鏈夐変腑鐨勯珮浜鏄劇ず錛岀晫闈㈣兘鏇村姞鍙嬪ソ
table.setSize(panel.getWidth(),panel.getHeight()-90);
table.setLocation(0,0);
btn=newJButton("Test");
btn.setSize(80,40);
btn.setLocation((panel.getWidth())/2-40,panel.getHeight()-80);
//鎸夐挳鐐瑰嚮鏃舵樉紺哄綋鍓嶉変腑欏
btn.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
for(introwindex:table.getSelectedRows()){
JOptionPane.showMessageDialog(null,rowindex+""+table.getValueAt(rowindex,0));
}
}});
panel.add(table);
panel.add(btn);
this.add(panel);
}
//瀹氫箟涓浜涘繀瑕佺殑緇勪歡
privateJPanelpanel;
privateJTabletable;
privateJButtonbtn;
}