JFrame questions

2019-08-28 05:17发布

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

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.

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.

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.

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.

标签: java jframe oop
1条回答
看我几分像从前
2楼-- · 2019-08-28 05:38

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.

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

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.

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();
   }
}
查看更多
登录 后发表回答