import javax.swing.*; import java.awt.*; import java.awt.event.*; public class D1 extends JPanel implements ActionListener { JTextField left = new JTextField (); JTextField right = new JTextField (); JRadioButton plus = new JRadioButton("+"); JRadioButton minus = new JRadioButton("-"); JRadioButton multiple = new JRadioButton("*"); JRadioButton divide = new JRadioButton("/"); ButtonGroup group = new ButtonGroup(); JLabel result = new JLabel ("Result = "); D1 () { setLayout(new GridLayout(3, 2)); JPanel groupPane = new JPanel(); groupPane.setLayout (new GridLayout (1, 0)); group.add(plus); group.add(minus); group.add(multiple); group.add(divide); groupPane.add(plus); groupPane.add(minus); groupPane.add(multiple); groupPane.add(divide); plus.addActionListener (this); minus.addActionListener (this); multiple.addActionListener (this); divide.addActionListener (this); add(left); add(groupPane); add(right); add(result); } public static void main(String[] args) { JFrame frame = new JFrame ("Ejercicio 2"); D1 pane = new D1(); frame.getContentPane().add(pane, BorderLayout.CENTER); frame.addWindowListener (new WindowAdapter() { public void windowClosing (WindowEvent e) { System.exit(0); } } ); frame.pack(); frame.setVisible(true); } public void actionPerformed (ActionEvent e) { int a = Integer.parseInt (left.getText()); int b = Integer.parseInt (right.getText()); if (e.getSource() == plus) result.setText ("Resultado = " + (a+b)); if (e.getSource() == minus) result.setText ("Resultado = " + (a-b)); if (e.getSource() == multiple) result.setText ("Resultado = " + (a*b)); if (e.getSource() == divide) result.setText ("Resultado = " + (a/b)); } }