gpt4 book ai didi

java - 如何计算单选按钮的下拉菜单并将结果保存到空白文本字段?

转载 作者:行者123 更新时间:2023-12-02 00:40:35 26 4
gpt4 key购买 nike

我想做的是允许用户从下拉菜单中选择一个选项,然后选择一个单选按钮,然后计算其结果以显示在文本字段中,或者我猜是一个新面板中。我不需要所有代码,因为我正在尝试自己学习 Java,但任何提示指针或示例代码都会很棒。基本上,如果他们选择下拉列表中的第一个选项和单选按钮,结果应该返回“低”,我一直在尝试使用 NetBeans,所以我认为我让自己更加困惑。我想知道是否可以将下拉变量存储为字符,例如 A-E,将单选按钮存储为 1-5,然后在计算按钮中执行 if 语句或其他内容?如何声明文本字段或面板以使结果显示在那里?就像 a*1 = 'low' b*2='low' e*5='high'

下拉菜单代表风险发生的可能性,单选按钮代表风险的后果。将两者相乘即可确定当前的风险。该程序是我用 PHP 编写的,但我正在尝试将其变成 java 程序,因为我正在学习 Java。

风险发生的可能性是有概率的。从远程概率来看,“近乎确定”选项的概率为 0.1、0.3、0.5、0.7 和 0.9。结果具有成本值(value)。对于没有成本值的最小影响或没有影响,第二个按钮的成本值<5、5-7%、7-10%,最后一个按钮的成本值>10%(无法实现关键里程碑。很容易在 PHP 中做,因为我能够创建一个保存这些值的数据库,然后在选择这两个选项时发送一个查询。只给它们提供数值以将两者相乘会更容易吗?

之所以有两种不同的数据收集是因为我试图让界面变得干净。如果您想了解该功能如何基于网络运行,该网站托管在 timesdomain.com。您必须注册,但它没有任何值(value),因为它很快就会被删除。不用担心您的密码被盗,因为它存储在带有 4 位盐的 40 位哈希中,因此我实际上无法知道它;)。我的大部分编程经验都是在数据库和php方面,所以整个java语言还是新的。我想让我的程序基于网络变成基于应用程序。我希望它更有意义。

这是我的代码。

package riskcalculator;

public class Gui extends javax.swing.JFrame {

/** Creates new form Gui */
public Gui() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

Consequence = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton4 = new javax.swing.JRadioButton();
jRadioButton5 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

jLabel1.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
jLabel1.setText("Risk Claculator");

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose a Liklihood", "Remote Chance", "Unlikely Chance", "Likely Chance", "Highly Likely Chance", "Near Certainty Chance" }));

Consequence.add(jRadioButton1);
jRadioButton1.setText("Minimal or no impact");

Consequence.add(jRadioButton2);
jRadioButton2.setText("Additional resources required, able to satisfy");

Consequence.add(jRadioButton3);
jRadioButton3.setSelected(true);
jRadioButton3.setText("Minor slip in key milestones; not able to meet need date");

Consequence.add(jRadioButton4);
jRadioButton4.setText("Major slip in key milestones or critical path impacted");

Consequence.add(jRadioButton5);
jRadioButton5.setText("Can't achieve key milestone");

jButton1.setText("Calculate Risk");
jButton1.setActionCommand("Calculate Risk");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jPanel2.setBorder(new javax.swing.border.MatteBorder(null));

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 186, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 70, Short.MAX_VALUE)
);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(26, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(jRadioButton3)
.addComponent(jRadioButton4)
.addComponent(jRadioButton5))
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(99, 99, 99)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(110, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(69, 69, 69)
.addComponent(jLabel1)
.addGap(92, 92, 92))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(80, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(29, 29, 29)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton5)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(29, 29, 29))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

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

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup Consequence;
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JRadioButton jRadioButton5;
// End of variables declaration
}

最佳答案

对于您当前手头的问题,简而言之,问题是如何在按下 JButton 时从 JComboBox 和 JRadioButton 集合中获取所选项目。您如何处理这些信息取决于您的程序逻辑,实际上不是这里的问题。要了解如何做到这一点,我建议您访问 Swing 教程以了解详细信息。简而言之,只需在组合框上调用 getSelectedItem 即可获得 JComboBox 中的选择。对于 JRadioButton 集合来说,这有点棘手,但我一直喜欢从将 JRadioButton 组合在一起的 ButtonGroup 中进行选择。下面的示例显示了此方法和组合框的方法:

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

import javax.swing.*;

public class GetInfoFromCombosRadios extends JPanel {
private static final String[] STUFF_A = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private static final String[] STUFF_B = {"Fe", "Fi", "Fo", "Fum"};
private JComboBox comboStuffA = new JComboBox(STUFF_A);
private ButtonGroup btnGroupStuffB = new ButtonGroup();

public GetInfoFromCombosRadios() {
JPanel radioPanel = new JPanel(new GridLayout(0, 1, 0, 5));
for (String stuffB : STUFF_B) {
JRadioButton radioBtn = new JRadioButton(stuffB);
radioBtn.setActionCommand(stuffB);
btnGroupStuffB.add(radioBtn);
radioPanel.add(radioBtn);
}

JPanel comboPanel = new JPanel();
comboPanel.add(comboStuffA);

JButton doItBtn = new JButton("Do It!");
doItBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
doItActionPerformed();
}
});
JPanel doItBtnPanel = new JPanel();
doItBtnPanel.add(doItBtn);

setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(comboPanel);
add(radioPanel);
add(doItBtnPanel);
}

private void doItActionPerformed() {
String comboSelection = comboStuffA.getSelectedItem().toString();
System.out.println("Combo Selection: " + comboSelection);

String radioSelection = "";
ButtonModel selectedBtnModel = btnGroupStuffB.getSelection();
if (selectedBtnModel == null) {
radioSelection = "No radio button selected yet";
} else {
radioSelection = selectedBtnModel.getActionCommand();
}
System.out.println("Radio selection: " + radioSelection);
}

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

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}

关于java - 如何计算单选按钮的下拉菜单并将结果保存到空白文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6514080/

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