2014-07-15 4 views
-2

저는 Java GUI를 처음 사용합니다. 나는 스레드 프로그래밍에 좋지 않다. 다른 JFrame 메서드에서 JFrame을 만들고 표시하는 쉬운 방법이 있는지 궁금합니다. 내 코드를 알려주지. SettingsForm은 close() 메소드를 통해 JFrame 인 Test를 호출합니다. 모든 수입이 포함되었습니다. 다른 JFrame 메서드에서 JFrame을 표시하는 방법은 무엇입니까?

class SettingsForm extends JFrame{ 

    JButton submit,defaultOption;//koumpia kataxwrhshs-akurwshs 
    JLabel host, port, dbName, user, pass;//koina JLabel.Etiketes host, port, dbName, user kai password 
    JTextField hostT, portT, dbNameT, userT, passT;//koina JTextField.perioxh egrafhs twn host, port, dbName, user kai password 
    ImageIcon img; 

    public SettingsForm()//arxikh dhmiourgia tou frame 
    { 
     super("Data Base Information"); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.img = new ImageIcon("logo.png"); 
    }//constractor 

    public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout 
    { 
     JPanel panel=new JPanel(); 
     GridLayout g=new GridLayout(rows,columns,vgap,hgap); 
     panel.setLayout(g); 
     this.setContentPane(panel); 

     this.addElements(); 
     this.setIconImage(this.img.getImage()); 
     this.pack(); 

     submit.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       if (hostT.getText().length()!=0 && portT.getText().length()!=0 && dbNameT.getText().length()!=0 && userT.getText().length()!=0 && passT.getText().length()!=0) 
       { 
        try { 
        CreateSettings s = new CreateSettings("Endocrino\\settings.txt"); 
        s.write(hostT.getText(), portT.getText(), dbNameT.getText(), userT.getText(), passT.getText()); 
        close(); 
        } catch (FileNotFoundException e1) { 
         e1.printStackTrace(); 
        } catch (Exception e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
       } 
       else{ 
        JOptionPane.showMessageDialog(null,"All items must be filled","Warning",JOptionPane.ERROR_MESSAGE); 
       } 
      } 
     }); 

     defaultOption.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { hostT.setText("localhost"); 
       portT.setText("3306"); 
       dbNameT.setText("endocrino"); 
       userT.setText("endo"); 
       passT.setText("1234"); 
      } 
     }); 
    }//createForm 


    public void addButtons()//prosthetei to koumpi kataxwrhshs sto panel 
    { 
     Container c=this.getContentPane(); 
     this.submit=new JButton("OK"); 
     this.defaultOption=new JButton("Set Default"); 

     c.add(submit); 
     c.add(defaultOption); 
    }//addButtons 

    public void addHost()//prosthetei to label kai text field gia egrafh tou host apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.host=new JLabel("Host"); 
     this.hostT=new JTextField(); 
     c.add(host); 
     c.add(hostT); 
    }//addHost 

    public void addPort()//prosthetei to label kai text field gia egrafh tou port apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.port=new JLabel("Port"); 
     this.portT=new JTextField(); 
     c.add(port); 
     c.add(portT); 
    }//addPort 

    public void addDBName()//prosthetei to label kai text field gia egrafh tou Data Base Name apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.dbName=new JLabel("Data Base Name"); 
     this.dbNameT=new JTextField(); 
     c.add(dbName); 
     c.add(dbNameT); 
    }//addDBName 

    public void addUser()//prosthetei to label kai text field gia egrafh tou user apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.user=new JLabel("User"); 
     this.userT=new JTextField(); 
     c.add(user); 
     c.add(userT); 
    }//addUser 

    public void addPassword()//prosthetei to label kai text field gia egrafh tou password apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.pass=new JLabel("Password"); 
     this.passT=new JTextField(); 
     c.add(pass); 
     c.add(passT); 
    }//addPassword 

    public void showForm()//emfanizei to frame 
    { 
     this.setVisible(true); 
     this.setLocationRelativeTo(null); 
    }//showFrame 

    public void addElements(){ 
     this.addHost(); 
     this.addPort(); 
     this.addDBName(); 
     this.addUser(); 
     this.addPassword(); 
     this.addButtons(); 
    }//addElements 

    public void close(){ 
     this.dispose(); 
     Test t=new Test(); 
     t.createForm(6,1,3,3); 
     t.showForm(); 
     t.dosCommands(); 
    }//close 
}//SettingsForm 

그리고 테스트 클래스

class Test extends JFrame{ 

    JLabel firstMsg, progressMsg, enFolderMsg, dbMsg, shortcutMsg, finishedMsg;//koina JLabel.Etiketes host, port, dbName, user kai password 
    ImageIcon img; 

    public Test()//arxikh dhmiourgia tou frame 
    { 
     super("Endocrino Installation"); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.img = new ImageIcon("logo.png"); 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2); 
    }//constractor 

    public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout 
    { 
     JPanel panel=new JPanel(); 
     GridLayout g=new GridLayout(rows,columns,vgap,hgap); 
     panel.setLayout(g); 
     this.setContentPane(panel); 

     this.addElements(); 
     this.setIconImage(this.img.getImage()); 
     this.pack(); 
    }//createForm 

    public void addProgressMsg()//prosthetei to minima "Plese wait.." 
    { 
     Container c=this.getContentPane(); 
     this.progressMsg=new JLabel("Progress"); 
     c.add(this.progressMsg); 
    }//addFirstMsg 


    public void revalidateItems(){ 
     this.firstMsg.revalidate(); 
     this.progressMsg.revalidate(); 
     this.enFolderMsg.revalidate(); 
     this.dbMsg.revalidate(); 
     this.shortcutMsg.revalidate(); 
     this.finishedMsg.revalidate(); 
     revalidate(); 
    } 
    public void repaintItems(){ 
     this.firstMsg.repaint(); 
     this.progressMsg.repaint(); 
     this.enFolderMsg.repaint(); 
     this.dbMsg.repaint(); 
     this.shortcutMsg.repaint(); 
     this.finishedMsg.repaint(); 
     repaint(); 
    } 

    public void addFirstMsg()//prosthetei to minima "Plese wait.." 
    { 
     Container c=this.getContentPane(); 
     this.firstMsg=new JLabel("Please wait..."); 
     c.add(this.firstMsg); 
    }//addFirstMsg 

    public void addEnFolderMsg()//prosthetei to label gia tin dimiourgia tou fakelou C:/Endocrino 
    { 
     Container c=this.getContentPane(); 
     this.enFolderMsg=new JLabel(); 
     c.add(this.enFolderMsg); 
    }//addEnFolderMsg 

    public void addDBMsg()//prosthetei to label gia tin dimiourgia tis basis 
    { 
     Container c=this.getContentPane(); 
     this.dbMsg=new JLabel(); 
     c.add(this.dbMsg); 
    }//addDBMsg 

    public void addShortcutMsg()//prosthetei to label gia tin dimiourgia tou shortcut sto Desktop 
    { 
     Container c=this.getContentPane(); 
     this.shortcutMsg=new JLabel(); 
     c.add(this.shortcutMsg); 
    }//addShortcutMsg 

    public void addFinishedMsg()//prosthetei to label gia tin dimiourgia tou minimatos "finished" 
    { 
     Container c=this.getContentPane(); 
     this.finishedMsg=new JLabel(); 
     c.add(this.finishedMsg); 
    }//addFinishedMsg 

    public void showForm()//emfanizei to frame 
    { 
     this.setVisible(true); 
    }//showFrame 

    public void addElements(){ 
     this.addProgressMsg(); 
     this.addFirstMsg(); 
     this.addEnFolderMsg(); 
     this.addDBMsg(); 
     this.addShortcutMsg(); 
     this.addFinishedMsg(); 
    }//addElements 

    public void close(){ 
     this.dispose(); 
     System.exit(0); 
    } 

    public void dosCommands() throws InterruptedException{ 
     revalidateItems();repaintItems(); 
     this.enFolderMsg.setText("Creating Endocrino Folder..."); 
     revalidateItems();repaintItems(); 
     doSth(); 
     this.dbMsg.setText("Creating Data Base..."); 
     revalidateItems();repaintItems(); 
     doSth(); 
     this.shortcutMsg.setText("Creating shortcut..."); 
     revalidateItems();repaintItems(); 
     doSth(); 
     this.finishedMsg.setText("Finished"); 
     JOptionPane.showMessageDialog(null,"Now you can delete the Endocrino folder from your Desktop"); 
     close(); 
    }//dosCommands 
}//Test 

내 문제는 ... 테스트 프레임 창이 열립니다 있지만 JLabels가 표시되지 않는 점이다 당신의 닫기의 끝 부분에이 줄을 추가

+0

CreateSettings 클래스가없는 .. –

+0

CreateSettings 클래스는 단지 txt 파일을 만듭니다 여기에 솔루션입니다. 그것은 작동하므로 포함시킬 필요가 없습니다 ... – xarlap

답변

0

수정했습니다.

class SettingsForm extends JFrame{ 

    JButton submit,defaultOption;//koumpia kataxwrhshs-akurwshs 
    JLabel host, port, dbName, user, pass;//koina JLabel.Etiketes host, port, dbName, user kai password 
    JTextField hostT, portT, dbNameT, userT, passT;//koina JTextField.perioxh egrafhs twn host, port, dbName, user kai password 
    ImageIcon img; 

    public SettingsForm()//arxikh dhmiourgia tou frame 
    { 
     super("Data Base Information"); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.img = new ImageIcon("logo.png"); 
     this.setIconImage(this.img.getImage()); 
     this.pack(); 
     this.setVisible(true); 
     this.setLocationRelativeTo(null); 
    }//constractor 

    public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout 
    { 
     JPanel panel=new JPanel(); 
     GridLayout g=new GridLayout(rows,columns,vgap,hgap); 
     panel.setLayout(g); 
     this.setContentPane(panel); 

     this.addElements(); 

     submit.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       if (hostT.getText().length()!=0 && portT.getText().length()!=0 && dbNameT.getText().length()!=0 && userT.getText().length()!=0 && passT.getText().length()!=0) 
       { 
        try { 
        CreateSettings s = new CreateSettings("Endocrino\\settings.txt"); 
        s.write(hostT.getText(), portT.getText(), dbNameT.getText(), userT.getText(), passT.getText()); 
        close(); 
        } catch (FileNotFoundException e1) { 
         e1.printStackTrace(); 
        } catch (Exception e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
       } 
       else{ 
        JOptionPane.showMessageDialog(null,"All items must be filled","Warning",JOptionPane.ERROR_MESSAGE); 
       } 
      } 
     }); 

     defaultOption.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { hostT.setText("localhost"); 
       portT.setText("3306"); 
       dbNameT.setText("endocrino"); 
       userT.setText("endo"); 
       passT.setText("1234"); 
      } 
     }); 
    }//createForm 


    public void addButtons()//prosthetei to koumpi kataxwrhshs sto panel 
    { 
     Container c=this.getContentPane(); 
     this.submit=new JButton("OK"); 
     this.defaultOption=new JButton("Set Default"); 

     c.add(submit); 
     c.add(defaultOption); 
    }//addButtons 

    public void addHost()//prosthetei to label kai text field gia egrafh tou host apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.host=new JLabel("Host"); 
     this.hostT=new JTextField(); 
     c.add(host); 
     c.add(hostT); 
    }//addHost 

    public void addPort()//prosthetei to label kai text field gia egrafh tou port apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.port=new JLabel("Port"); 
     this.portT=new JTextField(); 
     c.add(port); 
     c.add(portT); 
    }//addPort 

    public void addDBName()//prosthetei to label kai text field gia egrafh tou Data Base Name apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.dbName=new JLabel("Data Base Name"); 
     this.dbNameT=new JTextField(); 
     c.add(dbName); 
     c.add(dbNameT); 
    }//addDBName 

    public void addUser()//prosthetei to label kai text field gia egrafh tou user apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.user=new JLabel("User"); 
     this.userT=new JTextField(); 
     c.add(user); 
     c.add(userT); 
    }//addUser 

    public void addPassword()//prosthetei to label kai text field gia egrafh tou password apo ton xrhsth 
    { 
     Container c=this.getContentPane(); 
     this.pass=new JLabel("Password"); 
     this.passT=new JTextField(); 
     c.add(pass); 
     c.add(passT); 
    }//addPassword 

    public void addElements(){ 
     this.addHost(); 
     this.addPort(); 
     this.addDBName(); 
     this.addUser(); 
     this.addPassword(); 
     this.addButtons(); 
    }//addElements 

    public void close() throws InterruptedException{ 
     this.dispose(); 
     DosCommandsWindow d=new DosCommandsWindow(); 
     d.createForm(7,1,3,3); 
    }//close 

} // SettingsForm

및 DosCommandsWindow

public class DosCommandsWindow extends JFrame{ 

/** 
* 
*/ 
JLabel progressMsg, firstMsg, enFolderMsg, dbMsg, shortcutMsg, finishedMsg; 
ImageIcon img; 
int flag; 
JProgressBar pbar; 

public DosCommandsWindow(){ 
    super("DNA ENDOCRINO"); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    this.img = new ImageIcon("logo.png"); 
    this.setIconImage(this.img.getImage()); 

    this.flag=1; 

    this.firstMsg=new JLabel("Please wait..."); 
    this.progressMsg=new JLabel("Progress"); 
    this.enFolderMsg=new JLabel("Create C:\\Endocrino folder..."); 
    this.dbMsg=new JLabel(); 
    this.shortcutMsg=new JLabel(); 
    this.finishedMsg=new JLabel(); 

    pbar = new JProgressBar(); 
    pbar.setMinimum(0); 
    pbar.setMaximum(4); 
    this.pbar.setValue(1); 

    this.setSize(300, 200); 
    this.setVisible(true); 
    this.setLocationRelativeTo(null); 
}//constractor 

public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout 
{ 
    JPanel panel=new JPanel(); 
    GridLayout g=new GridLayout(rows,columns,vgap,hgap); 
    panel.setLayout(g); 
    this.setContentPane(panel); 

    this.add(this.progressMsg); 
    this.add(this.pbar); 
    this.add(this.firstMsg); 
    this.add(this.enFolderMsg); 
    this.add(this.dbMsg); 
    this.add(this.shortcutMsg); 
    this.add(this.finishedMsg); 

    SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 

      if(flag==1){ 
       DosCommands.createEnFolder(); 
       createDB(); 
      } 

      else if(flag==2){ 
       DosCommands.createDB(); 
       createShortcut(); 
      } 
      else{ 
       DosCommands.createShortcut(); 
       JOptionPane.showMessageDialog(null,"Now you can delete the Endocrino folder from your Desktop"); 
       System.exit(0); 
      } 
     } 
    }); 
}//createForm 

public void createDB(){ 
    this.dbMsg.setText("Create Data Base..."); 
    this.pbar.setValue(2); 
    createForm(7,1,3,3); 
    this.flag++; 
} 

public void createShortcut(){ 
    this.shortcutMsg.setText("Create shortcut..."); 
    this.pbar.setValue(3); 
    createForm(7,1,3,3); 
    this.flag++; 
    this.finishedMsg.setText("Finished"); 
    this.pbar.setValue(4); 
} 
}//DosCommandsWindow 
0

시도() 메소드 :

t.setVisible(true);  
+0

아니, 그게 아무것도 안했어. – xarlap

관련 문제