2014-07-14 132 views
0

我的應用程序出現問題。這個概念是我想創建一個顯示java命令進度的窗口。下面是這種代碼:未顯示JPanel內容

import java.awt.*; 

import javax.swing.*; 

class DosCommandsWindow extends JFrame{ 

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

    public DosCommandsWindow()//arxikh dhmiourgia tou frame 
    { 
     super("Endocrino Installation"); 
     this.setSize(400,200); 
     this.setMinimumSize(new Dimension(400,200)); 
     this.setMaximumSize(new Dimension(400,200)); 
     this.setResizable(true); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    }//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(); 
    }//createForm 

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


    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("jju"); 
     c.add(this.enFolderMsg); 
    }//addEnFolderMsg 

    public void addDBMsg()//prosthetei to label gia tin dimiourgia tis basis 
    { 
     Container c=this.getContentPane(); 
     this.dbMsg=new JLabel("jbj"); 
     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("jbgj"); 
     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("jg"); 
     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(){ 
     this.enFolderMsg.setText("Creating Endocrino Folder..."); 
     java.lang.Thread.sleep(1000); 
     revalidate();repaint(); 
     this.dbMsg.setText("Creating Data Base..."); 
     java.lang.Thread.sleep(1000); 
     revalidate();repaint(); 
     this.shortcutMsg.setText("Creating shortcut..."); 
     java.lang.Thread.sleep(1000); 
     revalidate();repaint(); 
     this.finishedMsg.setText("Finished"); 
     JOptionPane.showMessageDialog(null,"Now you can delete the Endocrino folder from your Desktop"); 
     close(); 
    }//dosCommands 
}//DosCOmmandsWindow 

的問題是,當我運行這個類一切順利,但是當從另一個JFrame中調用它沒有。這是調用DosCommandsWindow

import java.awt.*; 

import javax.swing.*; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.FileNotFoundException; 

class SettingsForm extends JFrame{ 

    JButton submit,defaultOption;//koumpia kataxwrhshs-akurwshs 
    JLabel host;//koina JLabel.Etiketes host, port, dbName, user kai password 
    JTextField hostT;//koina JTextField.perioxh egrafhs twn host, port, dbName, user kai password 

    public SettingsForm()//arxikh dhmiourgia tou frame 
    { 
     super("Data Base Information"); 
     this.setSize(300,300); 
     this.setMinimumSize(new Dimension(300,300)); 
     this.setMaximumSize(new Dimension(300,300)); 
     this.setResizable(true); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 

    }//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) 
       { 
        try { 
        CreateSettings s = new CreateSettings("Endocrino\\settings.txt"); 
        s.write(hostT.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"); 
      } 
     }); 
    }//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 showForm()//emfanizei to frame 
    { 
     this.setVisible(true); 
    }//showFrame 

    public void addElements(){ 
     this.addHost(); 
     this.addButtons(); 
    }//addElements 

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

不要使用'的Thread.sleep( 1000);'在swing應用程序中,而不是** Swing Timer **。 – Braj

+2

儘量避免使用'setMinimumSize()','setMaximumSize()'和'setSize()',並讓Layout Manager根據組件的首選大小設置大小和位置。 @ Braj的推薦使用'JFrame#pack()'而不是'setSize()' – Braj

+1

1+。您的代碼忽略Java Swing線程規則。另外,您知道Java區分大小寫並且拼寫很重要,因爲dosComandsWindow與DosCommandsWindow不同。你的SettingsForm窗口聲明瞭一個DosCommandsWindow,而你在上面發佈的類被稱爲dosComandsWindow,它的拼寫和大小寫不同。你的代碼似乎也有很多不必要的複雜性。 –

回答

2

是Swing的線程規則是GUI的所有更新運行在一個名爲EDT,又名事件調度線程的線程框架。因此,如果你讓線程進入睡眠狀態,它會阻止所有GUI更新過程並凍結,或者產生不可預知的結果。

使用工作線程的建議,
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

或振盪定時器作爲Braj在他的評論
http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

我給一個粗略的估計您的應用程序是不是這個原因提到工作如預測與線程有關。

此外,我會建議編寫與MVC結構, 搖擺應用程序,所以你可以處置一幀,並與控制器顯示其他。

這裏是基礎知識的一個很好的教程:
http://www.newthinktank.com/2013/02/mvc-java-tutorial/

並且還通過氣墊船全鰻魚其中給了我很大insipiration寫了一個很好的例子:
https://stackoverflow.com/a/15729267/3610291