如何制作像 JOptionPane 一样的 JFrame 模式?

发布于 2024-12-25 15:15:21 字数 2670 浏览 2 评论 0原文

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

如何使 JFrame 表现得像 JOptionPanes? (教程没有帮助)即

[显示内容>>返回一个值]。另外我需要它是

[显示框架>>>询问信息>>修改作为参数传递给构造函数的对象>> return Something]

我已经让“Ok”按钮得到了我想要的答案(在控制台中显示它),但我不知道在哪里放置

我想要的函数的 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 中。如果您想查看代码点击

为什么我不使用 JDialog,因为它需要一个我没有的父框架。我需要从另一个非 java 应用程序显示它,所以它必须已经是顶部框架。



You may not want to read this but if you are interested in reading what i'm i doing, please go ahead:

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

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

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

这是最后一项(参数形式)的屏幕截图,在示例中,每个项的类型为 Month,答案将填充其值。

在此处输入图像描述

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

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

MidPanel,从 JScrollPane 扩展(这个仅为列表中最后两个对话框创建) )并基于表单作为参数传递给用户的对象创建字段,需要在 java 中检查此输入,因为它可以是日期、文本字段或基于选择的组合框语句(图中是根据数据库查询得到一年中的12个月)。

BotPanel 具有 java 向表单提供的答案的控件,例如参数列表或“是/否”对话框的答案。该面板随各种对话框而变化。例如,对于是/否对话框,它有“是”和“否”按钮,将使 java 返回 true 或 false,但对于参数表单对话框,如果发生,它将返回错误消息以及包含该信息的对象用户选择的。

First, I will make the question and then I will explain the application.

How can I make JFrames to behave like JOptionPanes? (tutorial didn't help) i.e.

[Show content >> return a value]. Aditionally I need it to be

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

I already made the "Ok" button get the answer i want (displaying it in the console), but i cant figure out where to put the return statement

i want the function to be something like

public static String getAnswer(Args, Answer)

Args, may contain name of the field, type of data, maximum lenght, and

Answer, is the object to be modified, after the user gives the information and clicks "ok"

There is always and argument and an answer for it. e.g. (look at the screenshot, to see how the "most complex" type of message I need to display, it is incomplete though, because it needs to display different kind of components depending on the data type).

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

What i did so far, is to display a JFrame (which is the dialog) with the desired content and make a button to show me the answer in console.

How do I build the JFrame? I have 4 classes, three of the classes are described below the screenshot and the last one, build a JPanel that contains the three other panels and add it to the JFrame. If you want to see the code click.

Why I dont use JDialog, because it needs a parent frame which I dont have. I need to display this from another not java application, so this, must already be the top frame.



You may not want to read this but if you are interested in reading what i'm i doing, please go ahead:

Im aware, that by using JOptionPane.showMessageDialog() and so on, my problem may be solved, but I cant figure out hot to make it fit my needs. The content showed on a JOptionPane seems limited to me and I don't know how to keep control nor reference to the childrens I put into it, everything changes its behaviour .

I have to make five different dialogs that would return values to another application (painful oracle forms 6i). The types of dialogs are:

  • Print dialog, which shows a list of printers or the option "Export to file"
  • Message dialog, shows html content (like an e-mail preview) and has an "Ok" button.
  • Yes/No dialog, asks for a confirmation from the user, works like message dialog.
  • Parameter form, asks the user to give some information, the information it asks, is based on a select or an array of strings.
  • List of values, based on a select statement, shows the result just like plsql developer would do.

Here is a screenshot of the last item (Parameter form), in the example, evey item is type Month, and the answer would be filled with its value.

enter image description here

As you can see, the frame is divided by 3 blocks (which I made as 3 classes TopPanel, MidPanel and BotPanel): Message dialog, User inputs and command buttons respectively.

TopPanel, extends from JScrollPane and creates a JEditorPane, cuz it may show html content

MidPanel, extends from JScrollPane (this one, is created only for the last two dialogs in the list) and creates fields based on an object that forms pass as parameters to be asked to the user, the input to this, need to be checked in java as it can be a date, a textField or a combo box based on a select statement (in the picture, there are the 12 months of the year based on a query to the database).

BotPanel has the controls for the answer that java would give to forms, say, the list of parameters or the answer to a Yes/No dialog. This panels, changes with every kind of dialog. e.g. For a yes/no dialog, it has "Yes" and "No" buttons that will make java, return true or false, but for a parameter form dialog, it would return an error message if ocurred and the Object that contains the information that the user choosed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2025-01-01 15:15:21

同样,您可以将任何复杂的 gui 放入 JOptionPane 中。 JOptionPane show 方法的第二个参数采用一个对象,该对象可以是任何 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();
         }
      });
   }
}

Again, you can put any complex gui into a JOptionPane. The JOptionPane show method's second parameter takes an Object which can be any Swing component. For example:

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();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文