他们如何设计即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。 Frame1打开Frame2。 Frame2有一个JTextArea,我将在其中设置一些文本并对其进行编辑。按下第2帧中的ok按钮后,它将关闭,我希望文本在Frame1中的变量中。
或者说我是否想要制作字体选择器对话框。
JOptionPane对我来说不是一个选择。在frame2中,我将有一个HTML编辑器。在frame1中我有JTable。单击表格上的一行将使用HTML编辑器打开frame2。我为此目的使用SHEF。当我关闭frame2按OK / Save按钮时,我想在frame1中使用html文本String
。并相应地设置行内容。但是frame2可以是一个模态对话框。
答案 0 :(得分:4)
首先阅读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;
}
}
});
}
}
答案 1 :(得分:0)
您可以创建保存结果的特殊类。类似的东西:
public class Result {
private String result;
public void setResult(String result) { ... }
public String getResult() { ... }
}
在第一帧中创建此类的实例,并将其传递给第二帧。关闭第二帧集结果,然后第一帧可以得到它。
答案 2 :(得分:0)
将其设置为多个对话框而不是框架,然后阅读有关介体模式的信息:
http://blue-walrus.com/2013/06/mediator-pattern-in-swing/
您的基本问题是您拥有组件层次结构,并且一个分支上的组件想要与另一个远程分支上的组件进行通信。你需要某种中介对象来在这些遥远的分支之间进行交流。
答案 3 :(得分:0)
查看"Event Driven Programming":您可以发送事件,组件可以响应它们,而不是组件之间的紧密耦合(每个组件必须知道其他每个组件)。