2012-08-09 51 views
2

我寫一個小程序,轉換文件,我想有一個框彈出,詢問用戶,請稍候程序遍歷,並將所有相關的文件,但我遇到了一個小問題。彈出的框應該有一個JLabel和一個JButton,而用戶正在「等待」,我想顯示一條消息,說請等待,並禁用「確定」JButton,然後當它完成後,我想設置文本的JLabel讓他們知道它成功地轉換了他們的文件,並且給他們計算了轉換了多少文件。 (我寫了一個名爲alert的方法,用於設置標籤的文本並啓用按鈕。)問題是程序運行時,該框爲空,標籤和按鈕不可見,當標籤和按鈕結束時,標籤出現與我想要的最終文本和按鈕顯示啓用。我不確定到底發生了什麼,我試着多次更改JLabel和JButton的修改器,但我似乎無法使其正常工作。這裏是彈出的框的代碼,任何幫助都非常appricated。變化的JLabel與方法

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 


public class PleaseWait extends javax.swing.JFrame{ 

    private static final int height = 125; 
    private static final int width = 350; 
    final static JLabel converting = new JLabel("Please Wait while I convert your files"); 
    private static JButton OK = new JButton("OK"); 



    public PleaseWait(){ 
     // creates the main window // 
     JFrame mainWindow = new JFrame(); 
     mainWindow.setTitle("Chill For A Sec"); 
     mainWindow.setSize(width, height); 
     mainWindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 

     // creates the layouts// 
     JPanel mainLayout = new JPanel(new BorderLayout()); 
     JPanel textLayout = new JPanel(new FlowLayout()); 
     JPanel buttonLayout = new JPanel(new FlowLayout()); 

     // Sets Text // 
     converting.setText("Please wait while I convert your files"); 

     // disables button // 
     OK.setEnabled(false); 

     // adds to the layouts // 
     textLayout.add(converting); 
     buttonLayout.add(OK); 
     mainLayout.add(textLayout, BorderLayout.CENTER); 
     mainLayout.add(buttonLayout, BorderLayout.SOUTH); 

     // adds to the frame // 
     mainWindow.add(mainLayout); 

     // sets everything visible // 
     mainWindow.setVisible(true); 

    } 

    public static void alert(){ 
     OK.setEnabled(true); 
     String total = String.valueOf(Convert.result()); 
     converting.setText("Sucsess! " + total + " files Converted"); 
    } 

} 
+0

你可以顯示你調用alert()嗎? – 2012-08-09 19:18:45

回答

1

好的,這是問題所在。您正在擴展JFrame。這意味着你的類是一個JFrame。

當您創建PleaseWait幀你不對它做任何事情。這是你看到的空盒子。而是在您的構造函數中創建一個不同的 JFrame。刪除您的mainWindow,而只是使用this。現在,所有組件都將被添加到您的PleaseWait對象中。這應該解決您的空白框問題。

+0

我給它一個鏡頭,但它沒有解決問題。但是,感謝您讓我知道這一點,這是有用的信息。此外,警報被稱爲在我的轉換功能,inital調用是這樣的: PLEASEWAIT holdOnaSec =新PLEASEWAIT(); 轉換方法中的重要內容 holdOnaSec.alert(); – user1588265 2012-08-09 19:57:08

1

您需要應用程序先建立框架。這是這種應用程序的一個簡單例子。

import javax.swing.UIManager; 
import java.awt.*; 

public class Application { 
    boolean packFrame = false; 

    //Construct the application 
    public Application() { 
    PleaseWait frame = new PleaseWait(); 
    //Validate frames that have preset sizes 
    //Pack frames that have useful preferred size info, e.g. from their layout 
    if (packFrame) { 
     frame.pack(); 
    } 
    else { 
     frame.validate(); 
    } 
    //Center the window 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    Dimension frameSize = frame.getSize(); 
    if (frameSize.height > screenSize.height) { 
     frameSize.height = screenSize.height; 
    } 
    if (frameSize.width > screenSize.width) { 
     frameSize.width = screenSize.width; 
    } 
    frame.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2); 
    frame.setVisible(true); 

    frame.convert(); 

    } 

    //Main method 
    public static void main(String[] args) { 
    try { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    new Application(); 
    } 
} 

您必須稍微修改您的框架以將控件添加到內容窗格。您可以在創建框架後執行一些工作,然後致電警報。

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class PleaseWait extends JFrame { 

    private static final int height = 125; 
    private static final int width = 350; 
    final static JLabel converting = new JLabel(); 
    private static JButton OK = new JButton("OK"); 
    BorderLayout borderLayout1 = new BorderLayout(); 
    JPanel contentPane; 
    int count; 

    public PleaseWait(){ 
    contentPane = (JPanel)this.getContentPane(); 
    contentPane.setLayout(borderLayout1); 
    this.setSize(new Dimension(width, height)); 
    this.setTitle("Chill For A Sec"); 
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 

    // creates the layouts// 
    JPanel mainLayout = new JPanel(new BorderLayout()); 
    JPanel textLayout = new JPanel(new FlowLayout()); 
    JPanel buttonLayout = new JPanel(new FlowLayout()); 

    // Sets Text // 
    converting.setText("Please wait while I convert your files"); 

    // disables button // 
    OK.setEnabled(false); 
    OK.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) { 
     System.exit(0); 
     } 
    }); 

    // adds to the layouts // 
    textLayout.add(converting); 
    buttonLayout.add(OK); 
    mainLayout.add(textLayout, BorderLayout.CENTER); 
    mainLayout.add(buttonLayout, BorderLayout.SOUTH); 

    // adds to the frame // 
    contentPane.add(mainLayout); 
    } 

    public void convert(){ 
    count = 0; 
    for (int i = 0; i <10; i++){ 
     System.out.println("Copy "+i); 
     try { 
     Thread.sleep(1000); 
     } catch (InterruptedException e) { 
     } 
     count++; 
    } 
    alert(); 

    } 
    public void alert(){ 
    OK.setEnabled(true); 
//  String total = String.valueOf(Convert.result()); 
    converting.setText("Sucsess! " + count + " files Converted"); 
    } 

} 
+0

我認爲在更新控件之後_repaint_會很有用,但控件足夠聰明以更新自己。 – 2012-08-09 21:42:06

+0

因此,我遵循了你所說的一切,我仍然沒有任何運氣。我有一種應用程序首先創建了框架,但它與你的不同,所以我嘗試了你的。這裏是我原來的應用程序:http://pastebin.com/NinUypkH,這裏是我現在擁有的:http://pastebin.com/6dJ7zNkw – user1588265 2012-08-09 22:20:41

+0

我明白了,你只是想在你的應用邏輯中運行GUI線程,而GUI還沒有完成他的工作。你可能想在另一個工作線程中運行它。試試這個'SwingUtilities.invokeLater(Runnable的新(){ 公共無效的run(){//做你的邏輯在這裏 } }); ' – 2012-08-10 09:57:32