中易网

JAVA Swing 获取单选按钮的值,复选框的值

答案:3  悬赏:80  
解决时间 2021-01-16 07:35
JAVA Swing 获取单选按钮的值,复选框的值
最佳答案
由于我机子上没有你的布局器XYConstraints,所以我全注释了,你可以运行下面程序,能得到结果

package helloworld;

import java.awt.*;

import javax.swing.*;
//import com.borland.jbcl.layout.XYLayout;
//import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Frame1 extends JFrame {
//XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JButton jButton1 = new JButton();
JLabel jLabel2 = new JLabel();
JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
JCheckBox jCheckBox3 = new JCheckBox();
public static void main(String[] args) {
Frame1 frame=new Frame1();
frame.setBounds(100, 100, 200, 300);
frame.setVisible(true);
}
public Frame1() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
//getContentPane().setLayout(xYLayout1);
getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
jLabel1.setText("性 别:");
//xYLayout1.setWidth(326);
//xYLayout1.setHeight(214);
jButton1.setText("提 交");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jRadioButton2.setText("女");
jLabel2.setText("爱 好:");
jCheckBox1.setText("唱歌");
jCheckBox2.setText("跳舞");
jCheckBox3.setText("阅读");
this.getContentPane().add(jLabel1);//, new XYConstraints(31, 60, 46, 21));
this.getContentPane().add(jRadioButton1);//,new XYConstraints(127, 60, -1, -1));
this.getContentPane().add(jRadioButton2);//,new XYConstraints(216, 60, -1, -1));
this.getContentPane().add(jLabel2);//, new XYConstraints(31, 111, -1, -1));
this.getContentPane().add(jCheckBox1);//, new XYConstraints(97, 106, -1, -1));
this.getContentPane().add(jCheckBox2);//,new XYConstraints(164, 106, -1, -1));
this.getContentPane().add(jCheckBox3);//,new XYConstraints(233, 106, -1, -1));
this.getContentPane().add(jButton1);//, new XYConstraints(124, 154, -1, -1));
jRadioButton1.setText("男");
jRadioButton1.setSelected(true);
ButtonGroup bg=new ButtonGroup();
bg.add(jRadioButton1);
bg.add(jRadioButton2);

}

public void jButton1_actionPerformed(ActionEvent e) {
System.out.println("性别:"+(jRadioButton1.isSelected()?jRadioButton1.getText():jRadioButton2.getText()));
if(jCheckBox1.isSelected()){
System.out.println(jCheckBox1.getText());
}
if(jCheckBox2.isSelected()){
System.out.println(jCheckBox2.getText());
}
if(jCheckBox3.isSelected()){
System.out.println(jCheckBox3.getText());
}
}
}

class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
全部回答
String radValue= jRadioButton2.getText(); //单选按钮的值
//得到复选框的值
if(jCheckBox1.isSelected()){//如果复选框为选中
String txtValue=jCheckBox1.getText();
}
把复选框放到一个数组中,然后循环判断是否为选中。如果选中,则输出。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
食品工艺学 固体油脂种类有哪些
这像吊兰的是什么植物
wow5.0罗宁去哪了
大姚县农科所城北服务部在什么地方啊,我要过
每日焚香拜心中佛 可以吗? 没有供奉佛像,因
无法关闭Microsoft Office Word,因为一个对话
金视线巨幕影院怎么去啊,我要去那办事
parliament 香烟价格
在集安开个什么店最好,最挣钱
拉手祷告是什么派
西联汇款 收款要去开通什么的吗?
联想eton儿童锁怎么开
Z151次(北京—西宁)火车途经哪些站?
联想成语
辉具到栾川多远
推荐资讯
魏晨哥哥的家住哪?
凭韭菜发家致富,韭菜真的这么值钱吗
中日什么时候开战?
尚酷汽车用品地址在哪,我要去那里办事
请问纪念碑谷新买的章节支付成功后却说购买失
我的脑袋经常痛是怎么回事?
[PoRO]姫様限定有人知道吗
宽途汽车(3M名田汽车美容中心)地址在什么地方
ue 900 低音如何
公司里一个女同事,她和她分管副总关系不错,
羞辱怎么控制附身人物跑动
微信里哪有设置附近人允许看见我
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?