Java Swing的JFrame的背景没有显示 [英] Java Swing JFrame Background is not showing

查看:225
本文介绍了Java Swing的JFrame的背景没有显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么的背景颜色是不是对我的JFrame显示。下面是code,我试过了。

当我打电话

  AnimatedDialogBox animatedDialogBox =新AnimatedDialogBox(拯救TransSet表,dataSheetTable);

它不显示,我所需的确切颜色。它示出了没有任何背景颜色。该AnimatedDialogBox类是每如下:

 进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.Point中;
进口java.awt.Toolkit中;
进口java.beans.PropertyChangeEvent中;
进口的java.beans.PropertyChangeListener;
进口javax.swing.JComponent中;
进口javax.swing.JFrame中;
进口javax.swing.JLabel中;
进口javax.swing.JProgressBar中;
进口javax.swing.SwingWorker;
进口net.miginfocom.swing.MigLayout;
公共类AnimatedDialogBox {    私人的JFrame progressDialog;
    JProgressBar的私人酒吧;
    私人任务的任务;
    私人ResourceManager的ResourceManager的=新的ResourceManager();    公共AnimatedDialogBox(字符串消息,JComponent的为父级){
        progressDialog =新的JFrame(消息);
        progressDialog.setLayout(新MigLayout());
        progressDialog.setUndecorated(真);
          progressDialog.setBackground(resourceManager.getColor(error.Panel.background)); // RGB = 243,255,159
        progressDialog.set preferredSize(新尺寸(300,100));
        JLabel的标签=新JLabel的(消息);
        label.setBackground(resourceManager.getColor(error.Panel.background));
        progressDialog.add(标签,gapbefore 80,gapbottom 30,包装);
        栏=新JProgressBar的(0,100);
        bar.setIndeterminate(真);
        bar.setBackground(resourceManager.getColor(error.Panel.background));
        progressDialog.add(酒吧,gapbefore 80,gapbottom 30,包装);
        progressDialog.setFocusableWindowState(假);
        逐点= progressDialog.getLocation();
        尺寸CDIM = parentComponent.getSize();        progressDialog.setLocation((int)的(cDim.getWidth()/ 2)-100,
                    (INT)cDim.getHeight()+ 350);        progressDialog.pack();        任务=新任务();
        task.addPropertyChangeListener(新的PropertyChangeListener(){
            @覆盖
            公共无效的propertyChange(PropertyChangeEvent的EVT){
                如果(evt.getPropertyName()。equalsIgnoreCase(进步)){
                    INT进度= task.getProgress();
                    如果(进度== 0){
                        bar.setIndeterminate(真);
                    }其他{
                        bar.setIndeterminate(假);
                        bar.setValue(进度);
                        progressDialog.dispose();
                    }
                }
            }
        });
        task.execute();
        progressDialog.setVisible(真);
    }    类任务扩展的SwingWorker<太虚,太虚> {
        私有静态最后长SLEEP_TIME = 1000;        公共任务(){
        }        @覆盖
        公共无效doInBackground(){
            setProgress(0);
            尝试{
                视频下载(SLEEP_TIME); //模仿长时间运行的任务
            }赶上(InterruptedException的E){
            }
            setProgress(100);
            返回null;
        }        @覆盖
        公共无效完成(){
            Toolkit.getDefaultToolkit()音()。
        }
    }
}


解决方案

的背景颜色构造JProgressBar 是由它的UI代理确定,的ProgressBarUI 。例如,

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

不是所有的实现使用的颜色;例如, com.apple.laf.AquaProgressBarUI 忽略见过有利于外观设置的此处。作为替代方案,你可能要考虑封闭面板上的有色背景或边框,所建议的这里

另外请注意,您可以从更新GUI程序()方法,如图所示的这里

I don't know why the background color is not showing on my Jframe. Below is the code that I tried.

When I call

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

Its not showing the exact color that I needed. It shows without any background color. The AnimatedDialogBox class is per below :

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();
        }
    }
}

解决方案

The background color of a JProgressBar is determined by its UI delegate, ProgressBarUI. For example,

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

Not all implementations use the color; for example, com.apple.laf.AquaProgressBarUI ignores the setting in favor of the appearance seen here. As an alternative, you may want to consider a tinted background or Border on the enclosing panel, as suggested here.

Also note that you can update the GUI from the process() method, as shown here.

这篇关于Java Swing的JFrame的背景没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆