2012-02-01 3 views
6

이 문제는 해결되었습니다.사용자 정의 차단 Java 스윙 프롬프트 만들기

나는 투영을 기반으로 한 Java Swing을 개발 중이며, & 모양이 완전히 사용자 정의되었습니다. 우리는 프로그램 전체에서 일관된 모양을 유지하려고 노력하고 있으며 기본 Java 대화 상자 창은 동일하지 않습니다.

현재 문제는 사용자 프롬프트에 대한 호출을 차단하는 컨트롤이 필요합니다. JOptionPane.showConfirmDialog()와 비슷합니다.이 경우 정적 호출은 창을 생성하고 사용자가 옵션을 선택할 때까지 프로그램의 흐름을 중지합니다. 또한 옵션의 값을 반환합니다. GUI 자체는 논리적으로 잠겨 있지 않지만 사용자는 나머지 부분과 상호 작용할 수 없습니다.

int n = JOptionPane.showConfirmDialog(this, 
    "Are you sure?", 
    "Confirm" 
    JOptionPane.YES_NO_OPTION); 

사용자 지정 모양 및 문자열을 사용하여이 기능을 복제하고 싶습니다. 이상적으로 나타납니다 내 코드는 다음과 같습니다

String result = CustomPrompt.showPrompt(this, 
    "Please enter your name", //Text 
    "Prompt.",    //Title 
    "John Smith");   //Default 

이것은 일반적으로 암호 입력에 사용, 내가 암호의 반환 형식이 다른 이해하지만이 한 예입니다됩니다. 이는 여러 클래스에서 일련의 버튼 및 이벤트 리스너를 사용하여 수행 할 수 있지만 코드의 가독성과 응용 프로그램의 안정성이 감소합니다.

프레임은 NetBeans를 통해 만들어지며 거기에서 사용자 정의됩니다. 나는 그런 프롬프트가 스윙에 이미 존재한다는 것을 이해한다. 그러나 그것은 &의 느낌이 완전히 완전히 다르다는 것을 알 수있다.

요약 된 질문 : 사용자 정의 프레임을 사용하여 사용자에게 입력을 차단하는 방법.

public class PromptForm extends JDialog 
{ 
    transient char[] result; 

    public char[] getResult() 
    { 
     return result; 
    } 
    public PromptForm(Frame parent) 
    { 
     super(parent, true); 
     initComponents(); 
    } 
    public void prompt() 
    { 
     this.setVisible(true); 
    } 
    public static char[] promptForPassword(Frame parent) 
    { 
     PromptForm pf = new PromptForm(parent); 
     pf.prompt(); 
     return pf.getResult(); 
    } 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    { 
     result = jPasswordField1.getPassword(); 
     setVisible(false); 
     dispose(); 
    } 
    private void initComponents() {...} 

    private javax.swing.JButton jButton1; 
    private javax.swing.JPasswordField jPasswordField1; 
} 

에 의해 호출 : CustomPromptJDialog 확장 확인

char [] ret = PromptForm.promptForPassword(this); 
    JOptionPane.showMessageDialog(this, new String(ret), "Neat", JOptionPane.QUESTION_MESSAGE); 
+0

JComponent (실제로는 모든 객체를 제공 할 수 있지만 대부분의 객체는 원하는대로 작동하지 않습니다)를 매개 변수로 지정할 수 있다는 것을 알고 계십니까? 'this '를 부모로 지정하고 (예를 들어 당신처럼) GUI의 나머지 부분은'잠궈 야한다 '. – Hidde

+0

분명히 사용자 정의 그림을 사용하고 있으며 사용자 정의 LaF를 사용하지 않고 있습니다. – mre

+1

네, 맞습니다. 실제로 실제로 LookAndFeel을로드하는 것이 아니라 Netbeans를 통해 각 구성 요소에 속성을 할당하는 것입니다. 좋은 운영 절차와 관계없이, 이것들은 제가 따라야하는 규칙들입니다. – Reivax

답변

5

, 그것은 소유자 Window 원하는 ModalityType을 통과 생성자 호출 super의이

이 문제에 대한 해결책은 다음과 같습니다.

public class CustomPrompt extends JDialog { 

    public static String showPrompt(Window parent, String title, String text, 
     String defaultText) { 
    final CustomPrompt prompt = new CustomPrompt(parent); 
    prompt.setTitle(title); 
    // set other components text 
    prompt.setVisible(true); 

    return prompt.textField.getText(); 
    } 

    private JTextField textField; 

    // private if you only want this prompt to be accessible via constructor methods 
    private CustomPrompt(Window parent) { 
    super(parent, Dialog.DEFAULT_MODALITY_TYPE); 
    // Java >= 6, else user super(Frame, true) or super(Dialog, true); 
    initComponents(); // like Netbeans 
    } 

    // initComponents() and irrelevant code. 
} 
+0

고마워요, 모달이 무엇입니까. 매우 감사. 내 솔루션 : JDialog를 확장하는 PromptForm 클래스를 만듭니다. 생성자 시그니처 : public PromptForm (프레임 부모) {super (parent, true); initComponents();} – Reivax