0

So it works correctly but it is a bit confusing I would like to remove JOptionPane.showOptionDialog and simply add 2 buttons so that it is not so confusing in the code.............

I have tried with JButtons but could not make it And more than anything that fulfills the function of throwing the result

How can I use two buttons instead of JOptionPane.showOptionDialog?

float n, r;
int a = 1;
String input;

//Button content
Object[] options1 = {"°C -> °F", "°F -> °C", "Exit"};

int menu = JOptionPane.showOptionDialog(null, a + ". Select your conversion",
    "Temperature scale selection",
    JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
    null, options1, null);
//Button 2
if (menu == JOptionPane.YES_OPTION) {
    //Celsius to Fahrenheit
    input = JOptionPane.showInputDialog("Enter degrees Celsius: ");
    n = Integer.parseInt(input);
    r = ((9 * n) / 5) + 32;
    JOptionPane.showMessageDialog(null, r + " Fahrenheit");
} else if (menu == JOptionPane.YES_NO_CANCEL_OPTION) { //Button 3
    //Fahrenheit to Celsius 
    input = JOptionPane.showInputDialog("Enter degrees Fahrenheit ");
    n = Integer.parseInt(input);
    r = (n - 32) * 5 / 9;
    JOptionPane.showMessageDialog(null, r + " Celsius");
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jugger
  • 5
  • 1
  • Is one of the overloaded [`showConfirmDialog()`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html#showConfirmDialog-java.awt.Component-java.lang.Object-) methods not suitable? – Abra May 25 '20 at 09:21

1 Answers1

0

If you want to add a couple of JButtons to just display, that in itself is a bit of a process. To get one to display, you're going to need a JFrame and a JPanel. Once you have those two, you can add a new JButton to your JPanel. Adding multiple buttons and swing components requires the use of a layout for your JPanel. You can read more about how to use JPanels, examples of using JPanels, and layout managers.

After you have your buttons displayed on your panel, you have to add an ActionListener to each button so that it can perform the desired action. You can take a look at the JavaDocs or this thread for information on how to write one.

Getting buttons and other swing components basic functionality is a bit of a process, but it's the most basic way of achieving what you want.

parthlr
  • 386
  • 3
  • 13