中易网

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

答案:3  悬赏:80  
解决时间 2021-01-16 07:35
  • 提问者网友:我没有何以琛的痴心不悔
  • 2021-01-15 09:34
JAVA Swing 获取单选按钮的值,复选框的值
最佳答案
  • 二级知识专家网友:神的生死簿
  • 2021-01-15 10:17
由于我机子上没有你的布局器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);
}
}
全部回答
  • 1楼网友:玩世
  • 2021-01-15 11:01
String radValue= jRadioButton2.getText(); //单选按钮的值
//得到复选框的值
if(jCheckBox1.isSelected()){//如果复选框为选中
String txtValue=jCheckBox1.getText();
}
  • 2楼网友:夜余生
  • 2021-01-15 10:47
把复选框放到一个数组中,然后循环判断是否为选中。如果选中,则输出。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息