두 가지 옵션이 있습니다. JOptionPane 메서드를 사용하여 Paul Tomblin
에 의해 상세하게 표시된 대화 상자를 표시하거나 직접 대화 상자를 빌드하십시오.
두 번째 옵션은 대화 상자를 세밀하게 제어 한 경우 필요합니다. 다른 이름이 필요한 경우 (JOptionPane.showOptionDialog를 사용하여 수행 할 수도 있음) 대화 상자의 단추 위치 또는 모달이 아닌 대화 상자가 필요한 경우
간단한 예 :
import java.awt.Color;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class DialogsTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JPanel p = new JPanel();
JPanel contentPane = new JPanel();
contentPane.add(p);
JFrame f = new JFrame();
f.setContentPane(contentPane);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
/*
* NOTE: It is not recomended to share the same instance of a component
* by different parents. Thought it is fine here since the first
* dialog will release it before the second will get it.
* But in a situation when we wouldn't make the 'dialog' modal and
* we would show it after the 'option pane dialog' it would be empty.
*/
JPanel message = new JPanel();
message.add(new JLabel("Label:"));
message.add(new JTextField("ABCD"));
message.setBackground(Color.GREEN);
JOptionPane.showConfirmDialog(f, message, "Default made dialog", JOptionPane.YES_NO_OPTION);
Object[] options = new String[]{"a", "b", "c"};
JOptionPane.showOptionDialog(f, message, "", JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
JDialog dialog = new JDialog(f, "Custom made dialog");
dialog.setModal(true);
dialog.setContentPane(message);
dialog.pack();
dialog.setLocationRelativeTo(f);
dialog.setVisible(true);
}
});
}
}
가 BTW 당신이 읽을 자바 튜토리얼에서 매우 좋은 (아마 너무 부자) 예를 들어 있습니다.
튜토리얼의 첫 번째 페이지에는 라디오 버튼, 탭 및 누름 버튼이있는 대화 상자가 표시됩니다. 그게 "여러 항목을 가지고있는"것이 아닌가? –
"표시"단추를 누를 때까지 대화 상자가 표시되지 않습니다. 표시되는 유일한 대화 상자는 이전에 표시된 간단한 대화 상자입니다. – Travis
더 많은 항목이있는 [one] (http://stackoverflow.com/questions/3002787/simple-popup-java-form-with-at-least-two-fields/3002830#3002830)이지만 실행해야합니다. 그것을 볼 수 있습니다. :-) – trashgod