gpt4 book ai didi

java - 如何使JFrame模态像JOptionPane?

转载 作者:行者123 更新时间:2023-11-30 06:32:30 25 4
gpt4 key购买 nike

首先,我将提出问题,然后解释该应用程序。

如何使JFrame表现得像JOptionPanes? (tutorial didn't help),即

[Show content >> return a value]。另外,我需要它

[Show the frame >> Ask information >> Modify an object passed as parameter to the constructor >> return something]

我已经使“确定”按钮获得了我想要的答案(在控制台中显示),但是我不知道在哪里放置return语句

我希望功能像

public static String getAnswer(Args, Answer)


Args,可能包含字段名称,数据类型,最大长度和

Answer,是在用户提供信息并单击“确定”后要修改的对象

总有论据和答案。例如(查看屏幕截图,以查看我需要如何显示“最复杂的”消息类型,但是它并不完整,因为它需要根据数据类型显示不同种类的组件)。

| Arg                  |   Answer     |
--------------------------------------
| Type | fldName | |
--------------------------------------
| int | Age: | 22 |
| String | Name: | Roger |
| Date | Birth: | 31/10/1989 |


到目前为止,我要做的是显示一个具有所需内容的JFrame(即对话框),并创建一个按钮以在控制台中向我显示答案。

如何构建JFrame?我有4个类,其中三个类在屏幕截图下方进行了描述,最后一个类是构建一个包含其他三个面板的JPanel并将其添加到JFrame中。如果要查看代码 click

为什么我不使用JDialog,因为它需要一个我没有的父框架。我需要从另一个非Java应用程序中显示此内容,因此,它必须已经是最上面的框架。


您可能不想阅读这篇文章,但是如果您有兴趣阅读我在做什么,请继续:

我知道,通过使用 JOptionPane.showMessageDialog()等,我的问题可能会解决,但我无法解决让它满足我的需求的问题。 JOptionPane上显示的内容似乎仅限于我,而且我不知道如何保持控制力,也不知道我放入它的孩子,一切都会改变其行为。

我必须创建五个不同的对话框,这些对话框会将值返回到另一个应用程序(痛苦的oracle形式6i)。对话框的类型为:


打印对话框,其中显示打印机列表或选项“导出到文件”
消息对话框,显示html内容(如电子邮件预览),并带有“确定”按钮。
是/否对话框,要求用户确认,其作用类似于消息对话框。
参数形式,要求用户提供一些信息,它要求的信息是基于选择或字符串数​​组的。
基于select语句的值列表显示结果,就像plsql开发人员一样。


这是最后一项( Parameter form)的屏幕截图,在示例中,evey项的类型为Month,答案将用其值填充。



如您所见,该框架分为3个块(我将其划分为3个类 TopPanelMidPanelBotPanel):消息对话框,用户输入和命令按钮。

TopPanel,从JScrollPane扩展并创建JEditorPane,因为它可能显示html内容

MidPanel,从JScrollPane扩展(仅针对列表中的最后两个对话框创建),并基于一个对象创建字段,该对象作为参数输入给用户,而表单的输入则需要在java中进行了检查,因为它可以是日期,textField或基于select语句的组合框(在图中,基于对数据库的查询,一年中有12个月)。

BotPanel具有用于java给出的答案的控件,例如参数列表或“是/否”对话框的答案。此面板随各种对话框而变化。例如对于是/否对话框,它具有“是”和“否”按钮,这些按钮将使java返回true或false,但是对于参数形式对话框,如果出现,它将返回错误消息,并且包含包含以下信息的对象用户选择了。

最佳答案

同样,您可以将任何复杂的GUI放入JOptionPane中。 JOptionPane show方法的第二个参数采用一个Object,该Object可以是任何Swing组件。例如:

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

import com.roots.map.MapPanel.ControlPanel;

public class ComplexDialog extends JPanel {
public static final String[] COMBO_LABELS = { "Nombre 1",
"Identificacion 1", "Fecha 1", "Empresa 1", "Nombre 2",
"Identificacion 2", "Fecha 2", "Empresa 2", "Nombre 3",
"Identificacion 3", "Fecha 3", "Empresa 3", "Nombre 4",
"Identificacion 4", "Fecha 4", "Empresa 4", "Nombre 5",
"Identificacion 5", "Fecha 5", "Empresa 5", "Nombre 6",
"Identificacion 6", "Fecha 6", "Empresa 6", "Nombre 7",
"Identificacion 7", "Fecha 7", "Empresa 7" };
public static final String[] COMBO_ITEMS = { "January", "February", "March",
"April", "May", "June", "July", "August", "September", "October",
"November", "December" };
private JTextArea textarea = new JTextArea(15, 30);
private Map<String, JComboBox> comboMap = new HashMap<String, JComboBox>();

public ComplexDialog() {
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
for (int i = 0; i < 100; i++) {
textarea.append("This is a really large text. ");
}

JPanel comboPanel = new JPanel(new GridBagLayout());
for (int i = 0; i < COMBO_LABELS.length; i++) {
addToComboPanel(comboPanel, COMBO_LABELS[i], i);
}

int eb = 5;
setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(new JScrollPane(textarea));
add(Box.createVerticalStrut(5));
JScrollPane comboPanelScroll = new JScrollPane(comboPanel);
add(comboPanelScroll);

comboPanelScroll.getViewport().setPreferredSize(
textarea.getPreferredSize());
}

private void addToComboPanel(JPanel comboPanel, String labelText, int index) {
GridBagConstraints gbc = new GridBagConstraints(0, index, 1, 1, 0.2, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0,
0, 5), 0, 0);
comboPanel.add(new JLabel(labelText, SwingConstants.RIGHT), gbc);

gbc = new GridBagConstraints(1, index, 1, 1, 1.0, 1.0,
GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(
0, 0, 0, 0), 0, 0);
JComboBox combo = new JComboBox(COMBO_ITEMS);
comboMap.put(labelText, combo);
comboPanel.add(combo, gbc);

}

public String getComboChoice(String key) {
JComboBox combo = comboMap.get(key);
if (combo != null) {
return combo.getSelectedItem().toString();
} else {
return "";
}
}

public String getTextAreaText() {
return textarea.getText();
}

public int showDialog() {
return JOptionPane.showOptionDialog(null, this, "Sirena",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
new String[] { "Aceptar", "Cancelar" }, "Aceptar");
}

private static void createAndShowGui() {
ComplexDialog dlg = new ComplexDialog();
int response = dlg.showDialog();
if (response == 0) {
System.out.println("JTextArea's text is:");
System.err.println(dlg.getTextAreaText());

System.out.println("Combo box selections are: ");
for (String comboLabel : COMBO_LABELS) {

System.out.printf("%20s: %s%n", comboLabel, dlg.getComboChoice(comboLabel));
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 如何使JFrame模态像JOptionPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8747424/

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