gpt4 book ai didi

java - 让选定的单选按钮显示在文本区域中

转载 作者:行者123 更新时间:2023-11-30 08:13:36 26 4
gpt4 key购买 nike

我正在netbeans中制作一个程序,允许用户导入他们的计算机规范,我有2个单选按钮,当您选择选项时我希望它,并且当按下显示时它会在文本区域中显示您的选择。我已经有其他文本字段,人们可以在其中输入信息并显示到文本区域中,但我该如何为单选按钮执行此操作。

  private void displayActionPerformed(java.awt.event.ActionEvent evt) {                                        
// TODO add your handling code here:
textarea.append("Processor: " + processorTextField.getText() + "\nGraphics Card: "
+ graphicsCardTextField.getText() + "\nRam: " + ramTextField.getText() + "\nHard Drive: " +
hardDriveTextField.getText() + "\nOperating System: " + operatingSystemTextField.getText()
+ "\nMonitor Size: " + monitorTextField.getText());
}

这是我已经拥有的代码,用于在按下显示按钮时让其他文本字段进入文本区域

最佳答案

如果您已将 JRadioButton 添加到 ButtonGroup,则 ButtonGroup 可以通过调用 getSelection() 为您提供所选 JRadioButton 中的 ButtonModel。然后您可以获得模型的 actionCommand 字符串(必须为 JRadioButtons 显式设置)。例如,假设一个名为 ButtonGroup 的 ButtonGroup:

private void displayActionPerformed(java.awt.event.ActionEvent evt) { 

// 1st get the ButtonModel for the selected radio button
ButtonModel buttonModel = buttonGroup.getSelection();

// if a selection has been made, then model isn't null
if (buttonModel != null) {
// again actionCommand needs to be set for each JRadioButton
String actionCommand = buttonModel.getActionCommand();
// TODO: use actionCommand String as needed
}
}

例如:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class RadioEg extends JPanel {
private static final String[] RADIO_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private ButtonGroup buttonGroup = new ButtonGroup();

public RadioEg() {
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
for (String radioText : RADIO_TEXTS) {
JRadioButton radioButton = new JRadioButton(radioText);
radioButton.setActionCommand(radioText); // set this!
radioPanel.add(radioButton); // add to JPanel
buttonGroup.add(radioButton); // add to button group
}

JPanel southPanel = new JPanel();
southPanel.add(new JButton(new AbstractAction("GetSelection") {

@Override
public void actionPerformed(ActionEvent e) {
ButtonModel buttonModel = buttonGroup.getSelection();
if (buttonModel != null) {
String actionCommand = buttonModel.getActionCommand();
System.out.println("Selected Button: " + actionCommand);
}
}
}));

setLayout(new BorderLayout());
add(radioPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.PAGE_END);
}

private static void createAndShowGui() {
JFrame frame = new JFrame("RadioEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new RadioEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 让选定的单选按钮显示在文本区域中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29991222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com