Java 如何";隐藏“;在主界面外使用JButton的JFrame

Java 如何";隐藏“;在主界面外使用JButton的JFrame,java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,所以我几乎完成了这个项目,最后一件事就是“隐藏”按钮。我的老师从来没有教过我该做什么,但因为这让我很恼火,而且我找不到一个有效的答案,我想我应该问问你们这些好人 我试过: setExtendedState(JFrame.ICONIFIED)//导致编译器错误,找不到我的变量名 setState(JFrame.ICONIFIED)//同样的问题,“找不到符号”,或者更确切地说是找到我的变量 setVisible(false)//这不起作用,因为它隐藏了我的整个帧,如果不关闭程序,我就无法取回它 我

所以我几乎完成了这个项目,最后一件事就是“隐藏”按钮。我的老师从来没有教过我该做什么,但因为这让我很恼火,而且我找不到一个有效的答案,我想我应该问问你们这些好人

我试过:

  • setExtendedState(JFrame.ICONIFIED)
    //导致编译器错误,找不到我的变量名
  • setState(JFrame.ICONIFIED)
    //同样的问题,“找不到符号”,或者更确切地说是找到我的变量
  • setVisible(false)
    //这不起作用,因为它隐藏了我的整个帧,如果不关闭程序,我就无法取回它
  • 我使用
    Container c=getContentPane()
    创建窗格,然后在主窗格中使用:

    ClassName variableName = new ClassName() to create the parameters.
    
    这就是我被教导的方式,我现在必须使用这种方式(因为这是我的老师想要的),但我注意到还有其他方法可以实现同样的目标

    任何特定于我的程序的输入都会很棒!谢谢

    我的程序如下(我发布了全部内容,因此没有遗漏任何内容):

    import java.awt.*;
    导入java.awt.event.*;
    导入javax.swing.*;
    导入java.util.array;
    公共类Project9扩展了JFrame
    {
    字体f1=新字体(“衬线”,Font.BOLD,30);
    Font f2=新字体(“衬线”,Font.PLAIN,18);
    private BOOKItem[]bookArray=新建BOOKItem[10];
    私人JLabel headerLbl;
    私有JLabel消息slbl;
    私有JTextField idLabelFld;
    私有JTextField-idFld;
    私有JTextField pricelbld;
    私有JTextField priceFld;
    私人JTextField numInStockLabelFld;
    私有JTextField numInStockFld;
    私有JTextField代码标签elfld;
    私有JTextField代码;
    私有JTextField消息sfld;
    私有JButton insertBtn;
    私有JButton deleteBtn;
    专用按钮显示;
    私有JButton displayOneBtn;
    私人JButton hideBtn;
    私人JButton clearBtn;
    私有字符串输入=”;
    私有字符串displayOneStr=“”;
    私有int-put=0;
    私人双倍价格输入=0.0;
    私有整数numInStockInput=0;
    私有int codeInput=0;
    私有整数指数=0;
    私有int numItems=0;
    私有int响应代码=0;
    私有容器c=getContentPane();
    //Main方法,设置arrayFrame参数
    公共静态void main(字符串[]args)
    {
    Project9 arrayFrame=新项目9();
    阵列帧设置大小(555450);
    arrayFrame.setVisible(true);
    arrayFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }     
    //建造师
    公共项目9()
    {
    //创建数组
    for(int i=0;iimport java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Arrays;
    
    public class Project9 extends JFrame
    {
      Font f1 = new Font("Serif", Font.BOLD, 30);
      Font f2 = new Font("Serif", Font.PLAIN, 18);
    
      private BOOKItem[] bookArray = new BOOKItem[10];
    
      private JLabel headerLbl;
      private JLabel messagesLbl;
    
      private JTextField idLabelFld;
      private JTextField idFld;
      private JTextField priceLabelFld;
      private JTextField priceFld;
      private JTextField numInStockLabelFld;
      private JTextField numInStockFld;
      private JTextField codeLabelFld;
      private JTextField codeFld;
      private JTextField messagesFld;
    
      private JButton insertBtn;
      private JButton deleteBtn;
      private JButton displayBtn;
      private JButton displayOneBtn;
      private JButton hideBtn;
      private JButton clearBtn;
    
      private String input = "";
      private String displayOneStr = "";
    
      private int idInput = 0;
      private double priceInput = 0.0;
      private int numInStockInput = 0;
      private int codeInput = 0;
    
      private int index = 0;
      private int numItems = 0;
      private int responseCode = 0;
    
      private Container c = getContentPane();
    
      //Main Method, sets arrayFrame params
      public static void main(String[] args)
      {
            Project9 arrayFrame = new Project9();
            arrayFrame.setSize(555,450);
            arrayFrame.setVisible(true);
            arrayFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }     
    
    
      //Constructor
      public Project9()
      {
            //Creates the array 
            for (int i = 0; i < bookArray.length; i++)
            {
                  bookArray[i] = new BOOKItem();
                  System.out.println(bookArray[i]);
            } 
    
            setTitle("Project 9");
            c.setLayout(new FlowLayout());
    
            headerLbl = new JLabel("Data Entry: BestBargainBook Store");
            headerLbl.setFont(f1);
            c.add(headerLbl);
    
            idLabelFld = new JTextField("Enter ID:", 15);
            idLabelFld.setEditable(false);
            c.add(idLabelFld);
    
            idFld = new JTextField(25);
            c.add(idFld);
    
            priceLabelFld = new JTextField("Enter Price:", 15);
            priceLabelFld.setEditable(false);
            c.add(priceLabelFld);
    
            priceFld = new JTextField(25);
            c.add(priceFld);
    
            numInStockLabelFld = new JTextField("Enter Number In Stock:", 15);
            numInStockLabelFld.setEditable(false);
            c.add(numInStockLabelFld);
    
            numInStockFld = new JTextField(25);
            c.add(numInStockFld);
    
            codeLabelFld = new JTextField("Enter Code: 1,2,3 or 4:", 15);
            codeLabelFld.setEditable(false);
            c.add(codeLabelFld);
    
            codeFld = new JTextField(25);
            c.add(codeFld);
    
            insertBtn = new JButton("Insert");
            c.add(insertBtn);
    
            deleteBtn = new JButton("Delete");
            c.add(deleteBtn);
    
            displayBtn = new JButton("Display");
            c.add(displayBtn);
    
            displayOneBtn = new JButton("DisplayOne");
            c.add(displayOneBtn);
    
            hideBtn = new JButton("Hide");
            c.add(hideBtn);
    
            clearBtn = new JButton("Clear");
            c.add(clearBtn);
    
            messagesLbl = new JLabel("Messages:");
            messagesLbl.setFont(f2);
            c.add(messagesLbl);
    
            messagesFld = new JTextField(30);
            c.add(messagesFld);
    
            //Event Listeners
            insertBtn.addActionListener(new EventHandler());
            deleteBtn.addActionListener(new EventHandler());
            displayBtn.addActionListener(new EventHandler());
            displayOneBtn.addActionListener(new EventHandler());
            hideBtn.addActionListener(new EventHandler());
            clearBtn.addActionListener(new EventHandler());
    
      }//end constructor      
    
    private class EventHandler implements ActionListener
    {
      public void actionPerformed(ActionEvent ev)
      {
    
            if (ev.getSource() == insertBtn)
            {
                  input = idFld.getText();
                  idInput = Integer.parseInt(input);
    
                  input = priceFld.getText();
                  priceInput = Double.parseDouble(input);
    
                  input = numInStockFld.getText();
                  numInStockInput = Integer.parseInt(input);
    
                  input = codeFld.getText();
                  codeInput = Integer.parseInt(input);
    
                  insert(idInput, priceInput, numInStockInput, 
                         codeInput); 
    
                  if (responseCode == 0)
                  {
                        messagesFld.setText("Array is full. Cannot insert book ID: " + 
                                            idInput);
                  }
                  else if (responseCode == 1)
                  {
                        messagesFld.setText("Succesful insertion of " + idInput);
                  }
                  else if (responseCode == -1)
                  {
                        messagesFld.setText("Duplicate ID: " + idInput);
                  }                          
    
    
    
            }
    
            else if (ev.getSource() == deleteBtn)
            {
                  input = idFld.getText();
                  idInput = Integer.parseInt(input);
    
                  delete(idInput);
    
                  if (responseCode == 1)
                  {      
                        messagesFld.setText("Successful delete of book ID: " +
                                            idInput);
                  }
                  else if (responseCode == -1)
                  {
                        messagesFld.setText("ID: " + idInput + " not found.");
                  }                                
    
            }
    
            else if (ev.getSource() == displayBtn)
            {
                  for (index = 0; index < bookArray.length; index++)
                  {
                        bookArray[index].display();
                  }      
            }
    
            else if (ev.getSource() == displayOneBtn)
            {
                  input = idFld.getText();
                  idInput = Integer.parseInt(input);
    
                  for (index = 0; index < bookArray.length; index++)
                  {
                        if (bookArray[index].getID() == idInput)
                        {
                              bookArray[index].getID();
                              bookArray[index].getPrice();
                              bookArray[index].getNumberInStock();
                              bookArray[index].getCode();
    
                              messagesFld.setText("id: " + bookArray[index].getID() +
                                                  "  Price: " + bookArray[index].getPrice() +
                                                  "  Number In Stock: " + bookArray[index].getNumberInStock() +
                                                  "  Code: " + bookArray[index].getCode()); 
                        }
                  }      
    
            }      
    
            else if (ev.getSource() == hideBtn)
            {
    
            }
    
            else if (ev.getSource() == clearBtn)
            {
                  idFld.setText("");
                  priceFld.setText("");
                  numInStockFld.setText("");
                  codeFld.setText("");
                  messagesFld.setText("");
                  repaint();
            }
    
      }//End actionPerformed
    
    }//End handler
    
      //insert method, called when insert button is pressed
      public int insert(int iD, double prc, int numInStock, int code)
      {
            if (numItems == 10)
            {
                  System.out.println("\nThe Array is full, please delete an entry");
    
                  responseCode = 0;
                  return responseCode;
            }      
    
            for (index = 0; index < bookArray.length; index++)
            {
                  if (bookArray[index].getID() == iD)
                  {
                        System.out.println("\nThat ID already exists");
    
                        responseCode = -1;
                        return responseCode;
                  }
                  else if (bookArray[index].getID() == 0)
                  {
                        bookArray[index] = new BOOKItem(iD, prc, numInStock, code);
                        numItems++; 
    
                        System.out.println("\n" + idInput + "\n" + priceInput + "\n" + 
                                           numInStockInput + "\n" + codeInput + "\n" + index);
                        System.out.println("\nID: " + bookArray[index].getID());
                        System.out.println("Price: " + bookArray[index].getPrice());
                        System.out.println("NIS: " + bookArray[index].getNumberInStock());
                        System.out.println("Code: " + bookArray[index].getCode());
                        System.out.println("Items in Array: " + numItems); 
    
                        responseCode = 1;
                        return responseCode;
                  }               
    
            }
    
            return responseCode;
    
      }//end insert method 
    
      //Delete method, called when delete button is pressed
      public int delete(int id)
      {
            for (index = 0; index < bookArray.length; index++)
            {
                  if (bookArray[index].getID() == id)
                  {
                        bookArray[index].setID(0);
                        bookArray[index].setPrice(0);
                        bookArray[index].setStock(0);
                        bookArray[index].setCode(0);
                        numItems--;
                        System.out.println("\nSuccessful deletion");
    
                        responseCode = 1;
                        return responseCode;
                  }               
    
            }
    
            responseCode = -1;
            return responseCode;
    
      }//end delete method
    
    
    }//end app                                                                 
    
    button.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            frame.setVisible(false);
        }
    });
    
    Project9.this.setState(JFrame.ICONIFIED);
    
    JButton button = (JButton)e.getSource();
    Window window = SwingUtilities.windowForComponent( button );
    JFrame frame = (JFrame)window;
    frame.setState(JFrame.ICONIFIED);