下面列出了怎么用javax.swing.JRadioButton的API类实例代码及写法,或者点击链接到github查看源代码。
/**
*
* @param exponentialFitFofX
* @param parameterAValueSlider
* @param parameterBValueSlider
* @param parameterCValueSlider
* @param functionChoiceRadioButton
* @param bounds
*/
public ExponentialFitFunctionView (//
AbstractFunctionOfX exponentialFitFofX,//
ValueModelValueSlider parameterAValueSlider,//
ValueModelValueSlider parameterBValueSlider,//
ValueModelValueSlider parameterCValueSlider,//
JRadioButton functionChoiceRadioButton,//
Rectangle bounds ) {
super( exponentialFitFofX, functionChoiceRadioButton, bounds );
this.parameterAValueSlider = parameterAValueSlider;
this.parameterBValueSlider = parameterBValueSlider;
this.parameterCValueSlider = parameterCValueSlider;
}
void buildPanel() {
int count = this.model.getCount();
this.spinners[4].setVisible(count > 4);
for (int i = 0; i < count; i++) {
String text = this.model.getLabel(this, i);
Object object = this.spinners[i].getLabel();
if (object instanceof JRadioButton) {
JRadioButton button = (JRadioButton) object;
button.setText(text);
button.getAccessibleContext().setAccessibleDescription(text);
}
else if (object instanceof JLabel) {
JLabel label = (JLabel) object;
label.setText(text);
}
this.spinners[i].setRange(this.model.getMinimum(i), this.model.getMaximum(i));
this.spinners[i].setValue(this.values[i]);
this.spinners[i].getSlider().getAccessibleContext().setAccessibleName(text);
this.spinners[i].getSpinner().getAccessibleContext().setAccessibleName(text);
DefaultEditor editor = (DefaultEditor) this.spinners[i].getSpinner().getEditor();
editor.getTextField().getAccessibleContext().setAccessibleName(text);
this.spinners[i].getSlider().getAccessibleContext().setAccessibleDescription(text);
this.spinners[i].getSpinner().getAccessibleContext().setAccessibleDescription(text);
editor.getTextField().getAccessibleContext().setAccessibleDescription(text);
}
}
void buildPanel() {
int count = this.model.getCount();
this.spinners[4].setVisible(count > 4);
for (int i = 0; i < count; i++) {
String text = this.model.getLabel(this, i);
Object object = this.spinners[i].getLabel();
if (object instanceof JRadioButton) {
JRadioButton button = (JRadioButton) object;
button.setText(text);
button.getAccessibleContext().setAccessibleDescription(text);
}
else if (object instanceof JLabel) {
JLabel label = (JLabel) object;
label.setText(text);
}
this.spinners[i].setRange(this.model.getMinimum(i), this.model.getMaximum(i));
this.spinners[i].setValue(this.values[i]);
this.spinners[i].getSlider().getAccessibleContext().setAccessibleName(text);
this.spinners[i].getSpinner().getAccessibleContext().setAccessibleName(text);
DefaultEditor editor = (DefaultEditor) this.spinners[i].getSpinner().getEditor();
editor.getTextField().getAccessibleContext().setAccessibleName(text);
this.spinners[i].getSlider().getAccessibleContext().setAccessibleDescription(text);
this.spinners[i].getSpinner().getAccessibleContext().setAccessibleDescription(text);
editor.getTextField().getAccessibleContext().setAccessibleDescription(text);
}
}
/**
*
*/
public void refreshView() {
Enumeration buttons = includedFractions_buttonGroup.getElements();
while (buttons.hasMoreElements()) {
JRadioButton b = (JRadioButton) buttons.nextElement();
if (b.isSelected()) {
b.doClick();
break;
}
}
if (((TripoliSessionRawDataView) tripoliSessionRawDataView).isSAVED_YAXIS_IS_UNIFORM()) {
uniformYaxis.doClick();
} else {
independentYaxis.doClick();
}
if (((TripoliSessionRawDataView) tripoliSessionRawDataView).getSAVED_DATA_USED_FOR_SCALING().equals(IncludedTypeEnum.ALL)) {
allDataUsedForScaling.doClick();
} else {
includedDataUsedForScaling.doClick();
}
}
private void createExtraIcon() {
JRadioButton reference = new JRadioButton();
int iconTextGap = reference.getIconTextGap();
Icon disabledIcon = getDisabledIconSafe(reference);
Icon disabledSelectedIcon = getDisabledSelectedIconSafe(reference);
Icon icon = getIconSafe(reference);
Icon pressedIcon = getPressedIconSafe(reference);
Icon rolloverIcon = getRolloverIconSafe(reference);
Icon rolloverSelectedIcon = getRolloverSelectedIconSafe(reference);
Icon selectedIcon = getSelectedIconSafe(reference);
setDisabledIcon((disabledIcon == null) ? extraIcon : new DoubleIcon(disabledIcon, extraIcon, iconTextGap));
setDisabledSelectedIcon((disabledSelectedIcon == null) ? extraIcon
: new DoubleIcon(disabledSelectedIcon, extraIcon, iconTextGap));
setIcon((icon == null) ? extraIcon : new DoubleIcon(icon, extraIcon, iconTextGap));
setPressedIcon((pressedIcon == null) ? extraIcon : new DoubleIcon(pressedIcon, extraIcon, iconTextGap));
setRolloverIcon((rolloverIcon == null) ? extraIcon : new DoubleIcon(rolloverIcon, extraIcon, iconTextGap));
setRolloverSelectedIcon((rolloverSelectedIcon == null) ? extraIcon
: new DoubleIcon(rolloverSelectedIcon, extraIcon, iconTextGap));
setSelectedIcon((selectedIcon == null) ? extraIcon : new DoubleIcon(selectedIcon, extraIcon, iconTextGap));
}
@Override
protected String getNameForStep(int step) {
switch (step) {
case 0:
if (repoConfigPanels.entrySet().isEmpty()) {
return "local";
} else {
return "first";
}
case 1:
// go through the custom radio buttons and return the key of the selected button
for (Entry<String, Pair<RepositoryConfigurationPanel, JRadioButton>> entry : repoConfigPanels.entrySet()) {
if (entry.getValue().getSecond().isSelected()) {
return entry.getKey();
}
}
default:
throw new IllegalArgumentException("Illegal index: " + step);
}
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("JRadioButton Example");
check = new JRadioButton("Check here ");
check2 = new JRadioButton("Or check here");
ButtonGroup bg = new ButtonGroup();
bg.add(check);
bg.add(check2);
frame.getContentPane().add(check);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(check2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Thread.sleep(2000);
bg.clearSelection();
}
CheckboxDelegate() {
super();
cb = new JCheckBox() {
@Override
public boolean hasFocus() {
return getTarget().hasFocus();
}
};
rb = new JRadioButton() {
@Override
public boolean hasFocus() {
return getTarget().hasFocus();
}
};
setLayout(null);
setRadioButton(false);
add(rb);
add(cb);
}
void buildPanel() {
int count = this.model.getCount();
this.spinners[4].setVisible(count > 4);
for (int i = 0; i < count; i++) {
String text = this.model.getLabel(this, i);
Object object = this.spinners[i].getLabel();
if (object instanceof JRadioButton) {
JRadioButton button = (JRadioButton) object;
button.setText(text);
button.getAccessibleContext().setAccessibleDescription(text);
}
else if (object instanceof JLabel) {
JLabel label = (JLabel) object;
label.setText(text);
}
this.spinners[i].setRange(this.model.getMinimum(i), this.model.getMaximum(i));
this.spinners[i].setValue(this.values[i]);
this.spinners[i].getSlider().getAccessibleContext().setAccessibleName(text);
this.spinners[i].getSpinner().getAccessibleContext().setAccessibleName(text);
DefaultEditor editor = (DefaultEditor) this.spinners[i].getSpinner().getEditor();
editor.getTextField().getAccessibleContext().setAccessibleName(text);
this.spinners[i].getSlider().getAccessibleContext().setAccessibleDescription(text);
this.spinners[i].getSpinner().getAccessibleContext().setAccessibleDescription(text);
editor.getTextField().getAccessibleContext().setAccessibleDescription(text);
}
}
/** Get the current value in the entry with the given name
* and return as a boolean. If the entry is not a checkbox,
* then throw an exception.
* @return The state of the checkbox.
* @exception NoSuchElementException If there is no item with the
* specified name. Note that this is a runtime exception, so it
* need not be declared explicitly.
* @exception IllegalArgumentException If the entry is not a
* checkbox. This is a runtime exception, so it
* need not be declared explicitly.
*/
public boolean getBooleanValue(String name)
throws NoSuchElementException, IllegalArgumentException {
Object result = _entries.get(name);
if (result == null) {
throw new NoSuchElementException(
"No item named \"" + name + "\" in the query box.");
}
if (result instanceof JRadioButton) {
return ((JRadioButton) result).isSelected();
} else {
throw new IllegalArgumentException(
"Item named \""
+ name
+ "\" is not a radio button, and hence does not have "
+ "a boolean value.");
}
}
void buildPanel() {
int count = this.model.getCount();
this.spinners[4].setVisible(count > 4);
for (int i = 0; i < count; i++) {
String text = this.model.getLabel(this, i);
Object object = this.spinners[i].getLabel();
if (object instanceof JRadioButton) {
JRadioButton button = (JRadioButton) object;
button.setText(text);
button.getAccessibleContext().setAccessibleDescription(text);
}
else if (object instanceof JLabel) {
JLabel label = (JLabel) object;
label.setText(text);
}
this.spinners[i].setRange(this.model.getMinimum(i), this.model.getMaximum(i));
this.spinners[i].setValue(this.values[i]);
this.spinners[i].getSlider().getAccessibleContext().setAccessibleName(text);
this.spinners[i].getSpinner().getAccessibleContext().setAccessibleName(text);
DefaultEditor editor = (DefaultEditor) this.spinners[i].getSpinner().getEditor();
editor.getTextField().getAccessibleContext().setAccessibleName(text);
this.spinners[i].getSlider().getAccessibleContext().setAccessibleDescription(text);
this.spinners[i].getSpinner().getAccessibleContext().setAccessibleDescription(text);
editor.getTextField().getAccessibleContext().setAccessibleDescription(text);
}
}
/**
* Creates the entity mode button.
*/
private void createEntityModeButton() {
this.entityModeButton = new JRadioButton("Entities", this.viewMode == MODE_ENTITIES);
this.entityModeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JRadioButton radioButton = (JRadioButton) e.getSource();
if (!radioButton.isSelected() || viewMode == MODE_ENTITIES) {
// If the radio button is unselected, there is no need to refresh the UI.
// If the view mode is already entities view mode, there is no need to refresh the UI.
return;
}
viewMode = MODE_ENTITIES;
resetTabbedPane();
display();
}
});
}
static ComponentBuilder getBuilder(Instance instance, Heap heap) {
if (DetailsUtils.isSubclassOf(instance, JButton.class.getName())) {
return new JButtonBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JCheckBox.class.getName())) {
return new JCheckBoxBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JRadioButton.class.getName())) {
return new JRadioButtonBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JToggleButton.class.getName())) {
return new JToggleButtonBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JCheckBoxMenuItem.class.getName())) {
return new JCheckBoxMenuItemBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JRadioButtonMenuItem.class.getName())) {
return new JRadioButtonMenuItemBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JMenu.class.getName())) {
return new JMenuBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JMenuBar.class.getName())) {
return new JMenuBarBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JMenuItem.class.getName())) {
return new JMenuItemBuilder(instance, heap);
}
return null;
}
CheckboxDelegate() {
super();
cb = new JCheckBox() {
@Override
public boolean hasFocus() {
return getTarget().hasFocus();
}
};
rb = new JRadioButton() {
@Override
public boolean hasFocus() {
return getTarget().hasFocus();
}
};
setLayout(null);
setRadioButton(false);
add(rb);
add(cb);
}
public void selectRadioButtonNotSelected() throws InterruptedException {
final LoggingRecorder lr = new LoggingRecorder();
siw(new Runnable() {
@Override
public void run() {
List<Component> comps = ComponentUtils.findComponents(JRadioButton.class, frame);
JRadioButton button = (JRadioButton) comps.get(2);
RToggleButton rButton = new RToggleButton(button, null, null, lr);
button.setSelected(false);
rButton.mouseEntered(null);
button.setSelected(true);
rButton.mouseClicked(null);
}
});
Call call = lr.getCall();
AssertJUnit.assertEquals("select", call.getFunction());
AssertJUnit.assertEquals("true", call.getState());
}
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);
}
public RadioColumnEditor() {
super();
theRadioButton = new JRadioButton();
theRadioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
fireEditingStopped();
}
});
}
private static Icon getRolloverIconSafe(JRadioButton radio) {
Icon icon = radio.getIcon();
if (icon == null) {
return getDefaultIcon();
}
Icon rolloverIcon = radio.getRolloverIcon();
return (rolloverIcon != null) ? rolloverIcon : getIconSafe(radio);
}
private ActionListener getDbTypeListener() {
return e -> {
final JRadioButton theJRB = (JRadioButton) e.getSource();
boolean isPostgreDb = theJRB.getName().equals(CollectDBDriver.POSTGRESQL.name());
enableDBOptions(isPostgreDb);
};
}
private void createMatchByRegexControl() {
matchByRegexRadio = new JRadioButton();
addRadioButton(matchByRegexRadio, MATCH_BY_REGEX_NAME, true);
matchByRegexFieldRegex = new ZapTextField();
addLabelWithTextField(matchByRegexFieldRegex, MATCH_BY_REGEX_LABEL_REGEX);
matchByRegexFieldTag = new ZapTextField();
addLabelWithTextField(matchByRegexFieldTag, MATCH_BY_REGEX_LABEL_TAG);
}
/**
* 处理被选中的 zone 单选框
*
* @param group the group
* @param button the button
*/
private void addZoneRadioButton(@NotNull ButtonGroup group, JRadioButton button) {
group.add(button);
ActionListener actionListener = e -> {
Object sourceObject = e.getSource();
if (sourceObject instanceof JRadioButton) {
JRadioButton sourceButton = (JRadioButton) sourceObject;
zoneIndexTextFiled.setText(String.valueOf(sourceButton.getMnemonic()));
testMessage.setText("");
testButton.setText("Test Upload");
}
};
button.addActionListener(actionListener);
}
/** Sets the up function panel. */
protected void setUpFunctionPanel() {
JPanel panelFunction = new JPanel();
panelFunction.setBorder(null);
panelFunction.setLayout(new BoxLayout(panelFunction, BoxLayout.X_AXIS));
rdbtnFunction =
new JRadioButton(
Localisation.getString(
ExpressionPanelv2.class, "ExpressionPanelv2.function"));
rdbtnFunction.setMinimumSize(new Dimension(100, 20));
rdbtnFunction.setPreferredSize(new Dimension(100, 20));
panelFunction.add(rdbtnFunction);
rdbtnFunction.setActionCommand(FUNCTION);
buttonGroup.add(rdbtnFunction);
functionPanel =
new FunctionField(
new SubPanelUpdatedInterface() {
@Override
public void updateSymbol() {
buttonGroup.setSelected(rdbtnFunction.getModel(), true);
updateButtonState(true);
}
@Override
public void parameterAdded() {
if (parentObj != null) {
parentObj.dataApplied();
}
}
},
FunctionManager.getInstance());
panelFunction.add(functionPanel);
box.add(panelFunction);
}
public void setValue(Object o) {
Component[] c = getComponents();
for (int i = 0; i < c.length; i++) {
if (c[i] instanceof JRadioButton) {
if (((JRadioButton) c[i]).getText().equals(o)) {
((JRadioButton) c[i]).setSelected(true);
} else {
//Necessary for renderer, its buttons don't fire changes
((JRadioButton) c[i]).setSelected(false);
}
}
}
}
/**
* Returns the amount to indent the specified component if it's a JCheckBox
* or JRadioButton. If the component is not a JCheckBox or JRadioButton, 0
* will be returned.
*/
int getButtonChildIndent(JComponent c, int position) {
if ((c instanceof JRadioButton) || (c instanceof JCheckBox)) {
AbstractButton button = (AbstractButton) c;
Insets insets = c.getInsets();
Icon icon = getIcon(button);
int gap = button.getIconTextGap();
if (isLeftAligned(button, position)) {
return insets.left + icon.getIconWidth() + gap;
} else if (isRightAligned(button, position)) {
return insets.right + icon.getIconWidth() + gap;
}
}
return 0;
}
@Test
public void testByValueAlignedStructure() throws Exception {
init(emptyStructure, pgmRootCat, false);
CompEditorPanel editorPanel = (CompEditorPanel) getPanel();
addDataType(ByteDataType.dataType);
addDataType(CharDataType.dataType);
addFlexDataType(DWordDataType.dataType, null, null);
waitForSwing();
pressButtonByName(editorPanel, "Internally Aligned");
JTextField minAlignField =
(JTextField) getInstanceField("minAlignValueTextField", editorPanel);
assertNotNull(minAlignField);
JRadioButton byValueMinAlignButton =
(JRadioButton) getInstanceField("byValueMinAlignButton", editorPanel);
assertNotNull(byValueMinAlignButton);
pressButton(byValueMinAlignButton);
assertEquals("4", minAlignField.getText());
assertEquals(false, structureModel.viewComposite.isDefaultAligned());
assertEquals(false, structureModel.viewComposite.isMachineAligned());
assertEquals(4, structureModel.getMinimumAlignment());
assertEquals(2, structureModel.getNumComponents());
assertEquals(4, structureModel.getRowCount());
assertEquals(2, structureModel.getNumComponents());
assertEquals(4, structureModel.getRowCount());
checkRow(0, 0, 1, "db", ByteDataType.dataType, "", "");
checkRow(1, 1, 1, "char", CharDataType.dataType, "", "");
checkBlankRow(2);
checkRow(3, 4, 0, "ddw[0]", DWordDataType.dataType, "", "");
assertLength(4);
assertActualAlignment(4);
}
private NewRepositoryDialog() {
super(RapidMinerGUI.getMainFrame(), "repositorydialog", true, new Object[]{});
Box firstPage = new Box(BoxLayout.Y_AXIS);
ButtonGroup checkBoxGroup = new ButtonGroup();
Map<String, Component> cards = new HashMap<String, Component>();
cards.put("first", firstPage);
cards.put("local", localRepositoryPanel);
// register a radio button for each custom repository type
for (CustomRepositoryFactory factory : CustomRepositoryRegistry.INSTANCE.getFactories()) {
// some repos may not want to appear here
if (!factory.showRepositoryConfigurationInNewRepositoryDialog()) {
continue;
}
String key = factory.getI18NKey();
RepositoryConfigurationPanel repositoryConfigurationPanel = factory.getRepositoryConfigurationPanel();
JRadioButton radioButton = new JRadioButton(new ResourceActionAdapter(key));
radioButton.setEnabled(factory.enableRepositoryConfiguration());
radioButton.setSelected(repoConfigPanels.isEmpty());
repoConfigPanels.put(key, new Pair<>(repositoryConfigurationPanel, radioButton));
checkBoxGroup.add(radioButton);
firstPage.add(radioButton);
cards.put(factory.getI18NKey(), repositoryConfigurationPanel.getComponent());
}
firstPage.add(Box.createVerticalGlue());
layoutDefault(cards);
}
private JRadioButton createDoubleClickRadioButton(String title,
final int action) {
JRadioButton node = new JRadioButton(title);
node.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setDoubleClick(action);
}
});
return node;
}
/**
*
* @param meanFitFofX
* @param parameterAValueSlider
* @param functionChoiceRadioButton
* @param bounds
*/
public MeanFitFunctionView (//
AbstractFunctionOfX meanFitFofX,//
ValueModelValueSlider parameterAValueSlider,//
JRadioButton functionChoiceRadioButton,//
Rectangle bounds ) {
super( meanFitFofX, functionChoiceRadioButton, bounds );
this.parameterAValueSlider = parameterAValueSlider;
}
public String getSelectedDeployment() {
if (!hasDeployments()) return null;
for (Enumeration e=deploymentsButtonGroup.getElements(); e.hasMoreElements();) {
JRadioButton radio = (JRadioButton)e.nextElement();
if (radio.getModel() == deploymentsButtonGroup.getSelection()) {
return radio.getText();
}
}
throw new IllegalStateException("No deployment selected");
}
public ObjectViewerPanel() {
setLayout(new BorderLayout());
textpane = new JTextArea();
textpane.setLineWrap(true);
textpane.setEditable(false);
textpane.setWrapStyleWord(true);
add(new JScrollPane(textpane),BorderLayout.CENTER);
JPanel panel = new JPanel();
add(panel, BorderLayout.NORTH);
rdoJson = new JRadioButton("Json");
rdoJson.setSelected(true);
rdoMemory = new JRadioButton("Memory");
rdoBeanUtils = new JRadioButton("Bean");
ButtonGroup group = new ButtonGroup();
group.add(rdoJson);
group.add(rdoMemory);
group.add(rdoBeanUtils);
panel.add(rdoJson);
panel.add(rdoMemory);
panel.add(rdoBeanUtils);
rdoJson.addItemListener(il->show(currentObject));
rdoMemory.addItemListener(il->show(currentObject));
rdoBeanUtils.addItemListener(il->show(currentObject));
}