有关在java 中加单选框! 我是个java菜鸟!想问一下在java中加单选框的方法!不是在Frame下而是用JFrame中的JCheckBox在此先谢过....... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 awt 里不是 Choice吗? 哦!可能是是我没说清楚....我是想说若用JCheckBox可以实现吗! import javax.swing.JFrame;import javax.swing.JCheckBox;import java.awt.GridLayout;public class Test extends JFrame{ private JCheckBox box1 = new JCheckBox("ew",true); private JCheckBox box2 = new JCheckBox("ds",false); private JCheckBox box3 = new JCheckBox("dw",false); public Test() { setTitle("测试"); setLayout(new GridLayout(3,1)); add(box1); add(box2); add(box3); setSize(400,300); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); new Test(); }}不过你说的swing;里面的JCheckBox是复选框,单选按钮应该是JRadioButton 并且你用JRadioButton的时候,还要写相应的事件,比如你有三个单选按钮,当你选择了第三个,前面两个就要自动关闭,需要在事件里面判断,这也是JRadioButton按纽的用法,在这一点上我觉得没有awt包里面的好用 哦!您是说JRadioButton在事件处理中都要加而且有一个改变其余的都要改是吗?我的意思是说他的状态若选中为true则其余的都要改为false?就是说这都要加代码是把.......谢谢了..... 在awt里只要选择框是在同一组的就可以只能有一个被选中 另外我想问一下我的这个程序为什么JButton和Button的用途!import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Hanoi extends JFrame implements ActionListener//定义新窗口类{ static int n=1; //初始化盘子个数 static int timeact=18; //演示的时间设置为中速 static int xh=500; //原柱的的高度 static int yh=550; //中间柱的的高度 static int zh=550; //目的柱的的高度 static JLabel lab1=new JLabel("Please input the number of dishs (1~10)"); static JTextField txt1=new JTextField(2); //创建取得用户定义的盘子个数的文本区 static JTextArea jta=new JTextArea(" "); static JLabel lab0=new JLabel("Need moving times!"); static JLabel lab2=new JLabel("Demonstrate speed"); static JLabel note=new JLabel("Error!Please note!the number of dishs is irrational"); static JRadioButton jrb1=new JRadioButton("Quick"); //用户对演示的时间设置所使用的单选框 static JRadioButton jrb2=new JRadioButton("Middle"); static JRadioButton jrb3=new JRadioButton("Slow"); static JButton btn1=new JButton("Demonstrate"); //创建按钮 static JButton btn2=new JButton("Exit"); static JButton btn3=new JButton("Renovate"); static Button but[]=new Button[10]; //使用标签对象做盘子,最多10个盘 public Hanoi() { btn1.addActionListener(this); //设置按钮的事件监听者为FRM btn2.addActionListener(this); btn3.addActionListener(this); jrb1.addActionListener(this); jrb2.addActionListener(this); jrb3.addActionListener(this); this.setResizable(false); //设置窗口不可改变大小 this.setLayout(null); this.setTitle("Demonstrate of hanoi tower"); lab1.setBounds(20,560,240,15); //初始所有标签,按钮,文本框,单选框的位置 txt1.setBounds(250,560,20,15); lab2.setBounds(20,580,120,15); lab2.setBackground(Color.red); jrb1.setBounds(141,580,50,15); jrb2.setBounds(191,580,52,15); jrb3.setBounds(242,580,50,15); note.setBounds(20,620,300,15); note.setForeground(Color.red); jrb1.setSelected(false); jrb2.setSelected(true); jrb3.setSelected(false); note.setVisible(false); lab0.setBounds(20,600,120,15); jta.setBounds(150,600,100,15); jta.setEditable(false); jta.setBackground(Color.pink); btn1.setBounds(340,560,120,30); btn2.setBounds(600,560,120,30); btn3.setBounds(340,560,120,30); this.add(lab0); this.add(jta); this.add(txt1); //加载标签,按钮,文本框,单选框到窗口 this.add(lab1); this.add(lab2); this.add(note); this.add(btn1); this.add(btn2); this.add(btn3); this.add(jrb1); this.add(jrb2); this.add(jrb3); this.setVisible(true); //设置各组件对象是否可见 btn1.setVisible(true); btn3.setVisible(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(1000,700); setVisible(true); } public static void main(String args[]) { new Hanoi(); } public void actionPerformed(ActionEvent e) //设置按钮事件 { if(e.getSource()==btn1) //btn1为演示开始按钮 { try{ n=Integer.parseInt(txt1.getText()); } //对输入的异常处理 catch(NumberFormatException f) { note.setVisible(true); return; } if(n>0&&n<=10) //对输入的异常处理 { int sum=1; for(int i=1;i<=n;i++) sum=sum*2; jta.setText(String.valueOf(sum-1)); xh=550-50*n; note.setVisible(false); btn1.setVisible(true); } else btn1.setVisible(false); for(int i=0;i<=n-1;i++) //建立盘子对象并加载到窗口 { but[i]=new Button("Dish"+i); but[i].setSize(24*(i+1),50); but[i].setLocation(140-but[i].getWidth()/2,550-but[i].getHeight()*(n-i)); but[i].setForeground(Color.red); this.add(but[i]); } btn1.setVisible(false); for(double i=1;i<=Math.pow(10,6);i++); this.hanoi(but,n,140,400,660); //汉诺塔演示的核心程序的调用 btn3.setVisible(true); //演示结束显示刷新按钮 txt1.setVisible(false); } if(e.getSource()==btn3) //刷新,回到初始状态 { btn3.setVisible(false); btn1.setVisible(true); for(int i=0;i<=n-1;i++) this.remove(but[i]); txt1.setVisible(true); xh=550-50*n; yh=550; zh=550; } if(e.getSource()==btn2) //退出 System.exit(0); if(e.getSource()==jrb1) { jrb1.setSelected(true); jrb2.setSelected(false); jrb3.setSelected(false); timeact=17; } else if(e.getSource()==jrb2) { timeact=18; jrb1.setSelected(false); jrb2.setSelected(true); jrb3.setSelected(false); } else if(e.getSource()==jrb3) { timeact=19; jrb1.setSelected(false); jrb2.setSelected(false); jrb3.setSelected(true); } } public void settimeactr(int timeactc) //移动的间隔时间 { for(double h=1;h<=Math.pow(2,(double)timeactc);h++); } public void setactcolor(Button labl[],int ncl) //盘子移动时的颜色,绿色 { labl[ncl-1].setBackground(Color.green); } public void setcolorbak(Button labl[],int ncl) //盘子静止时的颜色,红色 { labl[ncl-1].setBackground(Color.red); } public void hanoi(Button labc[],int nc,int xc,int yc,int zc) //汉诺塔演示的核心程序,递归实现 { if(nc==1) {setactcolor(labc,1); settimeactr(timeact); move(labc,xc,1,zc); //当只有一个盘子时直接从原柱移动到目的柱 settimeactr(timeact); setcolorbak(labc,1); } else{ hanoi(labc,nc-1,xc,zc,yc);//当有N个盘子时,先把上面N-1个盘子移动到辅助的柱子上 setactcolor(labc,nc); settimeactr(timeact); move(labc,xc,nc,zc); //移动第N个盘子到目的柱 settimeactr(timeact); setcolorbak(labc,nc); hanoi(labc,nc-1,yc,xc,zc);//把剩下的N-1个盘子从辅助的柱子移动到目的柱 } } public void move(Button butt[],int xb,int nb,int zb) //移动操作 { if(xb==140&&zb==660) {butt[nb-1].setLocation(660-butt[nb-1].getWidth()/2,zh-50); xh=xh+50; zh=zh-50; } if(xb==140&&zb==400) {butt[nb-1].setLocation(400-butt[nb-1].getWidth()/2,yh-50); xh=xh+50; yh=yh-50; } if(xb==400&&zb==660) {butt[nb-1].setLocation(660-butt[nb-1].getWidth()/2,zh-50); yh=yh+50; zh=zh-50; } if(xb==400&&zb==140) {butt[nb-1].setLocation(140-butt[nb-1].getWidth()/2,xh-50); yh=yh+50; xh=xh-50; } if(xb==660&&zb==400) {butt[nb-1].setLocation(400-butt[nb-1].getWidth()/2,yh-50); zh=zh+50; yh=yh-50; } if(xb==660&&zb==140) {butt[nb-1].setLocation(140-butt[nb-1].getWidth()/2,xh-50); zh=zh+50; xh=xh-50; } }} 我将移动的盘若用JButton却得不到想要的结果这是为什么????? 一个写法的问题 JUnit4框架代码解析 java判断目录是否存在用什么函数... 這句为什么会提示finally 块没有正确的完成 SplitPane设置左右大小问题 写入日文xml文档出现了乱码的问题 为何子菜单项不能显示? 在线等待 SCJD是什么东西,在线等待。 在java中如何人工触发事件? 关于打开的游标超出最大数问题 一半不是自己编写的,搞了1个多小时,结果编译还是出错了,请指教 问Calendar.DATE是什么意思
我是想说若用JCheckBox可以实现吗!
import javax.swing.JCheckBox;
import java.awt.GridLayout;public class Test extends JFrame
{
private JCheckBox box1 = new JCheckBox("ew",true);
private JCheckBox box2 = new JCheckBox("ds",false);
private JCheckBox box3 = new JCheckBox("dw",false);
public Test()
{
setTitle("测试");
setLayout(new GridLayout(3,1));
add(box1);
add(box2);
add(box3);
setSize(400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
} public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
new Test();
}
}不过你说的swing;里面的JCheckBox是复选框,单选按钮应该是JRadioButton
我的意思是说他的状态若选中为true则其余的都要改为false?就是说这都要加代码是把.......
谢谢了.....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Hanoi extends JFrame implements ActionListener//定义新窗口类
{ static int n=1; //初始化盘子个数
static int timeact=18; //演示的时间设置为中速
static int xh=500; //原柱的的高度
static int yh=550; //中间柱的的高度
static int zh=550; //目的柱的的高度
static JLabel lab1=new JLabel("Please input the number of dishs (1~10)");
static JTextField txt1=new JTextField(2); //创建取得用户定义的盘子个数的文本区
static JTextArea jta=new JTextArea(" ");
static JLabel lab0=new JLabel("Need moving times!");
static JLabel lab2=new JLabel("Demonstrate speed");
static JLabel note=new JLabel("Error!Please note!the number of dishs is irrational");
static JRadioButton jrb1=new JRadioButton("Quick"); //用户对演示的时间设置所使用的单选框
static JRadioButton jrb2=new JRadioButton("Middle");
static JRadioButton jrb3=new JRadioButton("Slow");
static JButton btn1=new JButton("Demonstrate"); //创建按钮
static JButton btn2=new JButton("Exit");
static JButton btn3=new JButton("Renovate");
static Button but[]=new Button[10]; //使用标签对象做盘子,最多10个盘
public Hanoi()
{
btn1.addActionListener(this); //设置按钮的事件监听者为FRM
btn2.addActionListener(this);
btn3.addActionListener(this);
jrb1.addActionListener(this);
jrb2.addActionListener(this);
jrb3.addActionListener(this);
this.setResizable(false); //设置窗口不可改变大小
this.setLayout(null);
this.setTitle("Demonstrate of hanoi tower");
lab1.setBounds(20,560,240,15); //初始所有标签,按钮,文本框,单选框的位置
txt1.setBounds(250,560,20,15);
lab2.setBounds(20,580,120,15);
lab2.setBackground(Color.red);
jrb1.setBounds(141,580,50,15);
jrb2.setBounds(191,580,52,15);
jrb3.setBounds(242,580,50,15);
note.setBounds(20,620,300,15);
note.setForeground(Color.red);
jrb1.setSelected(false);
jrb2.setSelected(true);
jrb3.setSelected(false);
note.setVisible(false);
lab0.setBounds(20,600,120,15);
jta.setBounds(150,600,100,15);
jta.setEditable(false);
jta.setBackground(Color.pink);
btn1.setBounds(340,560,120,30);
btn2.setBounds(600,560,120,30);
btn3.setBounds(340,560,120,30);
this.add(lab0);
this.add(jta);
this.add(txt1); //加载标签,按钮,文本框,单选框到窗口
this.add(lab1);
this.add(lab2);
this.add(note);
this.add(btn1);
this.add(btn2);
this.add(btn3);
this.add(jrb1);
this.add(jrb2);
this.add(jrb3);
this.setVisible(true); //设置各组件对象是否可见
btn1.setVisible(true);
btn3.setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1000,700);
setVisible(true);
} public static void main(String args[])
{
new Hanoi();
}
public void actionPerformed(ActionEvent e) //设置按钮事件
{
if(e.getSource()==btn1) //btn1为演示开始按钮
{
try{
n=Integer.parseInt(txt1.getText());
} //对输入的异常处理
catch(NumberFormatException f)
{
note.setVisible(true);
return;
}
if(n>0&&n<=10) //对输入的异常处理
{
int sum=1;
for(int i=1;i<=n;i++)
sum=sum*2;
jta.setText(String.valueOf(sum-1));
xh=550-50*n;
note.setVisible(false);
btn1.setVisible(true);
}
else
btn1.setVisible(false);
for(int i=0;i<=n-1;i++) //建立盘子对象并加载到窗口
{
but[i]=new Button("Dish"+i);
but[i].setSize(24*(i+1),50);
but[i].setLocation(140-but[i].getWidth()/2,550-but[i].getHeight()*(n-i));
but[i].setForeground(Color.red);
this.add(but[i]);
}
btn1.setVisible(false);
for(double i=1;i<=Math.pow(10,6);i++);
this.hanoi(but,n,140,400,660); //汉诺塔演示的核心程序的调用
btn3.setVisible(true); //演示结束显示刷新按钮
txt1.setVisible(false);
}
if(e.getSource()==btn3) //刷新,回到初始状态
{ btn3.setVisible(false);
btn1.setVisible(true);
for(int i=0;i<=n-1;i++)
this.remove(but[i]);
txt1.setVisible(true);
xh=550-50*n;
yh=550;
zh=550;
}
if(e.getSource()==btn2) //退出
System.exit(0);
if(e.getSource()==jrb1)
{
jrb1.setSelected(true);
jrb2.setSelected(false);
jrb3.setSelected(false);
timeact=17;
}
else if(e.getSource()==jrb2)
{
timeact=18;
jrb1.setSelected(false);
jrb2.setSelected(true);
jrb3.setSelected(false);
}
else if(e.getSource()==jrb3)
{
timeact=19;
jrb1.setSelected(false);
jrb2.setSelected(false);
jrb3.setSelected(true);
}
}
public void settimeactr(int timeactc) //移动的间隔时间
{
for(double h=1;h<=Math.pow(2,(double)timeactc);h++);
}
public void setactcolor(Button labl[],int ncl) //盘子移动时的颜色,绿色
{
labl[ncl-1].setBackground(Color.green);
}
public void setcolorbak(Button labl[],int ncl) //盘子静止时的颜色,红色
{
labl[ncl-1].setBackground(Color.red);
}
public void hanoi(Button labc[],int nc,int xc,int yc,int zc) //汉诺塔演示的核心程序,递归实现
{ if(nc==1)
{setactcolor(labc,1);
settimeactr(timeact);
move(labc,xc,1,zc); //当只有一个盘子时直接从原柱移动到目的柱
settimeactr(timeact);
setcolorbak(labc,1);
}
else{
hanoi(labc,nc-1,xc,zc,yc);//当有N个盘子时,先把上面N-1个盘子移动到辅助的柱子上
setactcolor(labc,nc);
settimeactr(timeact);
move(labc,xc,nc,zc); //移动第N个盘子到目的柱
settimeactr(timeact);
setcolorbak(labc,nc);
hanoi(labc,nc-1,yc,xc,zc);//把剩下的N-1个盘子从辅助的柱子移动到目的柱
}
}
public void move(Button butt[],int xb,int nb,int zb) //移动操作
{ if(xb==140&&zb==660)
{butt[nb-1].setLocation(660-butt[nb-1].getWidth()/2,zh-50);
xh=xh+50;
zh=zh-50;
}
if(xb==140&&zb==400)
{butt[nb-1].setLocation(400-butt[nb-1].getWidth()/2,yh-50);
xh=xh+50;
yh=yh-50;
}
if(xb==400&&zb==660)
{butt[nb-1].setLocation(660-butt[nb-1].getWidth()/2,zh-50);
yh=yh+50;
zh=zh-50;
}
if(xb==400&&zb==140)
{butt[nb-1].setLocation(140-butt[nb-1].getWidth()/2,xh-50);
yh=yh+50;
xh=xh-50;
}
if(xb==660&&zb==400)
{butt[nb-1].setLocation(400-butt[nb-1].getWidth()/2,yh-50);
zh=zh+50;
yh=yh-50;
}
if(xb==660&&zb==140)
{butt[nb-1].setLocation(140-butt[nb-1].getWidth()/2,xh-50);
zh=zh+50;
xh=xh-50;
}
}
}