他们是如何设计的,例如JFileChooser?
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
如何设计带有信息交换的框架。例如,我有Frame1和Frame2。框架1打开框架2。 Frame2有一个JTextArea,我将在其中设置一些文本并进行编辑。在frame2中按下确定按钮后,它被关闭,我希望在Frame1中的变量中输入文本。
或者说是否要创建字体选择器对话框。
JOptionPane不是我的选择。在frame2中,我将有一个HTML编辑器。在frame1中,我有JTable。单击表格上的一行将使用HTML编辑器打开frame2。我为此使用SHEF。当我按OK / Save按钮关闭frame2时,我想要frame1中的html文本
String
。并相应地设置行内容。但是frame2可以是模式对话框。 最佳答案
首先通读The Use of Multiple JFrames: Good or Bad Practice?
然后阅读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;
}
}
});
}
}
关于java - 在两个JFrame之间交换信息的模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17292864/