gpt4 book ai didi

java - 两个 JFrame 之间的 swing 信息交换模式

转载 作者:行者123 更新时间:2023-11-30 07:16:05 24 4
gpt4 key购买 nike

他们如何设计 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,我将在其中设置一些文本并对其进行编辑。在 frame2 中按下 ok 按钮后,它被关闭,我想要 Frame1 中变量中的文本。

或者说如果我想做一个字体选择器对话框。

JOptionPane 不适合我。在 frame2 中,我将有一个 HTML 编辑器。在 frame1 中,我有 JTable。单击表格上的一行将使用 HTML 编辑器打开 frame2。我正在使用 SHEF以此目的。当我按确定/保存按钮关闭 frame2 时,我想要 frame1 中的 html 文本 String。并相应地设置行内容。但是 frame2 可以是模态对话框。

最佳答案

首先通读 The Use of Multiple JFrames: Good or Bad Practice?

然后阅读 How to make dialogs .

JFileChooser 是一个具有显示对话框的方法的组件。您的需求可能不同,但这不是一个坏模式,因为它不会让您的组件开始需要始终显示在对话框中

已更新

您可以使用 JOptionPane

enter image description here

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;
}

}

});
}

}

或者您可以创建更加自定义的解决方案...

enter image description here

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 示例

enter image description here

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 之间的 swing 信息交换模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17292864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com