3

我不知道为什么我的 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();
        }
    }
}
4

2 回答 2

3

a 的背景颜色JProgressBar由它的 UI 委托决定ProgressBarUI。例如,

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

并非所有实现都使用颜色;例如,com.apple.laf.AquaProgressBarUI忽略设置有利于此处看到的外观。作为替代方案,您可能需要考虑有色背景或Border封闭面板,如此处建议的那样

另请注意,您可以从该方法更新 GUI ,如此process()所示。

于 2012-11-20T03:04:22.160 回答
1

一些想法:

  1. 您不应该将组件直接添加到 JFrame。相反,您的内容应该放在“内容窗格”中。请参阅 JFrame.setContentPane()、JFrame.getContentPane()。您可能需要参考 JFrame 文档或教程。

  2. 如果一个组件不是不透明的,那么它的背景将不会被渲染。请参阅 JComponent.setOpaque()、Component.isOpaque()。

于 2012-11-20T02:18:39.877 回答