下面列出了怎么用javax.swing.ButtonGroup的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Construct a new action.
*
* @param context Graph Node.
* @param buttonGroup The button group to which this action belongs.
*/
public Toggle3DAction(final GraphNode context, final ButtonGroup buttonGroup) {
this.context = context;
this.buttonGroup = buttonGroup;
final ReadableGraph rg = context.getGraph().getReadableGraph();
final boolean isDisplayMode3D;
try {
final int displayMode3DAttribute = VisualConcept.GraphAttribute.DISPLAY_MODE_3D.get(rg);
isDisplayMode3D = displayMode3DAttribute != Graph.NOT_FOUND ? rg.getBooleanValue(displayMode3DAttribute, 0) : VisualGraphDefaults.DEFAULT_DISPLAY_MODE_3D;
} finally {
rg.release();
}
putValue(
Action.SMALL_ICON,
isDisplayMode3D ? MODE_3D_ICON : MODE_2D_ICON
);
putValue(Action.SHORT_DESCRIPTION, Bundle.CTL_Toggle3DAction());
putValue(Action.SELECTED_KEY, isDisplayMode3D);
}
/**
* Construct a new action.
*
* @param context Graph Node.
* @param buttonGroup The button group to which this action belongs.
*/
public ToggleDrawDirectedAction(final GraphNode context, final ButtonGroup buttonGroup) {
this.context = context;
this.buttonGroup = buttonGroup;
final ReadableGraph rg = context.getGraph().getReadableGraph();
final boolean drawDirected;
try {
final int drawDirectedAttribute = VisualConcept.GraphAttribute.DRAW_DIRECTED_TRANSACTIONS.get(rg);
drawDirected = drawDirectedAttribute != Graph.NOT_FOUND ? rg.getBooleanValue(drawDirectedAttribute, 0) : VisualGraphDefaults.DEFAULT_DRAWING_DIRECTED_TRANSACTIONS;
} finally {
rg.release();
}
putValue(
Action.SMALL_ICON,
drawDirected ? DIRECTED_ICON : UNDIRECTED_ICON
);
putValue(Action.SHORT_DESCRIPTION, Bundle.CTL_ToggleDrawDirectedAction());
putValue(Action.SELECTED_KEY, drawDirected);
}
/**
* Method addSelectionToolButton must have been invoked prior to this on the
* JToolBar.
*
* @param toolBar
* @param editor
* @param tool
* @param toolTipText
* @param labels
* @return
*/
private JToggleButton addToolButton(JToolBar toolBar,
DrawingEditor editor,
Tool tool,
String toolTipText,
ImageIcon iconBase) {
JToggleButton toggleButton = new JToggleButton();
toggleButton.setIcon(iconBase);
toggleButton.setText(null);
toggleButton.setToolTipText(toolTipText);
toggleButton.addItemListener(new ToolButtonListener(tool, editor));
// toggleButton.setFocusable(false);
ToolListener toolHandler = (ToolListener) toolBar.getClientProperty("toolHandler");
tool.addToolListener(toolHandler);
ButtonGroup group = (ButtonGroup) toolBar.getClientProperty("toolButtonGroup");
group.add(toggleButton);
toolBar.add(toggleButton);
return toggleButton;
}
/**
* Adds the feature radio buttons.
*/
private void addFeatureRadioButtons() {
if (this.featureRadioButtonMap.size() == 0) {
return;
}
List<JRadioButton> radioButtons = new ArrayList<>(this.featureRadioButtonMap.values());
radioButtons.sort(new Comparator<JRadioButton>() {
@Override
public int compare(JRadioButton arg0, JRadioButton arg1) {
return arg0.getText().toLowerCase().compareTo(arg1.getText().toLowerCase());
}
});
ButtonGroup featureRadioButtonGroup = new ButtonGroup();
for (JRadioButton radioButton : radioButtons) {
if (radioButton.getParent() != this.featureRadioButtonVerticalScrollPanel) {
this.featureRadioButtonVerticalScrollPanel.add(radioButton);
}
featureRadioButtonGroup.add(radioButton);
}
}
private void updatePerspectives(List<Perspective> perspectives) {
removeAll();
perspectiveMap.clear();
workspaceMenuGroup = new ButtonGroup();
for (Perspective p : perspectives) {
ResourceAction action = perspectiveController.createPerspectiveAction(p);
JMenuItem menuItem = new JRadioButtonMenuItem(action);
add(menuItem);
perspectiveMap.put(p.getName(), menuItem);
workspaceMenuGroup.add(menuItem);
}
if (perspectiveMap.containsKey(perspectiveName)) {
perspectiveMap.get(perspectiveName).setSelected(true);
}
}
/**
* Restore all integer options to their default values.
*/
private void defaultValuesInteger() {
for (Entry<ConfigurationValueInteger, Object> entry : integerValues.entrySet()) {
if ((entry.getValue() != null) && (entry.getKey() != null)) {
if (entry.getValue() instanceof JSpinner) {
JSpinner spinner = (JSpinner) entry.getValue();
SpinnerModel model = spinner.getModel();
model.setValue(Integer.valueOf(entry.getKey().getDefaultValue()));
}
if (entry.getValue() instanceof ButtonGroup) {
ButtonGroup group = (ButtonGroup) entry.getValue();
setButtonGroupSelection(group, entry.getKey().getDefaultValue());
}
}
}
}
/**
* 处理被选中的单选框
*
* @param group the group
* @param button the button
*/
private void addChangeTagRadioButton(@NotNull ButtonGroup group, JRadioButton button) {
group.add(button);
// 构造一个监听器,响应checkBox事件
ActionListener actionListener = e -> {
Object sourceObject = e.getSource();
if (sourceObject instanceof JRadioButton) {
JRadioButton sourceButton = (JRadioButton) sourceObject;
if (ImageMarkEnum.CUSTOM.text.equals(sourceButton.getText())) {
customHtmlTypeTextField.setEnabled(true);
} else {
customHtmlTypeTextField.setEnabled(false);
}
}
};
button.addActionListener(actionListener);
}
@SuppressWarnings("LeakingThisInConstructor")
public MetalThemeMenu(String name, MetalTheme[] themeArray) {
super(name);
themes = themeArray;
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < themes.length; i++) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(themes[i].
getName());
group.add(item);
add(item);
item.setActionCommand(i + "");
item.addActionListener(this);
if (i == 0) {
item.setSelected(true);
}
}
}
public Component getDefaultComponent(Container aContainer) {
Component c = getFirstComponent(aContainer);
if (c instanceof AbstractButton) {
ButtonModel bm = ((AbstractButton)c).getModel();
if (bm instanceof DefaultButtonModel) {
ButtonGroup bg = ((DefaultButtonModel)bm).getGroup();
Enumeration<AbstractButton> en = bg == null ? null : bg.getElements();
while (en != null && en.hasMoreElements()) {
AbstractButton ab = en.nextElement();
if (ab.isSelected()) return ab;
}
}
}
return c;
}
private void createToggleButtonPanel(SoundIdReader soundIdReader, int infoPanelWidth, CardLayout cardLayout, JPanel infoPanel) {
JPanel toggleButtonPanel = JPanelFactory.createBorderlessPanel();
toggleButtonPanel.setBounds(5, 5, infoPanelWidth, 40);
toggleButtonPanel.setLayout(new FlowLayout());
contentPanel.add(toggleButtonPanel);
ButtonGroup buttonGroup = new ButtonGroup();
JToggleButton familyButton = createToggleButton("Family", FAMILY_KEY, soundIdReader, cardLayout, infoPanel, buttonGroup, toggleButtonPanel, "Shows family members of the player character");
createToggleButton("Acquaintances", ACQUAINTANCES_KEY, soundIdReader, cardLayout, infoPanel, buttonGroup, toggleButtonPanel, "Shows acquaintances of the player character");
createToggleButton("Player Character Ranks", RANKS_KEY, soundIdReader, cardLayout, infoPanel, buttonGroup, toggleButtonPanel, "Shows group memberships of the player character");
createToggleButton("Organizations", ORGANIZATIONS_KEY, soundIdReader, cardLayout, infoPanel, buttonGroup, toggleButtonPanel, "Shows an overview of all organizations and their members");
createToggleButton("Deities", DEITIES_KEY, soundIdReader, cardLayout, infoPanel, buttonGroup, toggleButtonPanel, "Shows an overview of all deities and their happiness");
buttonGroup.setSelected(familyButton.getModel(), true);
}
@SuppressWarnings("LeakingThisInConstructor")
public MetalThemeMenu(String name, MetalTheme[] themeArray) {
super(name);
themes = themeArray;
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < themes.length; i++) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(themes[i].
getName());
group.add(item);
add(item);
item.setActionCommand(i + "");
item.addActionListener(this);
if (i == 0) {
item.setSelected(true);
}
}
}
@SuppressWarnings("LeakingThisInConstructor")
public MetalThemeMenu(String name, MetalTheme[] themeArray) {
super(name);
themes = themeArray;
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < themes.length; i++) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(themes[i].
getName());
group.add(item);
add(item);
item.setActionCommand(i + "");
item.addActionListener(this);
if (i == 0) {
item.setSelected(true);
}
}
}
private void createMenu(String selection) {
menu = new JMenu();
menu.setText("Capturing Hardware");
menu.setToolTipText("Allows direct selection of capturing hardware");
buttonGroup = new ButtonGroup();
table.entrySet().stream().map((kvp) -> {
String name = kvp.getKey();
final ICapturingHardware<?> hardware = kvp.getValue();
JRadioButton menuItem = new JRadioButton(name);
menuItem.setSelected(name.equals(selection));
//portRadioButtons[i] = menuItem;
menuItem.addActionListener((java.awt.event.ActionEvent evt) -> {
try {
select(hardware);
} catch (IOException | HarcHardwareException ex) {
guiUtils.error(ex);
}
});
return menuItem;
}).map((menuItem) -> {
buttonGroup.add(menuItem);
return menuItem;
}).forEachOrdered((menuItem) -> {
menu.add(menuItem);
});
}
/**
* This method builds the specified button according to the specified
* parameters.
*
* @param button The button to build.
* @param text The text displayed on the button.
* @param icon The icon to use.
* @param handler The action listener that monitors the buttons events.
* @param command The action command used when the button is clicked.
* @param group The group the button should belong to.
* @param selected The initial selected state of the button.
* @param tip The tool tip to display.
*/
public
static
void
buildButton(AbstractButton button, String text, Icon icon,
ActionListener handler, String command, ButtonGroup group,
boolean selected, String tip)
{
button.addActionListener(handler);
button.setActionCommand(command);
button.setIcon(icon);
button.setSelected(selected);
button.setText(text);
button.setToolTipText(tip);
if(group != null)
{
group.add(button);
}
}
private void addRadioButtonMenuItemZoom(
JMenu menuZoom, ButtonGroup buttonGroup,
final double zoom, String caption, int nemonic,
double currentZoom) {
JRadioButtonMenuItem radioButtonMenuItemZoom = new JRadioButtonMenuItem(caption);
if (nemonic != -1) {
radioButtonMenuItemZoom.setMnemonic(nemonic);
}
radioButtonMenuItemZoom.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setZoom(zoom);
}
});
if (currentZoom == zoom) {
radioButtonMenuItemZoom.setSelected(true);
}
buttonGroup.add(radioButtonMenuItemZoom);
menuZoom.add(radioButtonMenuItemZoom);
}
/**
* A layout direction button
*/
public DirectionButton(Icon icon, Icon downIcon, String direction,
String description, ActionListener l,
ButtonGroup group, boolean selected) {
super();
this.addActionListener(l);
setFocusPainted(false);
setHorizontalTextPosition(CENTER);
group.add(this);
setIcon(icon);
setSelectedIcon(downIcon);
setActionCommand(direction);
getAccessibleContext().setAccessibleName(direction);
getAccessibleContext().setAccessibleDescription(description);
setSelected(selected);
}
private void initGui() {
importLanguageRadioDe = new JRadioButton("");
importLanguageRadioEn = new JRadioButton("");
ButtonGroup importLanguageRadio = new ButtonGroup();
importLanguageRadio.add(importLanguageRadioDe);
importLanguageRadio.add(importLanguageRadioEn);
setLayout(new GridBagLayout());
{
language = new JPanel();
add(language, GuiUtil.setConstraints(0,0,1.0,0.0,GridBagConstraints.BOTH,5,0,5,0));
language.setBorder(BorderFactory.createTitledBorder(""));
language.setLayout(new GridBagLayout());
importLanguageRadioDe.setIconTextGap(10);
importLanguageRadioEn.setIconTextGap(10);
{
language.add(importLanguageRadioDe, GuiUtil.setConstraints(0,0,1.0,1.0,GridBagConstraints.BOTH,0,5,0,5));
language.add(importLanguageRadioEn, GuiUtil.setConstraints(0,1,1.0,1.0,GridBagConstraints.BOTH,0,5,0,5));
}
}
}
public GenealogyExample() {
super(new BorderLayout());
// Construct the panel with the toggle buttons.
JRadioButton showDescendant = new JRadioButton("Show descendants", true);
final JRadioButton showAncestor = new JRadioButton("Show ancestors");
ButtonGroup bGroup = new ButtonGroup();
bGroup.add(showDescendant);
bGroup.add(showAncestor);
showDescendant.addActionListener(this);
showAncestor.addActionListener(this);
showAncestor.setActionCommand(SHOW_ANCESTOR_CMD);
JPanel buttonPanel = new JPanel();
buttonPanel.add(showDescendant);
buttonPanel.add(showAncestor);
// Construct the tree.
tree = new GenealogyTree(getGenealogyGraph());
JScrollPane scrollPane = new JScrollPane(tree);
scrollPane.setPreferredSize(new Dimension(200, 200));
// Add everything to this panel.
add(buttonPanel, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
/**
*
* @param sampleSessionDataView
* @param fitFunctionView
* @param sessionOfStandardsSplinesWithOD
* @param bounds
*/
public SplineOverDispersionChooserPanel (//
JLayeredPane sampleSessionDataView,//
AbstractFitFunctionView fitFunctionView, //
SortedMap<Double, AbstractFunctionOfX> sessionOfStandardsSplinesWithOD,//
Rectangle bounds ) {
super( bounds );
this.sampleSessionDataView = sampleSessionDataView;
this.fitFunctionView = fitFunctionView;
this.sessionOfStandardsSplinesWithOD = sessionOfStandardsSplinesWithOD;
this.splineWithOdChoiceButtons = new JButton[sessionOfStandardsSplinesWithOD.size()];
setOpaque( true );
setCursor( Cursor.getDefaultCursor() );
fitFunctionButtonGroup = new ButtonGroup();
}
private void initRadioButtons() {
applicationDescButtonModel = new ToggleButtonModel();
appletDescButtonModel = new ToggleButtonModel();
compDescButtonModel = new ToggleButtonModel();
bg = new ButtonGroup();
applicationDescButtonModel.setGroup(bg);
appletDescButtonModel.setGroup(bg);
compDescButtonModel.setGroup(bg);
String desc = evaluator.getProperty(JNLP_DESCRIPTOR);
if (desc != null) {
if (desc.equals(DescType.application.toString())) {
applicationDescButtonModel.setSelected(true);
} else if (desc.equals(DescType.applet.toString())) {
appletDescButtonModel.setSelected(true);
} else if (desc.equals(DescType.component.toString())) {
compDescButtonModel.setSelected(true);
}
} else {
applicationDescButtonModel.setSelected(true);
}
}
public static JPanel enumToRadioPanel(Object defValue, Consumer<Object> listener) {
JPanel setP = new JPanel();
Object[] types = defValue.getClass().getEnumConstants();
ButtonGroup bg = new ButtonGroup();
setP.setLayout(new GridLayout(types.length, 1));
for (Object o : types) {
JRadioButton b = new JRadioButton(o.toString());
if(o.equals(defValue))
b.setSelected(true);
setP.add(b);
bg.add(b);
b.addChangeListener( ae -> {
if(b.isSelected())
listener.accept(o);
});
}
return setP;
}
@SuppressWarnings("LeakingThisInConstructor")
public MetalThemeMenu(String name, MetalTheme[] themeArray) {
super(name);
themes = themeArray;
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < themes.length; i++) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(themes[i].
getName());
group.add(item);
add(item);
item.setActionCommand(i + "");
item.addActionListener(this);
if (i == 0) {
item.setSelected(true);
}
}
}
@SuppressWarnings("LeakingThisInConstructor")
public MetalThemeMenu(String name, MetalTheme[] themeArray) {
super(name);
themes = themeArray;
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < themes.length; i++) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(themes[i].
getName());
group.add(item);
add(item);
item.setActionCommand(i + "");
item.addActionListener(this);
if (i == 0) {
item.setSelected(true);
}
}
}
/**
* Construct a new action.
*
* @param context Graph Node.
* @param buttonGroup The button group to which this action belongs.
*/
public DrawEdgesAction(final GraphNode context, final ButtonGroup buttonGroup) {
this.context = context;
this.buttonGroup = buttonGroup;
putValue(Action.SMALL_ICON, UserInterfaceIconProvider.EDGES.buildIcon(16));
putValue(Action.SHORT_DESCRIPTION, NbBundle.getMessage(this.getClass(), "CTL_DrawEdgesAction"));
putValue(Action.SELECTED_KEY, true);
}
/**
* Construct a new action.
*
* @param context Graph Node.
* @param buttonGroup The button group to which this action belongs.
*/
public DrawTransactionsAction(final GraphNode context, final ButtonGroup buttonGroup) {
this.context = context;
this.buttonGroup = buttonGroup;
putValue(Action.SMALL_ICON, UserInterfaceIconProvider.TRANSACTIONS.buildIcon(16));
putValue(Action.SHORT_DESCRIPTION, NbBundle.getMessage(this.getClass(), "CTL_DrawTransactionsAction"));
}
/**
*
*/
public MaintainConnectionDialog() {
super(SketchFrame.this, "Cannot Edit Sketch With Active Connection");
final ButtonGroup bg = new ButtonGroup();
keepConnection = new JRadioButton("Keep connection and edit with subset of tools");
throwConnection = new JRadioButton("Disconnect to have all tools");
throwConnection.setSelected(true);
bg.add(keepConnection);
bg.add(throwConnection);
setSize(435, 160);
showDialog();
}
private String getSelectedButton(ButtonGroup bGroup) {
for (Enumeration<AbstractButton> buttons = bGroup.getElements(); buttons.hasMoreElements();) {
AbstractButton button = buttons.nextElement();
if (button.isSelected()) {
return button.getText();
}
}
return "None";
}
private void createButtonsAndLabels() {
copyAllRButton = new JRadioButton("Copy");
geocodingRButton = new JRadioButton("Use Geocoding Only");
subsetRButton = new JRadioButton("Use Subset");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(copyAllRButton);
buttonGroup.add(geocodingRButton);
buttonGroup.add(subsetRButton);
copyAllRButton.setActionCommand(COPY_ALL_COMMAND);
geocodingRButton.setActionCommand(COPY_GEOCODING_COMMAND);
subsetRButton.setActionCommand(COPY_SUBSET_COMMAND);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateUI();
}
};
copyAllRButton.addActionListener(listener);
geocodingRButton.addActionListener(listener);
subsetRButton.addActionListener(listener);
if (subsetDef != null) {
subsetRButton.setSelected(true);
} else {
geocodingRButton.setSelected(true);
}
subsetButton = new JButton("Subset...");
subsetButton.addActionListener(createSubsetButtonListener());
labelWidthInfo = new JLabel(DEFAULT_NUMBER_TEXT);
labelHeightInfo = new JLabel(DEFAULT_NUMBER_TEXT);
labelCenterLatInfo = new JLabel(DEFAULT_LATLON_TEXT);
labelCenterLonInfo = new JLabel(DEFAULT_LATLON_TEXT);
}
/**
* toolbar for when you're looking at a grid
* @return
*/
private JToolBar buildGridToolbar() {
JToolBar toolBar = new JToolBar();
ButtonGroup modeButtonGroup = new ButtonGroup();
selectButton = makeRadioToolButton("select_mode.png", "", "Select (" +
KeyEvent.getKeyText(KeyEvent.VK_ESCAPE) + ")", this, modeButtonGroup);
toolBar.add(selectButton);
selectButton.setSelected(true);
addComponentButton = makeRadioToolButton("add_component.png", "", "Add Modules (M)", this, modeButtonGroup);
toolBar.add(addComponentButton);
toolBar.add(makeToolButton("", "editGridSize", "Edit Grid Size", this));
toolBar.addSeparator();
zoomButton = new JToggleButton();
zoomButton.setText("Zoom");
panButton = new JToggleButton();
panButton.setText("Pan");
toolBar.add(zoomButton);
toolBar.add(makeToolButton("", "unZoom", "Un-Zoom", this));
toolBar.add(panButton);
toolBar.addSeparator();
modelPanel = new ModelPanel(bioModel, modelEditor);
toolBar.add(modelPanel);
toolBar.setFloatable(false);
/*
compartmentList.setSelectedItem(bioModel.getDefaultCompartment());
compartmentList.addActionListener(this);
*/
return toolBar;
}
private ButtonGroup getRadioBTNsForStartState() {
ButtonGroup theStartStateRBTNGroup = new ButtonGroup();
JRadioButton theBTN = new JRadioButton("random start");
theBTN.setSelected( true );
theStartStateRBTNGroup.add(theBTN);
theStartStateRBTNGroup.add(new JRadioButton("4/3 start"));
theStartStateRBTNGroup.add(new JRadioButton("5/2 start"));
return theStartStateRBTNGroup;
}