61阅读

buttongroup-ButtonGroup

发布时间:2017-10-05 所属栏目:jbutton与button

一 : ButtonGroup

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();

JPanel entreePanel = new JPanel();
final ButtonGroup entreeGroup = new ButtonGroup();
JRadioButton radioButton;
entreePanel.add(radioButton = new JRadioButton("A"));
radioButton.setActionCommand("A");
entreeGroup.add(radioButton);
entreePanel.add(radioButton = new JRadioButton("B"));
radioButton.setActionCommand("B");
entreeGroup.add(radioButton);
entreePanel.add(radioButton = new JRadioButton("C", true));
radioButton.setActionCommand("C");
entreeGroup.add(radioButton);

final JPanel condimentsPanel = new JPanel();
condimentsPanel.add(new JCheckBox("Ketchup"));
condimentsPanel.add(new JCheckBox("Mustard"));
condimentsPanel.add(new JCheckBox("Pickles"));

JPanel orderPanel = new JPanel();
JButton orderButton = new JButton("Place Order");
orderPanel.add(orderButton);

frame.setLayout(new GridLayout(3, 1));
frame.add(entreePanel);
frame.add(condimentsPanel);
frame.add(orderPanel);

orderButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String entree = entreeGroup.getSelection().getActionCommand();
System.out.println(entree + " sandwich");
Component[] components = condimentsPanel.getComponents();
for (Component c : components) {
JCheckBox cb = (JCheckBox) c;
if (cb.isSelected())
System.out.println("With " + cb.getText());
}
}
});

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setVisible(true);
}
}

扩展:java buttongroup / buttongroup是什么 / button

二 : JButton-ButtonGroup-JTextFeild—JCheckBox练习

package com.lovo;

import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class MyFrame extends JFrame{
public MyFrame(int x,int y) {
init(x,y);
}

public void init(int x,int y) {
this.setSize(x, y);
this.setLocationRelativeTo(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(null);
JLabel jl = new JLabel();
Icon ic = newImageIcon("D:/image/fruit.gif");
jl.setIcon(ic);
jl.setBounds(100, 100,ic.getIconWidth(),ic.getIconHeight());
//按钮
JButton jb =createJbutton("登陆",50,20,60,20);
//文本框
JTextField jtf = newJTextField();
jtf.setBounds(130, 20, 100,20);
//单选按钮
JRadioButton jrb1 = newJRadioButton("男");
jrb1.setBounds(100, 130, 40,20);
JRadioButton(www.61k.com) jrb2 = newJRadioButton("女");
jrb2.setBounds(150, 130, 40,20);
//组对象,用来添加相同一组单选按钮的对象
ButtonGroup bg = newButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
//复选按钮
JCheckBox jcb1 = newJCheckBox("体育");
jcb1.setBounds(50, 160, 60,20);
JCheckBox jcb2 = newJCheckBox("音乐");
jcb2.setBounds(120, 160, 60,20);
JCheckBox jcb3 = newJCheckBox("游戏");
jcb3.setBounds(190, 160, 60,20);
Container c =this.getContentPane();
c.add(jl);
c.add(jb);
c.add(jtf);
c.add(jrb1);
c.add(jrb2);
c.add(jcb1);
c.add(jcb2);
c.add(jcb3);
this.setVisible(true);
}
//将单选框封装成为1个方法
public JButton createJbutton(String name,intx,int y,int width,int height) {
JButton jb = newJButton(name);
jb.setBounds(x, y, width,height);
return jb;
}
//将复选框封装成为1个方法
public JCheckBox createjcheckbutton(Stringname,int x,int y,int width,int height){
JCheckBox jcb = newJCheckBox(name);
return jcb;
}

public static void main(String[] args) {
new MyFrame(300,300);
}

}

本文标题:buttongroup-ButtonGroup
本文地址: http://www.61k.com/1091431.html

61阅读| 精彩专题| 最新文章| 热门文章| 苏ICP备13036349号-1