JFrame问题

时间:2011-04-09 18:22:36

标签: java jframe oop

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

我在java中创建了一些对象类,我想为这些对象制作GUI。所以我创建了2个JFrame。一个JFrame操纵这些不同的类,另一个JFrame操纵第一个类。

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

现在,AnimalGUI只是包含菜单和textareas的JFrame。在其中一个菜单中有一个菜单项“Create animal”。现在我想要的方式是当我点击“创建动物”时,应该出现TypesGUI以创建我想要创建的内容。在TypesGUI中,有一个按钮。因此,如果我在创建我想要创建的内容后单击该按钮,则窗口应该消失,我创建的内容应该出现在AnimalGUI的textareas中。换句话说,我希望能够获得我创造的动物的不同特征。

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

1 个答案:

答案 0 :(得分:1)

首先,第二个窗口,即允许用户选择动物类型的窗口,不是作为独立的应用程序窗口,而是作为与主窗口相关并依赖于主窗口的对话框。例如,您永远不会自己显示第二个窗口,而是只显示它以获取发往主窗口/类的信息。因此它应该是一个对话框类型的窗口,可以是JOptionPane,也可以是模态JDialog,而不是JFrame。这有几个其他优点,包括保证它在z顺序上保持在主窗口的前面,并且如果它被丢弃它将不会关闭整个应用程序。

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

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

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

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