JProgressBar 背景色不出现
JProgressBar background color does not appear
我为我的 JProgressBar
做了一个 自定义 UI,如下所示:
public class BarUI extends BasicProgressBarUI {
private Rectangle r = new Rectangle();
@Override
protected void paintIndeterminate(Graphics g, JComponent c) {
Graphics2D G2D = (Graphics2D) g;
G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//r = getBox(r);
//g.setColor(c.getForeground());
//g.fillRect(r.x,r.y,r.width,r.height);
}
}
这很好用,可以将进度条的前景色更改为所需的颜色。问题是我不能改变它的背景颜色。
我尝试了以下选项,但其中 none 有效:
选项 1:
c.setBackground(new Color(184, 184, 184,255));
选项 2:
((Graphics2D) g).setBackground(new Color(184, 184, 184,255));
为什么这不能正常工作?
更新
我对代码进行了更多修改,并从 BarUI
class 中删除了最后 3 行,因为它们不需要。这也是我用来在框架内制作实际进度条的代码:
Bar.setUI(new BarUI());
Bar.setSize(new Dimension(180,2));
Bar.setBackground(new Color(184, 184, 184,255));
Bar.setForeground(new Color(0, 126, 255,255));
"appear" JProgressBar
的默认 opaque
是 false
,使进度条默认透明
所以,如果我在代码中添加bar.setOpaque(true);
,背景就会被绘制
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.basic.BasicProgressBarUI;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JProgressBar bar;
public TestPane() {
bar = new JProgressBar();
bar.setIndeterminate(true);
bar.setOpaque(true);
bar.setUI(new BarUI());
bar.setPreferredSize(new Dimension(180, 10));
bar.setBackground(new Color(184, 184, 184, 255));
bar.setForeground(new Color(0, 126, 255, 255));
add(bar);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class BarUI extends BasicProgressBarUI {
private Rectangle r = new Rectangle();
@Override
protected void paintIndeterminate(Graphics g, JComponent c) {
Graphics2D G2D = (Graphics2D) g;
G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
r = getBox(r);
g.setColor(c.getForeground());
g.fillRect(r.x,r.y,r.width,r.height);
}
}
}
我为我的 JProgressBar
做了一个 自定义 UI,如下所示:
public class BarUI extends BasicProgressBarUI {
private Rectangle r = new Rectangle();
@Override
protected void paintIndeterminate(Graphics g, JComponent c) {
Graphics2D G2D = (Graphics2D) g;
G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//r = getBox(r);
//g.setColor(c.getForeground());
//g.fillRect(r.x,r.y,r.width,r.height);
}
}
这很好用,可以将进度条的前景色更改为所需的颜色。问题是我不能改变它的背景颜色。
我尝试了以下选项,但其中 none 有效:
选项 1:
c.setBackground(new Color(184, 184, 184,255));
选项 2:
((Graphics2D) g).setBackground(new Color(184, 184, 184,255));
为什么这不能正常工作?
更新
我对代码进行了更多修改,并从 BarUI
class 中删除了最后 3 行,因为它们不需要。这也是我用来在框架内制作实际进度条的代码:
Bar.setUI(new BarUI());
Bar.setSize(new Dimension(180,2));
Bar.setBackground(new Color(184, 184, 184,255));
Bar.setForeground(new Color(0, 126, 255,255));
"appear" JProgressBar
的默认 opaque
是 false
,使进度条默认透明
所以,如果我在代码中添加bar.setOpaque(true);
,背景就会被绘制
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.basic.BasicProgressBarUI;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JProgressBar bar;
public TestPane() {
bar = new JProgressBar();
bar.setIndeterminate(true);
bar.setOpaque(true);
bar.setUI(new BarUI());
bar.setPreferredSize(new Dimension(180, 10));
bar.setBackground(new Color(184, 184, 184, 255));
bar.setForeground(new Color(0, 126, 255, 255));
add(bar);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class BarUI extends BasicProgressBarUI {
private Rectangle r = new Rectangle();
@Override
protected void paintIndeterminate(Graphics g, JComponent c) {
Graphics2D G2D = (Graphics2D) g;
G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
r = getBox(r);
g.setColor(c.getForeground());
g.fillRect(r.x,r.y,r.width,r.height);
}
}
}