简体   繁体   中英

How do I implement JOptionPane with my problem?

For a homework problem, I have to use the JOptionPane class to get 2 values from a user, then run it through the methods I've created in a class file. However, even after reading through the class, I've no idea what to do. I'm not sure whether to implement the class methods into the class file I've created or in a new PairTester file. I'm really stuck with the implementation.

Requirements: besides the constructor values, I can use no param values or local variables.

import javax.swing.JOptionPane;

public class Pair{
  private double x;
  private double y;
public Pair(){}
public Pair(double x, double y){
       this.x=x;
       this.y=y;
}

        public double getSum(){
            return x + y;
}
        public double getDifference(){
            return x - y;
}
        public double getProduct(){
            return x * y;
}
        public double getQuotient(){
            return (x + y) / 2;
}
        public double getDistance(){
            return Math.abs(x - y);
}
        public double getMax(){
            return Math.max(x,y);
}
        public double getMin(){
            return Math.min(x,y);
}
        public double getx(){
            return x;
}
        public double gety(){
            return y;
}
        public String toString(){
            return "Pair[x = "+getx()+", y = "+gety()+"]";
}
}

You could achieve this by adding a panel to your dialog with the inputs desired. Dialogs can accept an object as their 2nd parameter which in this case you could pass a custom panel with your inputs.

public void getInputs()
{
        // Create the custom panel and inputs
        JPanel inputPanel = new JPanel(new GridLayout(2, 2));
        JTextField xInput = new JTextField();
        JTextField yInput = new JTextField();

        //Add the inputs and labels to the custom panel
        inputPanel.add(new JLabel("Enter X: "));
        inputPanel.add(xInput);
        inputPanel.add(new JLabel("Enter Y: "));
        inputPanel.add(yInput);

        //Display the dialog 
        int dialogResult = JOptionPane.showConfirmDialog(null, inputPanel, "Enter your X & Y values below");

        if(dialogResult == JOptionPane.OK_OPTION)
        {
            try
            {
                //Get the input values from our custom inputs in the panel
                x = Integer.parseInt(xInput.getText());
                y = Integer.parseInt(yInput.getText());
            }

            //Exception is thrown if user doesn't enter numbers
            catch(NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null, "Please only enter numbers");
            }
        }
    }
public class JOptionPaneMultiInput {
   public static void main(String[] args) {
      JTextField xField = new JTextField(5);
      JTextField yField = new JTextField(5);

      JPanel myPanel = new JPanel();
      myPanel.add(new JLabel("x:"));
      myPanel.add(xField);
      myPanel.add(Box.createHorizontalStrut(15)); // a spacer
      myPanel.add(new JLabel("y:"));
      myPanel.add(yField);

      int result = JOptionPane.showConfirmDialog(null, myPanel, 
               "Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
      if (result == JOptionPane.OK_OPTION) {
         Pair pair = new Pair(Double.valueOf(xField.getText()), Double.valueOf(yField.getText()));
         // call functions here like pair.getSum()
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM