2012-11-20 74 views
3

我不知道爲什麼背景顏色沒有顯示在我的Jframe上。以下是我嘗試的代碼。Java Swing JFrame背景未顯示

當我打電話

AnimatedDialogBox animatedDialogBox = new AnimatedDialogBox("Saving TransSet form", dataSheetTable); 

它沒有顯示出我所需要的確切的顏色。它顯示沒有任何背景顏色。該AnimatedDialogBox類是按照以下:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Point; 
import java.awt.Toolkit; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JProgressBar; 
import javax.swing.SwingWorker; 
import net.miginfocom.swing.MigLayout; 


public class AnimatedDialogBox { 

    private JFrame progressDialog ; 
    private JProgressBar bar; 
    private Task task; 
    private ResourceManager resourceManager = new ResourceManager(); 

    public AnimatedDialogBox(String message, JComponent parentComponent) { 
     progressDialog = new JFrame(message); 
     progressDialog.setLayout(new MigLayout()); 
     progressDialog.setUndecorated(true); 
      progressDialog.setBackground(resourceManager.getColor("error.Panel.background")); // RGB = 243, 255, 159 
     progressDialog.setPreferredSize(new Dimension(300, 100)); 
     JLabel label = new JLabel(message); 
     label.setBackground(resourceManager.getColor("error.Panel.background")); 
     progressDialog.add(label, "gapbefore 80,gapbottom 30, wrap"); 
     bar = new JProgressBar(0, 100); 
     bar.setIndeterminate(true); 
     bar.setBackground(resourceManager.getColor("error.Panel.background")); 
     progressDialog.add(bar, "gapbefore 80, gapbottom 30, wrap"); 
     progressDialog.setFocusableWindowState(false); 
     Point point = progressDialog.getLocation(); 
     Dimension cDim = parentComponent.getSize(); 

     progressDialog.setLocation((int) (cDim.getWidth()/2)-100, 
        (int) cDim.getHeight() + 350); 

     progressDialog.pack(); 

     task = new Task(); 
     task.addPropertyChangeListener(new PropertyChangeListener() { 
      @Override 
      public void propertyChange(PropertyChangeEvent evt) { 
       if (evt.getPropertyName().equalsIgnoreCase("progress")) { 
        int progress = task.getProgress(); 
        if (progress == 0) { 
         bar.setIndeterminate(true); 
        } else { 
         bar.setIndeterminate(false); 
         bar.setValue(progress); 
         progressDialog.dispose(); 
        } 
       } 
      } 
     }); 
     task.execute(); 
     progressDialog.setVisible(true); 
    } 

    class Task extends SwingWorker<Void, Void> { 
     private static final long SLEEP_TIME = 1000; 

     public Task() { 
     } 

     @Override 
     public Void doInBackground() { 
      setProgress(0); 
      try { 
       Thread.sleep(SLEEP_TIME);// imitate a long-running task 
      } catch (InterruptedException e) { 
      } 
      setProgress(100); 
      return null; 
     } 

     @Override 
     public void done() { 
      Toolkit.getDefaultToolkit().beep(); 
     } 
    } 
} 

回答

3

一個JProgressBar的背景顏色是由它的UI代理,ProgressBarUI確定。例如,

UIManager.put("ProgressBar.background", Color.red); 

並非所有實現都使用該顏色;例如,com.apple.laf.AquaProgressBarUI會忽略該設置,以支持看到的外觀here。作爲替代方案,您可能想要在封閉面板上考慮有色背景或Border,如建議here

另請注意,您可以從process()方法更新GUI,如here所示。

1

幾個想法:

  1. 你不應該直接添加組件的JFrame。相反,您的內容應該放在「內容窗格」中。請參閱JFrame.setContentPane(),JFrame.getContentPane()。您可能需要參考JFrame文檔或教程。

  2. 如果一個組件不是不透明的,那麼它的背景將不會被渲染。請參閱JComponent.setOpaque(),Component.isOpaque()。

+0

'JFrame#add()'_forwards_到'contentPane'。 – trashgod

+1

+1與'contentPane'有關的想法,但我猜測唯一缺少的是'JFrame'只覆蓋'contentPane'的三種方法,即'setLayout(),remove()和add()'。所以我猜想改變顏色是什麼是'frame.getContentPane()。setBackground(Color.RED);'。這將改變所說的東西的背景顏色。 –

+1

@GagandeepBali:很好的洞察力;對於['setBackground()'](http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#setBackground%28java.awt.Color%29)「,它已經到了以尊重這個財產的外觀和感覺,有些人可能會選擇忽視它。「 – trashgod