首先阅读多个 JFrame 的使用:好的还是坏的做法?
然后阅读How to make dialogs。
JFileChooser
是一个组件,它有一个也显示对话框的方法。您的需求可能会有所不同,但这不是一个糟糕的模式,因为它不会让您的组件开始需要始终显示在对话框中
更新
你可以使用一个JOptionPane
import java.awt.EventQueue;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestOptionPane12 {
public static void main(String[] args) {
new TestOptionPane12();
}
public TestOptionPane12() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTextField field = new JTextField();
int option = JOptionPane.showConfirmDialog(null, field, "Fill it out", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
switch (option) {
case JOptionPane.OK_OPTION:
System.out.println("You entered " + field.getText());
break;
}
}
});
}
}
或者您可以创建一个更自定义的解决方案...
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestOptionPane12 {
public static void main(String[] args) {
new TestOptionPane12();
}
public TestOptionPane12() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
FieldsPane pane = new FieldsPane();
switch (pane.showDialog(null)) {
case JOptionPane.OK_OPTION:
String text = pane.getText();
System.out.println("You entered: " + text);
break;
}
}
});
}
protected class FieldsPane extends JPanel {
private JTextField field;
private int state = JOptionPane.CANCEL_OPTION;
public FieldsPane() {
setLayout(new GridBagLayout());
field = new JTextField(10);
add(field);
}
public String getText() {
return field.getText();
}
public int showDialog(Component parent) {
JButton btnOkay = new JButton("Ok");
JButton btnCancel = new JButton("Cancel");
JPanel buttons = new JPanel();
buttons.add(btnOkay);
buttons.add(btnCancel);
btnOkay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
state = JOptionPane.OK_OPTION;
Window win = SwingUtilities.getWindowAncestor((Component)e.getSource());
win.dispose();
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
state = JOptionPane.CANCEL_OPTION;
Window win = SwingUtilities.getWindowAncestor((Component)e.getSource());
win.dispose();
}
});
JDialog dialog = new JDialog(parent == null ? (Window)null : SwingUtilities.getWindowAncestor(parent), "Fill it out");
dialog.setModal(true);
dialog.add(this);
dialog.add(buttons, BorderLayout.SOUTH);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
return state;
}
}
}
更新了 JOptionPane 和 JEditorPane 示例
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestOptionPane12 {
public static void main(String[] args) {
new TestOptionPane12();
}
public TestOptionPane12() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JEditorPane editorPane = new JEditorPane("text/html", null);
JScrollPane scrollPane = new JScrollPane(editorPane);
scrollPane.setPreferredSize(new Dimension(200, 200));
int option = JOptionPane.showConfirmDialog(null, scrollPane, "Fill it out", JOptionPane.OK_CANCEL_OPTION, -1);
switch (option) {
case JOptionPane.OK_OPTION:
System.out.println("You entered " + editorPane.getText());
break;
}
}
});
}
}