- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望在我的应用程序中包含此部分,允许用户添加更多选项(以 JradioButton 的形式)。因此,默认情况下,我在 JradioButton 中向用户提供一些选项,如果他们添加更多选项(在应用程序的其他部分);我的 Jframe 应该通过 Setup_Equipment_Frame 方法自动添加选项(如下所示),其中它获取基本上是用户添加的选项的字符串数组。我面临一些困难。
<小时/>代码:
<小时/>public void Setup_Equipment_frame(String a[]) //String_Array of Newly added options
{
//creating default options
JRadioButton option1 = new JRadioButton("Visual Stadio");
JRadioButton option2 = new JRadioButton("Netbeans");
JRadioButton option3 = new JRadioButton("Eclipse");
//Creating the button group
ButtonGroup group = new ButtonGroup();
group.add(option1);
group.add(option2);
group.add(option3);
//setting the frame layout
setLayout(new FlowLayout());
for(int i=0; i<=a.length-1;i++) //loop for how many new options are added
{
if(a[i] != null) //if the array's current item is not null
{
JRadioButton NewButton1= new JRadioButton(""+a[i]); //Add the Button
add(NewButton1);
}
}
//adding the default options
add(option1);
add(option2);
add(option3);
pack();
}
<小时/>
现在它确实可以工作了。我添加了单选按钮,但是由于添加的按钮的名称都是“NewButton1”,我无法对它们进行任何控制,并且我只能访问最后创建的 JRadioButton 和默认按钮。我不知道用户可能会添加多少新选项。
我的问题是如何自动创建具有不同名称的 JRadioButton。
如果我的问题或代码令人困惑,我提前道歉。我没有那么丰富的经验。
谢谢
<小时/>感谢您的回答,在您的帮助下,我只需添加 JradioButtons 数组就解决了问题
对于那些可能面临同样问题的人,适合我的代码如下:
已解决:
public void Setup_Equipment_frame(String a[])
{
int number_of_options=1;//number of new options
for(int i=0; i<=a.length-1;i++)
{
if(a[i] != null){
number_of_options++;
}
}
JRadioButton []v=new JRadioButton[number_of_options];
setLayout(new FlowLayout());
for(int z=0; z<=number_of_options-1;z++)
{if(a[z] != null){
{
v[z]=new JRadioButton(a[z]);
add(v[z]);
}
}
}
}
<小时/>
非常感谢
最佳答案
Now it actually works. I add the radio button however since the name of the added buttons are all "NewButton1", I can't have any control over them and I have access to only last created JRadioButton and the default ones. I don't know how many new options user might add.
不完全是,您可能会将对象与变量混淆。了解 JRadioButton 对象没有名称,没有对象有名称,是的,它们被创建然后分配给名为 NewButton1 的本地变量,该变量的范围仅限于for 循环,因此无论变量的名称如何,它甚至不存在于 for 循环之外。
实际上,您的问题本质上可以归结为:我如何才能获得引用一堆新创建的对象,有几个不错的解决方案,包括使用 JRadioButton 的 ArrayList,并将每个按钮添加到列表中。或者,如果您想将每个 JRadioButton 与一个字符串相关联,请改用 Map<String, JRadioButton>
.
顺便说一句,您将需要学习和使用Java naming conventions 。变量名应全部以小写字母开头,而类名应以大写字母开头。学习并遵循这一点将使我们更好地理解您的代码,并且将使您更好地理解其他人的代码。
这是一个使用 ArrayList<JRadioButton>
的示例
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class AddRadioButtons extends JPanel {
private static final int PREF_W = 300;
private static final int PREF_H = 400;
// List that holds all added JRadioButtons
private List<JRadioButton> radioButtonList = new ArrayList<>();
// jpanel to hold radiobuttons in a verticle grid
private JPanel buttonPanel = new JPanel(new GridLayout(0, 1));
private JTextField radioBtnNameField = new JTextField(10);
public AddRadioButtons() {
// jpanel to add to jscrollpane
// nesting JPanels so that JRadioButtons don't spread out inside the scrollpane.
JPanel innerViewPanel = new JPanel(new BorderLayout());
innerViewPanel.add(buttonPanel, BorderLayout.PAGE_START);
JScrollPane scrollPane = new JScrollPane(innerViewPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// holds textfield and button for adding new radiobuttons
JPanel topPanel = new JPanel();
topPanel.add(radioBtnNameField);
Action addRBtnAction = new AddRadioBtnAction("Add Radio Button");
topPanel.add(new JButton(addRBtnAction));
radioBtnNameField.setAction(addRBtnAction);
// holds button to display selected radiobuttons
JPanel bottomPanel = new JPanel();
bottomPanel.add(new JButton(new PrintAllSelectedBtnAction("Print All Selected Buttons")));
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(topPanel, BorderLayout.PAGE_START);
add(bottomPanel, BorderLayout.PAGE_END);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
// I prefer to use AbstractAction in place of ActionListeners since
// they have a little more flexibility and power.
private class AddRadioBtnAction extends AbstractAction {
public AddRadioBtnAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent evt) {
String text = radioBtnNameField.getText();
JRadioButton rbtn = new JRadioButton(text);
radioButtonList.add(rbtn);
buttonPanel.add(rbtn);
buttonPanel.revalidate();
buttonPanel.repaint();
radioBtnNameField.selectAll();
}
}
private class PrintAllSelectedBtnAction extends AbstractAction {
public PrintAllSelectedBtnAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
for (JRadioButton radioBtn : radioButtonList) {
if (radioBtn.isSelected()) {
System.out.println(radioBtn.getActionCommand() + " is selected");
}
}
System.out.println();
}
}
private static void createAndShowGui() {
AddRadioButtons mainPanel = new AddRadioButtons();
JFrame frame = new JFrame("Add Radio Buttons");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
// run the Swing code in a thread-safe manner
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
关于java - 当用户添加更多项目时创建 JradioButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32386868/
我正在尝试制作一家餐厅的菜单,我提出这个问题的原因是因为我想要使用边框布局有4个面板。我有4个面板,每个面板名为westPanel,topPanel,eastPanel和bottomPanel。我已经
这个问题已经有答案了: Swings setSelected() not working for array of JRadioButtons [closed] (1 个回答) 已关闭10 年前。 这
我对这段代码有疑问当我单击文件并单击新时,新面板出现在屏幕上,当我想更改 JRadioBox 状态以更改标签状态时,标签状态发生变化,但面板也消失了:( public class MainClass
这是代码: String male = "Male"; String female = "Female"; JRadioButton checkGenderMale = new JRadio
我有两个 JRadioButton,每个都有 ImageIcon。由于我使用的 ImageIcons,我需要给出一个按钮被选中而另一个按钮未被选中的外观。为此,我试图禁用另一个按钮,它会自动将 Ima
我是 java GUI 编程新手,在处理该项目时,我收到错误无法在我的 JRadioButtons 的 addActionListener 上找到符号,我不太确定我做错了什么,因为我没有使用 JBut
我正在创建的代码涉及 JRadioButton 和 JComboBox。我希望在选择 JRadioButton 时启用 JComboBox,在未选择或取消选择时禁用 JComboBox。我的问题是,如
我是 Java 和一般编程新手。我正在尝试创建单选按钮,在选择时更改背景颜色。目前我正在使用 Eclipse IDE。 Eclipse 没有给我任何错误,我可以很好地运行 b/m 程序,单选按钮显示并
我有这样的简单检查: boolean isPressed; if ((jRadioButton1.isSelected() == false) || (jRadioButton2.isSelecte
我创建了一个 JDialog 框,其中有 2 个单选按钮,每当我单击另一个按钮时,它将更改 JLabel(例如:单击全职按钮时的月薪,单击兼职按钮时的时薪) 所以我的问题是我该怎么做?我是否为 rad
我试图让一组 JRadioButton 使用箭头键进行导航。我本来打算用 KeyListeners 手动实现这一点,但显然这种行为至少在过去 8 年里已经有效( http://bugs.sun.com
我希望在我的应用程序中包含此部分,允许用户添加更多选项(以 JradioButton 的形式)。因此,默认情况下,我在 JradioButton 中向用户提供一些选项,如果他们添加更多选项(在应用程序
我有一个菜单栏,上面有两个菜单。 在其中一个菜单上,我遇到了简单、中等、困难的困难。 当单击其中几个单选按钮时,它们都会保持选中状态。我的问题是:如何取消选中它们并仅确保一次可以保持其中一个按钮的选中
Java:我在 JPanel 中添加了 3 个 JRadioButtons,后者又被添加到 GraphicsProgram 中。当我运行程序时,我可以选择单选按钮(当我单击按钮时,它们会起作用)。但是
我有一个程序,它从文本文件中读取具有多项选择答案的问题,然后在 JOptionPane 中随机显示其中的一组(在新 Pane 中的每个问题)。在我的文本文件中,问题和 4 个答案选项都在一行中,然后我
我正在使用具有许多组件的 java 应用程序,但我遇到了 JRadioButton 的问题,它不是垂直方式,是否有任何其他方法可以在不使用 JPanel 的情况下使其垂直 什么是我可以使用的最好的布局
我编写这段代码是为了让用户在选择游戏模式时必须做出选择: JButton btnNewButton = new JButton("Start Game"); JRadioButton beginner
我遇到了一个问题,我的 JRadioButtons 在鼠标悬停 之前不会出现。我以前查过这个问题,似乎大多数人都使用 layouts 解决了它。但是,我不应该将它们用于我的作业,因为我对使用 Java
我知道在Java中的事件驱动程序中可以找出哪个对象引起了事件(例如选择了JRadioButton,因此将发生特定的操作)。我的问题是,如果按钮组中有 2 个 JRadioButton,并且都添加了操作
import java.awt.Frame; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import j
我是一名优秀的程序员,十分优秀!