Java- 사용자 정의 대화 상자를 만드는 방법은 무엇입니까?
-
16-09-2019 - |
문제
jframe에 버튼이 클릭하면 사용자 입력을 위해 여러 텍스트 영역으로 대화 상자가 팝업을 원합니다. 나는 이것을하는 방법을 알아 내려고 노력했지만 계속 혼란스러워한다. 누구든지 도울 수 있습니까?
해결책
맞춤형 동작 방식이 많이 필요하지 않으면 Joptionpane은 좋은 시간을 절약합니다. OK / 취소 옵션의 배치 및 현지화를 관리하며 자신의 클래스를 정의 할 필요없이 사용자 정의 대화 상자를 표시하는 빠른 방법입니다. 대부분의 경우 Joptionpane의 "메시지"매개 변수는 문자열이지만 jcomponent 또는 jcomponents도 전달할 수 있습니다.
예시:
JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JPasswordField password = new JPasswordField();
final JComponent[] inputs = new JComponent[] {
new JLabel("First"),
firstName,
new JLabel("Last"),
lastName,
new JLabel("Password"),
password
};
int result = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog", JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println("You entered " +
firstName.getText() + ", " +
lastName.getText() + ", " +
password.getText());
} else {
System.out.println("User canceled / closed the dialog, result = " + result);
}
다른 팁
원하는대로 대화 상자를 사용자 정의하기 위해이 간단한 클래스를 사용해보십시오.
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
public class CustomDialog
{
private List<JComponent> components;
private String title;
private int messageType;
private JRootPane rootPane;
private String[] options;
private int optionIndex;
public CustomDialog()
{
components = new ArrayList<>();
setTitle("Custom dialog");
setMessageType(JOptionPane.PLAIN_MESSAGE);
setRootPane(null);
setOptions(new String[] { "OK", "Cancel" });
setOptionSelection(0);
}
public void setTitle(String title)
{
this.title = title;
}
public void setMessageType(int messageType)
{
this.messageType = messageType;
}
public void addComponent(JComponent component)
{
components.add(component);
}
public void addMessageText(String messageText)
{
JLabel label = new JLabel("<html>" + messageText + "</html>");
components.add(label);
}
public void setRootPane(JRootPane rootPane)
{
this.rootPane = rootPane;
}
public void setOptions(String[] options)
{
this.options = options;
}
public void setOptionSelection(int optionIndex)
{
this.optionIndex = optionIndex;
}
public int show()
{
int optionType = JOptionPane.OK_CANCEL_OPTION;
Object optionSelection = null;
if(options.length != 0)
{
optionSelection = options[optionIndex];
}
int selection = JOptionPane.showOptionDialog(rootPane,
components.toArray(), title, optionType, messageType, null,
options, optionSelection);
return selection;
}
public static String getLineBreak()
{
return "<br>";
}
}
이 수업 Java 튜토리얼에서는 예제 및 API 링크와 함께 각 스윙 구성 요소를 자세히 설명합니다.
당신이 사용하는 경우 Netbeans Ide (이 시점의 최신 버전은 6.5.1입니다), 파일> 새 프로젝트를 사용하여 기본 GUI Java 응용 프로그램을 작성하고 Java 카테고리를 선택한 다음 Java 데스크탑 응용 프로그램을 선택할 수 있습니다.
일단 만들어지면 메뉴 선택을 사용하여 열 수있는 상자에 대한 정보가 포함 된 간단한 Bare Bones GUI 앱이 있습니다. 이를 필요에 맞게 조정하고 버튼 클릭에서 대화 상자를 열는 방법을 배울 수 있어야합니다.
대화 상자를 시각적으로 편집 할 수 있습니다. 거기에있는 항목을 삭제하고 일부 텍스트 영역을 추가하십시오. 당신이 갇히면 더 많은 질문으로 돌아 오십시오 :)
글쎄, 당신은 본질적으로 jdialog를 만들고, 텍스트 구성 요소를 추가하고, 그것을 보이게 만듭니다. 어떤 특정 비트를 좁히면 도움이 될 수 있습니다.
사용자 정의 대화 상자 API를 만들었습니다. 여기에서 확인하십시오 https://github.com/markmyword03/customdialog. 메시지 및 확인란을 지원합니다. JoptionPane에서와 마찬가지로 입력 및 옵션 대화 상자가 곧 구현됩니다.
CustomDialog API의 샘플 오류 대화 상자 :CustomDialog 오류 메시지