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;
}