hello im a java newbie and i really need your help mates
i need a code that uses jradiobutton that will be use for selecting mathematical operations
-multiplication
-addition
-subtraction
-division
when i click addition , a JOptionPane will appear to add INputBox for inputing 1st Integer and another input box to input 2nd integer to be solved please can you modify this code regarding to my request!
i can still wait brothersCode:import javax.swing.*; import java.awt.event.*; import java.awt.*; public class justabasic extends JFrame{ double equals = 0.0; JLabel selectoperationLabel = new JLabel("Select Operation:"); JLabel firstdigitLabel = new JLabel("FIRST DIGIT:"); JLabel seconddigitLabel = new JLabel("SECOND DIGIT:"); JLabel outputLabel = new JLabel("Display Output"); JTextField firstDigit = new JTextField(5); JTextField secondDigit = new JTextField(5); JButton buttonOutput = new JButton("CLICK HERE"); JRadioButton rbMultiply = new JRadioButton("Multiply"); JRadioButton rbAddition = new JRadioButton("Addition"); JRadioButton rbSubtract = new JRadioButton("Subtraction"); JRadioButton rbDivision = new JRadioButton("Division"); public justabasic(){ super("I <3 EraserHead"); setSize(200,300); setVisible(true); ButtonGroup g = new ButtonGroup(); g.add(rbMultiply); g.add(rbAddition); g.add(rbSubtract); g.add(rbDivision); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container bigcon = getContentPane(); bigcon.setLayout(new GridLayout(3,3)); JPanel smallcon1 = new JPanel(); smallcon1.add(firstdigitLabel); smallcon1.add(firstDigit); smallcon1.add(seconddigitLabel); smallcon1.add(secondDigit); JPanel smallcon2 = new JPanel(); smallcon2.add(selectoperationLabel); smallcon2.add(rbMultiply); smallcon2.add(rbAddition); smallcon2.add(rbSubtract); smallcon2.add(rbDivision); smallcon2.add(buttonOutput); smallcon2.add(buttonOutput); smallcon2.add(outputLabel); bigcon.add(smallcon1); bigcon.add(smallcon2); Aksyon a = new Aksyon(); rbMultiply.addActionListener(a); rbAddition.addActionListener(a); rbSubtract.addActionListener(a); rbDivision.addActionListener(a); buttonOutput.addActionListener(a); } private class Aksyon implements ActionListener{ public void actionPerformed(ActionEvent evt){ if(evt.getSource()== rbMultiply){ double first = (double) (Double.parseDouble(firstDigit.getText())); double second = (double) (Double.parseDouble(secondDigit.getText())); equals = first * second; } else if(evt.getSource()== rbAddition){ double first = (double) (Double.parseDouble(firstDigit.getText())); double second = (double) (Double.parseDouble(secondDigit.getText())); equals = first + second; } else if(evt.getSource()== rbSubtract){ double first = (double) (Double.parseDouble(firstDigit.getText())); double second = (double) (Double.parseDouble(secondDigit.getText())); equals = first - second; } else if(evt.getSource()== rbDivision){ double first = (double) (Double.parseDouble(firstDigit.getText())); double second = (double) (Double.parseDouble(secondDigit.getText())); equals = first / second; } if(evt.getSource()== buttonOutput){ outputLabel.setText("WEEE!!" + equals); } } } private class ligaya implements ItemListener{ public void itemStateChanged(ItemEvent e){ } } public static void main(String [] args){ justabasic frame = new justabasic(); } }
Last edited by ZekeDragon; 03-04-2010 at 11:02 PM. Reason: Do Not Double Post
The people here won't do your work for you, however most people here will help you learn how to do it yourself. The first thing I'd do in the situation of your class is reformat it, for example in this situation:
It is very unclear that the constructor started here. If I weren't really looking for it I'd never notice that it's there. You should reformat the class in such a manner that it's very easy to tell how control flow works and where methods are vs where data members are. In fact, how it's formatted, your document looks like 3 separate classes with the first one having no method at all.Code:JRadioButton rbDivision = new JRadioButton("Division"); public justabasic(){ super("I <3 EraserHead"); setSize(200,300);
The second thing I'd do is look at how a JOptionPane works and start applying that to the ActionListener's ActionPerformed method. That's where you'd want to generate the two JOptionPanes to take in user input and where you'd want to perform the calculation.
Side Note: It's not considered kosher to double-post, especially after less than 2 hours from your original post. Just edit your post with anything additional you want, and be patient. There aren't many people on at this time of night (in the USA), so if it takes some time for a response don't be discouraged.
Wow I changed my sig!
thank you sir!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks