2013-10-23 61 views
0

的JFrame的。当我打电话内部类JFrame,它被称为,它会显示,但在外部类JFrame不隐藏。我在这里提供我的代码。 具体而言,我试图填写一个表格,然后我想要预览我写的表单。调用内部类的一个JFrame和隐藏外部类

package com.vote.rmc; 

public class LRegister extends JFrame implements ActionListener { 

    final public JFrame rframe = new JFrame(); 
    JLabel 

    public LRegister() { 

     rframe.setSize(800, 600); 
     rframe.setLocationRelativeTo(null); 
     cp = getContentPane(); 
     cp.setLayout(null); 
     setSize(550, 300); 
     rframe.setTitle("User Registration Form"); 
     cp.setBackground(new Color(140, 180, 180)); 

     JTextArea plainTextArea = new JTextArea(); 
     JScrollPane scrollPanePlain = new JScrollPane(plainTextArea); 
     cp.add(scrollPanePlain); 
     scrollPanePlain.setBounds(150, 250, 150, 100); 
     scrollPanePlain.setVisible(true); 

     CFirstName = new JLabel("First Name"); 
     CLastName = new JLabel("Last Name"); 
     DOB = new JLabel("Date Of Birth"); 
     FatherName = new JLabel("Father's Name"); 
     PhoneNumber = new JLabel("Phone Number"); 
     address = new JLabel("Address"); 
     departmentName = new JLabel("Department Name"); 
     Year = new JLabel("Year"); 
     EmailID = new JLabel("Email ID"); 
     jpic = new JLabel("Upload your Picture"); 

     l = new JLabel("Candidates Information"); 
     jlmsg = new JLabel("", JLabel.CENTER); 

     Cfname = new JTextField(10); 
     Clname = new JTextField(10); 
     birth = new JTextField(10); 
     Fname = new JTextField(10); 
     PhNum = new JTextField(10); 
     addrss = new JTextField(10); 
     deptName = new JTextField(10); 
     Yr = new JTextField(10); 
     mailID = new JTextField(10); 

     jbsubmit = new JButton("PREVIEW"); 

     jbrowse = new JButton("BROWSE PICTURE"); 
     jbexit = new JButton("CLOSE"); 

     l.setBounds(270, 10, 250, 35); 
     CFirstName.setBounds(10, 50, 100, 35); 
     Cfname.setBounds(150, 55, 150, 25); 

     CLastName.setBounds(400, 50, 500, 35); 
     Clname.setBounds(550, 50, 150, 25); 

     DOB.setBounds(10, 100, 100, 35); 
     birth.setBounds(150, 105, 150, 25); 

     FatherName.setBounds(10, 150, 100, 35); 
     Fname.setBounds(150, 155, 150, 25); 

     PhoneNumber.setBounds(400, 100, 500, 35); 
     PhNum.setBounds(550, 100, 150, 25); 

     departmentName.setBounds(400, 150, 500, 35); 
     deptName.setBounds(550, 150, 150, 25); 

     EmailID.setBounds(10, 190, 150, 50); 
     mailID.setBounds(150, 200, 150, 25); 

     address.setBounds(10, 250, 100, 35); 
     addrss.setBounds(150, 250, 150, 100); 

     Year.setBounds(400, 200, 500, 35); 
     Yr.setBounds(550, 200, 150, 25); 

     jbsubmit.setBounds(150, 380, 92, 25); 
     //jbnew.setBounds(300,380,92,25); 
     jbexit.setBounds(450, 380, 92, 25); 

     jpic.setBounds(400, 250, 500, 35); 
     jbrowse.setBounds(550, 253, 150, 25); 

     jbsubmit.addActionListener(this); 
     //jbnew.addActionListener(this); 
     jbrowse.addActionListener(this); 
     jbexit.addActionListener(this); 

     cp.add(jpic); 
     cp.add(Cfname); 
     cp.add(CFirstName); 
     cp.add(CLastName); 
     cp.add(DOB); 
     cp.add(EmailID); 
     cp.add(FatherName); 
     cp.add(Clname); 
     cp.add(Fname); 
     cp.add(departmentName); 
     cp.add(deptName); 
     cp.add(Year); 
     cp.add(birth); 
     cp.add(Fname); 
     cp.add(PhNum); 
     cp.add(addrss); 
     cp.add(Yr); 
     cp.add(mailID); 

     cp.add(DOB); 
     cp.add(PhoneNumber); 
     cp.add(address); 
     cp.add(jbsubmit); 
     //cp.add(jbnew); 
     cp.add(jbexit); 
     cp.add(jbrowse); 
     cp.add(l); 

     rframe.add(cp); 
     rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     rframe.setVisible(true); 

    } 

    public void actionPerformed(ActionEvent ae) { 
     String action = ae.getActionCommand(); 

     if (action == "PREVIEW") { 
      new B(); 

      rframe.setVisible(false); 

     } 

     if (action == "CLOSE") { 
      rframe.setVisible(false); 
     } 

     if (action == "BROWSE PICTURE") { 
      if (ae.getSource() == jbrowse) { 
       analizepath(); 
       rframe.setVisible(true); 
      } 
     } 

    } 

    private File getFile() { 
     JFileChooser filechooser = new JFileChooser(); 
     filechooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 
     @SuppressWarnings("unused") 
     int result = filechooser.showOpenDialog(this); 
     File filename = filechooser.getSelectedFile(); 
     if ((filename == null) || (filename.getName().equals(""))) { 
      JOptionPane.showMessageDialog(this, "invalid filename", "invalidfilename", JOptionPane.ERROR_MESSAGE); 
     } 
     return filename; 
    } 

    public void analizepath() { 
     File name = getFile(); 

     path = name.getAbsolutePath(); 
     picture = new ImageIcon(path); 
     pic = new File(path); 
     try { 
      inputStream = new FileInputStream(pic); 
     } catch (Exception d) { 
      System.out.println(d); 
     } 
     System.out.print(path); 
    } 

    class B { 

     JFrame frame = new JFrame(); 

     public B() { 

      B.this.frame.setSize(800, 600); 
      B.this.frame.setLocationRelativeTo(null); 
      cp = getContentPane(); 
      cp.setLayout(null); 
      setSize(550, 300); 
      B.this.frame.setTitle("Preview Form"); 
      cp.setBackground(new Color(140, 180, 180)); 

      JTextField c = LRegister.this.Cfname; 
      String cfn = c.getText(); 
      System.out.println(cfn); 

      CFirstName = new JLabel(cfn); 

      CLastName = new JLabel("Last Name"); 

      DOB = new JLabel("Date Of Birth"); 

      FatherName = new JLabel("Father's Name"); 

      PhoneNumber = new JLabel("Phone Number"); 

      address = new JLabel("Address"); 

      departmentName = new JLabel("Department Name"); 

      Year = new JLabel("Year"); 

      EmailID = new JLabel("Email ID"); 

      jpic = new JLabel("Your Picture"); 

      l = new JLabel("Preview Your Form Before Submitting"); 

      jlmsg = new JLabel("", JLabel.CENTER); 

      l.setBounds(270, 10, 250, 35); 
      CFirstName.setBounds(10, 50, 100, 35); 
      CLastName.setBounds(400, 50, 500, 35); 
      DOB.setBounds(10, 100, 100, 35); 
      FatherName.setBounds(10, 150, 100, 35); 
      PhoneNumber.setBounds(400, 100, 500, 35); 
      departmentName.setBounds(400, 150, 500, 35); 
      EmailID.setBounds(10, 190, 150, 50); 
      address.setBounds(10, 250, 100, 35); 
      Year.setBounds(400, 200, 500, 35); 
      jpic.setBounds(400, 250, 500, 35); 

      cp.add(jpic); 
      cp.add(CFirstName); 
      cp.add(CLastName); 
      cp.add(DOB); 
      cp.add(EmailID); 
      cp.add(FatherName); 
      cp.add(departmentName); 
      cp.add(Year); 
      cp.add(DOB); 
      cp.add(PhoneNumber); 
      cp.add(address); 
      cp.add(l); 

      frame.repaint(); 
      B.this.frame.add(cp); 
      B.this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      B.this.frame.setVisible(true); 

     } 

    } 
+0

'action ==“PREVIEW”'不是如何在Java中完成字符串比较 – MadProgrammer

+0

请参见[使用多个JFrames,好/坏实践?](http://stackoverflow.com/a/9554657/418556 ) –

回答

0
public void actionPerformed(ActionEvent ae) { 
    String action = ae.getActionCommand(); 

    if (action == "PREVIEW") { 
     new B(); 

     rframe.setVisible(false); 

    } 

    if (action == "CLOSE") { 
     rframe.setVisible(false); 
    } 

    if (action == "BROWSE PICTURE") { 
     if (ae.getSource() == jbrowse) { 
      analizepath(); 
      rframe.setVisible(true); 
     } 
    } 
} 

在旅游actionPerformed(),有三种不同的if语句只触发每一个帧动作。这是你的意图吗?如果你想让外框看不见,你不应该加outerFrame.setVisible(false);

添加所有action == "PREVIEW"应该是action.equals("PREVIEW")

+0

为什么不通知OP有关他/她的字符串比较问题。 – Vallentin

+0

谢谢你的追捕。 –

+0

'actionCommand'可以是'null',一个更好的比较将是'PREVIEW'.equals(action)',因为它可以防范'NullPointerException' – MadProgrammer

1

你正在创建的JFrame具有彼此没有连接的多个不同的实例...

public class LRegister extends JFrame implements ActionListener { 

    final public JFrame rframe = new JFrame(); 

LRegisterJFrame,其上(从我可以告诉),你建立你的用户界面。但是,你尝试改变rframe的可见性状态?

您可以考虑使用CardLayout而不是诉诸使用不同的帧,但随后将意味着你将需要开始使用Layout Managers直接

这也将是一个不错的主意,有通过How do I compare strings in Java?The Use of Multiple JFrames: Good or Bad Practice?