Rog*_*ger 2 java swing modal-dialog
首先,我将提出问题然后我将解释该应用程序.
我怎么能让JFrames表现得像JOptionPanes?(教程没有帮助)即
[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)
Run Code Online (Sandbox Code Playgroud)
Args,可能包含字段名称,数据类型,最大长度和
Answer,是用户提供信息并点击"确定"后要修改的对象
它始终存在争论和答案.例如(查看屏幕截图,看看我需要显示的"最复杂"类型的消息,但它不完整,因为它需要根据数据类型显示不同类型的组件).
| Arg | Answer |
--------------------------------------
| Type | fldName | |
--------------------------------------
| int | Age: | 22 |
| String | Name: | Roger |
| Date | Birth: | 31/10/1989 |
Run Code Online (Sandbox Code Playgroud)
我到目前为止所做的是显示一个带有所需内容的JFrame(对话框),并在控制台中显示一个按钮以显示答案.
如何构建JFrame?我有4个类,其中三个类在屏幕截图和最后一个类中描述,构建一个包含其他三个面板的JPanel并将其添加到JFrame.如果要查看代码,请单击.
为什么我不使用JDialog,因为它需要一个我没有的父框架.我需要从另一个不是java应用程序显示这个,所以这必须已经是顶部框架.
我知道,通过使用JOptionPane.showMessageDialog()等等,我的问题可能会得到解决,但我无法弄清楚它是否适合我的需求.在JOptionPane上显示的内容似乎仅限于我,我不知道如何控制或引用我投入其中的孩子,一切都改变了它的行为.
我必须制作五个不同的对话框,将值返回给另一个应用程序(痛苦的oracle形式6i).对话框的类型是:
这是最后一项(Parameter form)的屏幕截图,在示例中,evey项目是类型Month,答案将用其值填充.

如您所见,框架被3个块(我将其作为3个类TopPanel,MidPanel并且BotPanel)分开:消息对话框,用户输入和命令按钮.
TopPanel,从JScrollPane扩展并创建一个JEditorPane,因为它可能显示html内容
MidPanel,从JScrollPane扩展(这个,仅为列表中的最后两个对话框创建),并根据一个对象创建字段,该对象形成为要向用户询问的参数,输入到此,需要在java中检查因为它可以是基于select语句的日期,textField或组合框(在图中,基于对数据库的查询,有一年中的12个月).
BotPanel具有java将为表单提供的答案的控件,例如,参数列表或是/否对话框的答案.这个面板随着各种对话而变化.例如,对于是/否对话框,它具有"是"和"否"按钮,它们将生成java,返回true或false,但是对于参数表单对话框,如果出现,则会返回错误消息,并且包含该信息的Object用户选择的.
同样,您可以将任何复杂的gui放入JOptionPane.JOptionPane show方法的第二个参数采用一个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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4395 次 |
| 最近记录: |