How to calculate a drop down to a radio button and have the results to a blank text field?
What I am trying to do is allow the user to select an option from a drop down menu and then select a radio button and then calculate their results to appear in a textfield or I guess a new panel. I don't want all of the code, because I am trying to learn Java myself, but any tips pointers or sample code would be great. Basically, if they choose the first option on the drop down and the radio button the result should return a 'LOW', I've been trying to use NetBeans, so I think I am confusing myself more. I'm wondering if I can store the drop down variables as a character, such as A-E and the radio buttons as 1-5 and then in the calculate button do an if statement or something? How do I declare the textfield or panel to get the results to show there? So like a*1 = 'low' b*2='low' e*5='high'
The drop down menu represents the likelihood of the risk happening, and the radio buttons represents the consequence of the risk. The two get multiplied together to determine the risk at hand. The program is something I made in PHP but I am trying to make it into a java program as I am trying to learn Java.
The likelihood of the risk happening holds a probability. From the Remote chance probability is .1, .3, .5, .7, and .9 chance for a 'Near Certainty' option. The consequence holds a cost value. For minimal or no impact has no cost value, the 2nd button holds a cost value of <5, 5-7%, 7-10% and >10% for the last button (Can't achieve key milestone. It was easy to do in PHP as I was able to just make a database that holds these values and then send a query when the two choices were chosen. Would it be easier to just give them numeric values to multiply the two?
The reason for the two different data collections was because I was trying to make the interface clean. The website is hosted at timesdomain.com if you'd care to take a look at how the function runs web based. You'll have to register, but it holds no value as it is being deleted soon. Don't worry about your password being taken because it is stored in a 40 bit hash with a 4 digit salt, so there is literally no way for me to know it ;). I have most of my programming experience in databases and php, so the whole java language is still new. I'd like to make my program I made web based into an application based. I hope it makes more sense.
Here is my code.
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()开发者_Python百科;
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
}
For your current problem at hand, the issue in a nutshell is how to get the selected item from a JComboBox and a collection of JRadioButtons when a JButton is pressed. What you do with this information is dependent on your program logic and really isn't the issue here. To learn do to this, I suggest that you go to the Swing tutorials for details. Briefly though, the selection from a JComboBox is obtained simply by calling getSelectedItem on the combo box. For a JRadioButton collection it's a little trickier, but I've always liked getting the selection from the ButtonGroup that holds the JRadioButtons together. The methods for this and for the combo box are shown in the example below:
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();
}
});
}
}
From scanning the code, it looks like you've got the components initialized and laid out in the container.
In order to make the interface interactive, you need to look into the other big part of Java Swing interfaces, the event listeners.
Basically, everything you do (clicks, value changes, moving the mouse around, pressing keys, etc.) generates an event. You then write Event Listeners that react to those events.
Check out this intro to the topic.
精彩评论