JFrame问题 [英] JFrame questions

查看:69
本文介绍了JFrame问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JFrame有一些疑问.这是场景:

I have some questions regarding JFrame. Here's the scenario:

我已经用Java制作了一些对象类,并且想为这些对象制作GUI.所以我创建了2个JFrame.一个JFrame处理这些不同的类,另一个JFrame处理第一个类.

I have made some object classes in java and I want to make the GUI for these objects. So i created 2 JFrames. One JFrame manipulating these different classes and the other JFrame manipulating the first one.

我将第一个JFrame称为"TypesGUI".它操纵动物(狮子,老虎等)的不同实例. 我将第二个JFrame称为AnimalGUI.

The first JFrame i'll call it "TypesGUI". It manipulates differents instances of animals (Lion, tiger etc). The second JFrame i'll call it AnimalGUI.

现在AnimalGUI只是包含菜单和文本区域的JFrame.在菜单之一中,有一个菜单项创建动物".现在,我需要一种方式,当我单击创建动物"时,TypesGUI应该出现,以便创建我要创建的内容.在TypesGUI中,有一个按钮.因此,如果我在创建要创建的对象后单击该按钮,则该窗口应消失,而我创建的对象应出现在AnimalGUI的文本区域中.换句话说,我希望能够获得所创造动物的不同特征.

Now AnimalGUI is just JFrame that contains menus and textareas. In one of the menus there is a menu item "Create animal". Now i want it in a way that when I click on "Create animal", TypesGUI should appear so as to create what i want to create. And in TypesGUI, there is a button. SO if i should click on that button after creating what i want to create, the window should disappear and what i created the should appear in the textareas of AnimalGUI. In other words, I want to be able to get the different characteristics of the animals I created.

到目前为止,我已经向菜单项和按钮添加了一个动作侦听器,并分别使用setVisible方法和true或false值.我觉得使用setVisible(true或false)并不能真正释放TypesGUI所使用的内存. 使用setVisible是一个好方法吗?另外,如何获得在TypesGUI中创建的不同动物的特征,并在AnimalGUI中显示它们?谢谢.

So far I added an action listener to the menu item and the button and used setVisible method with values true or false for each of them respectively. I feel like using setVisible(true or false) does not really free the memory used by the TypesGUI. Is using setVisible a good approach? Also how can I be able to get the characteristics of the different animals created in TypesGUI and show them in AnimalGUI? Thanks.

推荐答案

首先,第二个窗口(该窗口允许用户选择一种动物)不是充当独立应用程序窗口,而是充当对话框与主窗口相关并依赖于主窗口.例如,您永远不会单独显示第二个窗口,而是只显示它以获取发往主窗口/类的信息.因此,它应该是窗口的对话框类型,是JOptionPane还是模式JDialog,而不是JFrame.这还有其他几个优点,包括保证它保持在主窗口z顺序的前面,并且确保它永远不会在关闭后关闭整个应用程序.

First off, the second window, the one that allows the user to select a type of animal is not acting as an indepent application window but more of as a dialog that is related to and dependent on the main window. For example, you'd never display this second window on its own, but rather you'd only display it to get information that is destined for the main window/class. Thus it should be a dialog type of window, either a JOptionPane, or a modal JDialog, and not a JFrame. This has several other advantages including guaranteeing that it remains in front of the main window z-order wise, and that it won't ever shut down the entire application if it is disposed of.

关于setVisible(true/false),这应该可以正常工作.

Regarding setVisible(true/false) this should work just fine.

关于从第二个窗口/对话框中提取动物类型信息:如果您将第二个窗口称为模式JDialog或JOptionPane(实际上是专门的模式JDialog),您将确切地知道用户何时完成使用第二个窗口,因为主窗口的代码将在您在第二个窗口上调用setVisible(true)或称为JOptionPane.showXXXX()的位置之后立即启动.此时,您可以通过给第二个窗口/类一个公共方法,例如getAnimalType()来返回第二个信息,从而使主窗口/类从第二个窗口请求动物类型.

Regarding extracting the Animal type information from the second window/dialog: if you call the second window as a modal JDialog or JOptionPane (which is really a specialized modal JDialog), you will know exactly when the user has completed his work with the second window, since your main window's code will start up right after the spot where you called setVisible(true) on the second window or called JOptionPane.showXXXX(). At this point you could have your main window/class request the animal type from the second window by giving the second window/class a public method, say getAnimalType() which returns this information.

例如,这是一个两个窗口的程序,其中显示了一个使用GridBagLayout并在JOptionPane中接受两个JTextField文本的JPanel:

For example, here's a two-window program that show's a JPanel that uses GridBagLayout and accepts two JTextField texts in a JOptionPane:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TwoWindowEg {
   private static void createAndShowUI() {
      JFrame frame = new JFrame("Two Window Eg");
      frame.getContentPane().add(new MainPanel());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

class MainPanel extends JPanel {
   private JTextField textField = new JTextField(20);
   private DialogPanel dialogPanel = null;

   public MainPanel() {
      textField.setEditable(false);
      textField.setFocusable(false);
      textField.setBackground(Color.white);

      JButton openDialogBtn = new JButton("Open Dialog");
      openDialogBtn.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            if (dialogPanel == null) {
               dialogPanel = new DialogPanel();
            }
            int result = JOptionPane.showConfirmDialog(MainPanel.this, dialogPanel,
                     "Enter First and Last Name", JOptionPane.OK_CANCEL_OPTION,
                     JOptionPane.PLAIN_MESSAGE);
            if (result == JOptionPane.OK_OPTION) {
               String text = dialogPanel.getText();
               textField.setText(text);
            }

         }
      });

      add(textField);
      add(openDialogBtn);

      setPreferredSize(new Dimension(400, 300));
      setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
   }
}

class DialogPanel extends JPanel {
   private static final Insets INSETS = new Insets(0, 10, 0, 10);
   private JTextField firstNameField = new JTextField(10);
   private JTextField lastNameField = new JTextField(10);

   public DialogPanel() {
      setLayout(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
               GridBagConstraints.LINE_START, GridBagConstraints.BOTH, 
               INSETS, 0, 0);
      add(new JLabel("First Name"), gbc);
      gbc = new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
               GridBagConstraints.LINE_END, GridBagConstraints.BOTH, 
               INSETS, 0, 0);
      add(new JLabel("Last Name"), gbc);
      gbc = new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
               GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, 
               INSETS, 0, 0);
      add(firstNameField, gbc);

      gbc = new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
               GridBagConstraints.LINE_END, GridBagConstraints.HORIZONTAL, 
               INSETS, 0, 0);
      add(lastNameField, gbc);
   }

   public String getText() {
      return firstNameField.getText() + " " + lastNameField.getText();
   }
}

这篇关于JFrame问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆