实现一个ActionListener
在actionPerformed函数里面判断是哪个单选按钮。
然后找到那个发生事件的按钮,取出值就可以了。
或者
你在提交的时候判断哪个单选按钮被选中了也行,不用给单选按钮添加时间。
效果:
JLabel label = new JLabel();// labelsetAlignmentX(JLabelCENTER_ALIGNMENT);
labelsetBorder(BorderFactorycreateLineBorder(ColorBLACK));
StringBuffer info = new StringBuffer();
infoappend("<html>");
infoappend("<meta charset=UTF-8>");
infoappend("<form>");
infoappend("<div><legend><h2 style=margin-left:50px>大写:__________ 合计:_________</h2></legend><div><br>");
infoappend("<div><legend><h2 style=\"margin-left:0px\">选中型号:__________ 选中规格:_________ 单价(米):_________元</h2></legend><div>");
infoappend("</form>");
infoappend(" </fieldset>");
infoappend("</body>");
infoappend("</html>");
labelsetText(infotoString());
import javaxswing;
import javaxswingborder;
import javaawt;
import javaawtevent;
import javautil;
/
@author Hardneedl
/
final public class PaintDemo extends JApplet {
private final static Dimension PREFERRED_SIZE = new Dimension(800, 600);
public Dimension getPreferredSize() {return PREFERRED_SIZE;}
public Dimension getMinimumSize() {return PREFERRED_SIZE;}
private Brush brush;private BrushCanvas canvas;
private JCheckBox ckGreen,ckRed,ckBlue;
private ButtonGroup rbGroup;
private JRadioButton rbSmall,rbMidel,rbBig;
public void init() {
canvas=new BrushCanvas(brush = new StringBrush(){
public Component getOwner() {
return canvas;
}
}
){
public Dimension getPreferredSize() {
return new Dimension(400, 450);
}
};
ckGreen=new JCheckBox(new ColorValueAction("green", ColorGREEN,brush));
ckRed=new JCheckBox(new ColorValueAction("red", ColorRED,brush));
ckBlue=new JCheckBox(new ColorValueAction("blue", ColorBLUE,brush));
rbGroup=new ButtonGroup();
rbGroupadd(rbSmall=new JRadioButton(new IntegerValueAction("10",10,brush)));
rbGroupadd(rbMidel=new JRadioButton(new IntegerValueAction("16",16,brush)));
rbGroupadd(rbBig=new JRadioButton(new IntegerValueAction("50", 50,brush)));
Container container = getContentPane();
BoxLayout boxLayout=new BoxLayout(container,BoxLayoutY_AXIS);
containersetLayout(boxLayout);
JComponent p = new JPanel();
padd(ckGreen);padd(ckRed);padd(ckBlue);
containeradd(p);
JComponent p0 = new JPanel();
p0add(rbSmall);p0add(rbMidel);p0add(rbBig);
containeradd(p0);
containeradd(canvas);
}
public void start() {
rbGroupsetSelected(rbBiggetModel(),true);
}
}
/整数值的动作,getValue("integer")返回代表的整数值/
class IntegerValueAction extends AbstractAction{
private int v;
private Observable observable=new Observable(){
public void notifyObservers(Object arg) {
setChanged();supernotifyObservers(arg);
}
};
public IntegerValueAction(String t, int v,Observer observer) {
super(t);
thisv=v;
observableaddObserver(observer);
}
public void actionPerformed(ActionEvent e) {
observablenotifyObservers(getValue("integer"));
}
public Object getValue(String key) {
if (keyequalsIgnoreCase("integer")){
return v;
}
return supergetValue(key);
}
}
class ColorValueAction extends AbstractAction{
private Color v=new Color(0);
private Observable observable=new Observable(){
public void notifyObservers(Object arg) {
setChanged();supernotifyObservers(arg);
}
};
public ColorValueAction(String t, Color v, Observer observer) {
super(t);
thisv=v;
observableaddObserver(observer);
}
public void actionPerformed(ActionEvent e) {
observablenotifyObservers(getValue("color"));
}
public Object getValue(String key) {
if (keyequalsIgnoreCase("color")){
return v;
}
return supergetValue(key);
}
}
/画笔/
interface Brush extends Observer{
/
绘图
@param g 绘图环境
/
void paint(Graphics g);
Component getOwner();
}
abstract class StringBrush implements Brush{
private int fontSize = 14;
private Color color;
String getString(){
return "请选择";
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)gcreate();
g2setColor(color==nullColorBLACK:color);
g2setFont(g2getFont()deriveFont((float)fontSize));
g2translate(40,50);
g2drawString(getString()==null"":getString(),0,0);
g2dispose();
}
public void update(Observable o, Object arg) {
if (arg instanceof Integer) {
fontSize = (int)arg;
}
if (arg instanceof Color) {
Color c = (Color)arg;
color=new Color(cgetRed(),cgetGreen(),cgetBlue());
}
Component owner = getOwner();
if (owner!=null)ownerrepaint();
}
}
/画布/
class BrushCanvas extends JComponent{
private Brush brush;
public BrushCanvas(Brush brush) {
setBrush(brush);
}
void setBrush(Brush b){
brush=b;
}
protected void paintComponent(Graphics g) {
superpaintComponent(g);
if (brush!=null)
brushpaint(g);
}
}
以上就是关于JAVA中单选按钮事件怎么写全部的内容,包括:JAVA中单选按钮事件怎么写、请问这种界面用java代码怎么实现、用Java编写这道题,并叙述一下分析思路,谢谢啦我是个Java初学者麻烦了!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)