UFO ET IT

JOptionPane.showInputDialog의 다중 입력

ufoet 2020. 12. 6. 22:25
반응형

JOptionPane.showInputDialog의 다중 입력


JOptionPane.showInputDialog하나의 입력 대신 여러 입력을 생성하는 방법이 있습니까?


예. 당신은 당신이 어떤을 넣을 수 있다는 것을 알고 ObjectObject대부분의 매개 변수 JOptionPane.showXXX methods, 종종 그 Object을 될 일이 JPanel.

귀하의 상황에서 아마도 JPanel몇 가지가있는를 사용할 수 있습니다 JTextFields.

import javax.swing.*;

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) {
         System.out.println("x value: " + xField.getText());
         System.out.println("y value: " + yField.getText());
      }
   }
}

이것이 내 해결책이다

JTextField username = new JTextField();
JTextField password = new JPasswordField();
Object[] message = {
    "Username:", username,
    "Password:", password
};

int option = JOptionPane.showConfirmDialog(null, message, "Login", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
    if (username.getText().equals("h") && password.getText().equals("h")) {
        System.out.println("Login successful");
    } else {
        System.out.println("login failed");
    }
} else {
    System.out.println("Login canceled");
}

참고 URL : https://stackoverflow.com/questions/6555040/multiple-input-in-joptionpane-showinputdialog

반응형