| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1158 人关注过本帖
标题:[求助]一个比较菜的问题
只看楼主 加入收藏
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
 问题点数:0 回复次数:19 
[求助]一个比较菜的问题
今天学Swing,问一个比较菜的问题.
怎么添加单选按钮组,怎么使用ButtonGroup这个类.
谢谢大家。。
搜索更多相关主题的帖子: ButtonGroup Swing 按钮 
2006-06-22 16:50
水影月圆
Rank: 4
等 级:贵宾
威 望:11
帖 子:738
专家分:0
注 册:2005-8-2
收藏
得分:0 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyRadioButtons extends JFrame implements ActionListener
{
JRadioButton rad1,rad2;
ButtonGroup rbgrp;

public MyRadioButtons()
{
setTitle("单选按钮");
rbgrp=new ButtonGroup();
JPanel cpane=(JPanel)getContentPane();
cpane.setLayout(new GridLayout());
rad1=new JRadioButton("启用");
rad1.addActionListener(this);

cpane.add(rad1);
rad2=new JRadioButton("禁用");
rad2.addActionListener(this);

cpane.add(rad2);
rbgrp.add(rad1);
rbgrp.add(rad2);
radadapter radapp=new radadapter();
addWindowListener(radapp);
}
class radadapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==rad1)
{
//setTitle("启用第一个单选按钮");
rad1.setEnabled(false);
rad2.setEnabled(true);
}
else if(e.getSource()==rad2)
{
//setTitle("启用第二个单选按钮");
rad1.setEnabled(true);
rad2.setEnabled(false);
}
}
public static void main(String args[])
{
MyRadioButtons radb=new MyRadioButtons();
radb.setSize(300,200);
radb.setVisible(true);
}
}


此类用于为一组按钮创建一个多斥(multiple-exclusion)作用域。使用相同的 ButtonGroup 对象创建一组按钮意味着“开启”其中一个按钮时,将关闭组中的其他所有按钮


子非鱼,安知鱼之江湖?子非我,安知我之功夫 http://20681.
2006-06-22 16:57
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

ButtonGroup只是把所有需在互斥的单选接钮加到一起去就可以了
如果你不加的话,就会出现可以同时选择很多单选的情况


可惜不是你,陪我到最后
2006-06-22 17:00
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 

晕,看不懂。我们今天只学了怎么用JBuilder自动生成代码
有个地方不会写。。。。
package studentapplication;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class StudentApplicationFrameClass extends JFrame {
JPanel contentPane;
JLabel lblName = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextArea jTextArea1 = new JTextArea();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JRadioButton jRadioButton1 = new JRadioButton();
JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
JCheckBox jCheckBox3 = new JCheckBox();
ButtonGroup button = new ButtonGroup();
这里接下来这么写,就是让性别只能选一个
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
Border border1 = BorderFactory.createEtchedBorder(Color.lightGray,
new Color(148, 145, 140));
public StudentApplicationFrameClass() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Students Detail");
lblName.setText("姓名");
lblName.setBounds(new Rectangle(34, 55, 42, 15));
jTextField1.setBounds(new Rectangle(69, 54, 72, 21));
jLabel2.setText("资历");
jLabel2.setBounds(new Rectangle(214, 55, 42, 15));
jComboBox1.setBounds(new Rectangle(283, 55, 66, 23));
jComboBox1.addItem("本科生");
jComboBox1.addItem("研究生");
jComboBox1.addItem("工程师");
jComboBox1.addItem("博士");
jComboBox1.addItem("其他");
jLabel4.setText("兴趣");
jLabel4.setBounds(new Rectangle(214, 113, 42, 15));
jLabel5.setText("性别");
jLabel5.setBounds(new Rectangle(34, 203, 42, 15));
jLabel3.setText("地址");
jLabel3.setBounds(new Rectangle(34, 118, 42, 15));
jTextArea1.setBounds(new Rectangle(69, 115, 83, 60));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(271, 100, 95, 77));
jPanel1.setLayout(null);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(63, 191, 97, 79));
jPanel2.setLayout(null);
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(17, 12, 103, 23));
jCheckBox1.setText("阅读");
jCheckBox1.setBounds(new Rectangle(19, 6, 85, 23));
jCheckBox2.setText("唱歌");
jCheckBox2.setBounds(new Rectangle(19, 27, 85, 23));
jCheckBox3.setText("跳舞");
jCheckBox3.setBounds(new Rectangle(19, 50, 85, 23));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(17, 45, 103, 23));
jButton1.setBounds(new Rectangle(207, 244, 83, 25));
jButton1.setText("验证");
jButton2.setBounds(new Rectangle(308, 244, 83, 25));
jButton2.setText("重置");
contentPane.setBorder(border1);
contentPane.add(lblName);
contentPane.add(jLabel2);
contentPane.add(jComboBox1);
contentPane.add(jLabel4);
contentPane.add(jLabel5);
contentPane.add(jLabel3);
jPanel1.add(jCheckBox1);
jPanel1.add(jCheckBox2);
jPanel1.add(jCheckBox3);
jPanel2.add(jRadioButton2);
jPanel2.add(jRadioButton1);
contentPane.add(jPanel1);
contentPane.add(jTextArea1);
contentPane.add(jTextField1);
contentPane.add(jButton1);
contentPane.add(jButton2);
contentPane.add(jPanel2);
}
}


曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-06-22 17:06
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

你在用JB,初学者最好不要用JB
还是用一些简单的IDE好一点
用简单的IDE可以让你了解,这些东西是怎么来的
而不会让JB自动给你生成
并且JB生成的效率也不是很高


可惜不是你,陪我到最后
2006-06-22 17:11
水影月圆
Rank: 4
等 级:贵宾
威 望:11
帖 子:738
专家分:0
注 册:2005-8-2
收藏
得分:0 
用JC吧 自己写很有成就感的 你用JB都给你生成好了 你自己还能学到什么呢?

敢问下 你是上大学 还是培训机构的啊?

再追一句 如果看不懂 就把我那段代码用JC自己打遍 我想你到下星期一之前就该能看的懂了 这段代码很简单的

[此贴子已经被作者于2006-6-22 17:21:48编辑过]


子非鱼,安知鱼之江湖?子非我,安知我之功夫 http://20681.
2006-06-22 17:15
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 

材料学本科,现在在上培训机构。
package studentapplication;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class StudentApplicationFrameClass extends JFrame {
JPanel contentPane;
JLabel lblName = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextArea jTextArea1 = new JTextArea();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JRadioButton jRadioButton1 = new JRadioButton();
JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
JCheckBox jCheckBox3 = new JCheckBox();
ButtonGroup button = new ButtonGroup();
这里下面该怎么写啊?




JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
Border border1 = BorderFactory.createEtchedBorder(Color.lightGray,
new Color(148, 145, 140));
public StudentApplicationFrameClass() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Students Detail");
lblName.setText("姓名");
lblName.setBounds(new Rectangle(34, 55, 42, 15));
jTextField1.setBounds(new Rectangle(69, 54, 72, 21));
jLabel2.setText("资历");
jLabel2.setBounds(new Rectangle(214, 55, 42, 15));
jComboBox1.setBounds(new Rectangle(283, 55, 66, 23));
jComboBox1.addItem("本科生");
jComboBox1.addItem("研究生");
jComboBox1.addItem("工程师");
jComboBox1.addItem("博士");
jComboBox1.addItem("其他");
jLabel4.setText("兴趣");
jLabel4.setBounds(new Rectangle(214, 113, 42, 15));
jLabel5.setText("性别");
jLabel5.setBounds(new Rectangle(34, 203, 42, 15));
jLabel3.setText("地址");
jLabel3.setBounds(new Rectangle(34, 118, 42, 15));
jTextArea1.setBounds(new Rectangle(69, 115, 83, 60));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(271, 100, 95, 77));
jPanel1.setLayout(null);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(63, 191, 97, 79));
jPanel2.setLayout(null);
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(17, 12, 103, 23));
jCheckBox1.setText("阅读");
jCheckBox1.setBounds(new Rectangle(19, 6, 85, 23));
jCheckBox2.setText("唱歌");
jCheckBox2.setBounds(new Rectangle(19, 27, 85, 23));
jCheckBox3.setText("跳舞");
jCheckBox3.setBounds(new Rectangle(19, 50, 85, 23));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(17, 45, 103, 23));
jButton1.setBounds(new Rectangle(207, 244, 83, 25));
jButton1.setText("验证");
jButton2.setBounds(new Rectangle(308, 244, 83, 25));
jButton2.setText("重置");
contentPane.setBorder(border1);
contentPane.add(lblName);
contentPane.add(jLabel2);
contentPane.add(jComboBox1);
contentPane.add(jLabel4);
contentPane.add(jLabel5);
contentPane.add(jLabel3);
jPanel1.add(jCheckBox1);
jPanel1.add(jCheckBox2);
jPanel1.add(jCheckBox3);
jPanel2.add(jRadioButton2);
jPanel2.add(jRadioButton1);
contentPane.add(jPanel1);
contentPane.add(jTextArea1);
contentPane.add(jTextField1);
contentPane.add(jButton1);
contentPane.add(jButton2);
contentPane.add(jPanel2);
}
}


曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-06-22 17:20
NiceGirl
Rank: 2
等 级:新手上路
威 望:4
帖 子:909
专家分:0
注 册:2006-6-18
收藏
得分:0 
哦,我会写了,谢谢水影啊,你的代码对我就点启发~!哎,真苯~!

曾经以为百般艰难,蓦然回首,才发现已飞渡千山。。!
2006-06-22 17:33
水影月圆
Rank: 4
等 级:贵宾
威 望:11
帖 子:738
专家分:0
注 册:2005-8-2
收藏
得分:0 

我刚才把你给改好了 贴上来吧 看看和你的样不样


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class StudentApplicationFrameClass extends JFrame {
JPanel contentPane;
JLabel lblName = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextArea jTextArea1 = new JTextArea();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JRadioButton jRadioButton1 = new JRadioButton();
//你没的声明
JRadioButton jRadioButton2 = new JRadioButton();

JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
JCheckBox jCheckBox3 = new JCheckBox();
ButtonGroup button = new ButtonGroup();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();

public StudentApplicationFrameClass()
{
try
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Students Detail");
lblName.setText("姓名");
lblName.setBounds(new Rectangle(34, 55, 42, 15));
jTextField1.setBounds(new Rectangle(69, 54, 72, 21));
jLabel2.setText("资历");
jLabel2.setBounds(new Rectangle(214, 55, 42, 15));
jComboBox1.setBounds(new Rectangle(283, 55, 66, 23));
jComboBox1.addItem("本科生");
jComboBox1.addItem("研究生");
jComboBox1.addItem("工程师");
jComboBox1.addItem("博士");
jComboBox1.addItem("其他");
jLabel4.setText("兴趣");
jLabel4.setBounds(new Rectangle(214, 113, 42, 15));
jLabel5.setText("性别");
jLabel5.setBounds(new Rectangle(34, 203, 42, 15));
jLabel3.setText("地址");
jLabel3.setBounds(new Rectangle(34, 118, 42, 15));
jTextArea1.setBounds(new Rectangle(69, 115, 83, 60));
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(271, 100, 95, 77));
jPanel1.setLayout(null);
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBounds(new Rectangle(63, 191, 97, 79));
jPanel2.setLayout(null);
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(17, 12, 103, 23));
jCheckBox1.setText("阅读");
jCheckBox1.setBounds(new Rectangle(19, 6, 85, 23));
jCheckBox2.setText("唱歌");
jCheckBox2.setBounds(new Rectangle(19, 27, 85, 23));
jCheckBox3.setText("跳舞");
jCheckBox3.setBounds(new Rectangle(19, 50, 85, 23));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(17, 45, 103, 23));
jButton1.setBounds(new Rectangle(207, 244, 83, 25));
jButton1.setText("验证");
jButton2.setBounds(new Rectangle(308, 244, 83, 25));
jButton2.setText("重置");
// contentPane.setBorder(border1);
contentPane.add(lblName);
contentPane.add(jLabel2);
contentPane.add(jComboBox1);
contentPane.add(jLabel4);
contentPane.add(jLabel5);
contentPane.add(jLabel3);
jPanel1.add(jCheckBox1);
jPanel1.add(jCheckBox2);
jPanel1.add(jCheckBox3);
//把你的jRadioButton1和jRadioButton2给添到ButtonGroup里
button.add(jRadioButton2);
button.add(jRadioButton1);

jPanel2.add(jRadioButton2);
jPanel2.add(jRadioButton1);
contentPane.add(jPanel1);
contentPane.add(jTextArea1);
contentPane.add(jTextField1);
contentPane.add(jButton1);
contentPane.add(jButton2);
contentPane.add(jPanel2);
}
//我用JC添上去的
public static void main(String args[])
{
StudentApplicationFrameClass sa = new StudentApplicationFrameClass();
sa.setSize(800,600);
sa.setVisible(true);
}
}


子非鱼,安知鱼之江湖?子非我,安知我之功夫 http://20681.
2006-06-22 17:37
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
水影 ,不错啦,现在水平越来越高了

可惜不是你,陪我到最后
2006-06-22 19:40
快速回复:[求助]一个比较菜的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018433 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved