社区
Java SE
帖子详情
有关在java 中加单选框!
K_THEKING
2008-03-05 11:27:41
我是个java菜鸟!想问一下在java中加单选框的方法!不是在Frame下而是用JFrame中的JCheckBox
在此先谢过.......
...全文
2070
8
打赏
收藏
有关在java 中加单选框!
我是个java菜鸟!想问一下在java中加单选框的方法!不是在Frame下而是用JFrame中的JCheckBox 在此先谢过.......
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
8 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
K_THEKING
2008-03-07
打赏
举报
回复
我将移动的盘若用JButton却得不到想要的结果这是为什么?????
K_THEKING
2008-03-07
打赏
举报
回复
另外我想问一下我的这个程序为什么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;
}
}
}
dufenal
2008-03-06
打赏
举报
回复
在awt里只要选择框是在同一组的就可以只能有一个被选中
zr_dixuexiongying
2008-03-06
打赏
举报
回复
并且你用JRadioButton的时候,还要写相应的事件,比如你有三个单选按钮,当你选择了第三个,前面两个就要自动关闭,需要在事件里面判断,这也是JRadioButton按纽的用法,在这一点上我觉得没有awt包里面的好用
K_THEKING
2008-03-06
打赏
举报
回复
哦!您是说JRadioButton在事件处理中都要加而且有一个改变其余的都要改是吗?
我的意思是说他的状态若选中为true则其余的都要改为false?就是说这都要加代码是把.......
谢谢了.....
zr_dixuexiongying
2008-03-06
打赏
举报
回复
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
K_THEKING
2008-03-05
打赏
举报
回复
哦!可能是是我没说清楚....
我是想说若用JCheckBox可以实现吗!
wuy13862574600
2008-03-05
打赏
举报
回复
awt 里不是 Choice吗?
java
checkbox
单选框
_复选框JCheckBox,
单选框
JRadioButton的简单使用
[
java
]代码库/*** 功能: 复选框JCheckBox,
单选框
JRadioButton的简单使用*/package com.jiemian;import
java
.awt.*;import
java
x.swing.*;public class Text6 extends JFrame{JPanel jp1,jp2,jp3;JLabel jl1,jl2;JCheckBox jcb1,jcb2,...
java
swing表格中包含
单选框
1,如何让表单中显示
单选框
呢? 步骤一:设置表格数据 在表格的data中设置元素为JRadioButton public void layoutTable( ) {//TODO Object[][] datas=new Object[4][3]; for(int i=0;i<4;i++){ Object[]objs=new Object[3...
HTML12:文本框和
单选框
【代码】HTML12:文本框和
单选框
。
Android的Button点击事件,提示以及
单选框
和复选框的点击事件
Android的Button点击事件,提示以及
单选框
和复选框的点击事件
java
:实现包含复选框的表格(附完整源码)
java
:实现包含复选框的表格(附完整源码)
Java SE
62,623
社区成员
307,257
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章