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

这是我自己写的一个测试程序,要求在点击“提交”按钮时,在监听事件中获取单选按钮的值,和复选框的值packagehelloworld;importjava.awt.*;im... 这是我自己写的一个测试程序,要求在点击“提交”按钮时,在监听事件中获取单选按钮的值,和复选框的值

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 Frame1() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(xYLayout1);
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(jRadioButton1,
new XYConstraints(127, 60, -1, -1));
this.getContentPane().add(jRadioButton2,
new XYConstraints(216, 60, -1, -1));
this.getContentPane().add(jButton1, new XYConstraints(124, 154, -1, -1));
this.getContentPane().add(jLabel2, new XYConstraints(31, 111, -1, -1));
this.getContentPane().add(jLabel1, new XYConstraints(31, 60, 46, 21));
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));
jRadioButton1.setText("男");
}

public void jButton1_actionPerformed(ActionEvent e) {

}
}

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);
}
}
展开
 我来答
方杭尽
2009-08-26 · TA获得超过1万个赞
知道大有可为答主
回答量:1296
采纳率:0%
帮助的人:650万
展开全部
由于我机子上没有你的布局器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);
}
}
16...7@qq.com
2009-08-25 · TA获得超过781个赞
知道小有建树答主
回答量:818
采纳率:0%
帮助的人:393万
展开全部
String radValue= jRadioButton2.getText(); //单选按钮的值
//得到复选框的值
if(jCheckBox1.isSelected()){//如果复选框为选中
String txtValue=jCheckBox1.getText();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
麴淑英熊风
2020-02-10 · TA获得超过3.6万个赞
知道大有可为答主
回答量:1.2万
采纳率:28%
帮助的人:2263万
展开全部
首先你应该在你所点击的那个按钮建立按钮监听程序,这是首要的。要获得按钮值和复选框的内容可以这样写在testfeild里面:jtextfeild1.setText(jButton1.g
etaction
commond
)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友388688327
2009-08-25 · TA获得超过1053个赞
知道小有建树答主
回答量:1297
采纳率:0%
帮助的人:967万
展开全部
把复选框放到一个数组中,然后循环判断是否为选中。如果选中,则输出。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
武树花渠香
2019-11-19 · TA获得超过3.6万个赞
知道大有可为答主
回答量:1.4万
采纳率:28%
帮助的人:819万
展开全部
每个按钮都有一个boolean值表示是否被选中(用isSelected方法获取),根据这个来判断就行了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式