下面列出了怎么用javax.swing.plaf.basic.BasicProgressBarUI的API类实例代码及写法,或者点击链接到github查看源代码。
public ColoredProgressBar(int start, int end) {
setMinimum(start);
setMaximum(end);
setForeground(SystemColor.window);
setBackground(SystemColor.window);
setBorder(new EmptyBorder(3, 5, 3, 5));
Dimension size = new Dimension(300, 20);
setPreferredSize(size);
setMaximumSize(size);
setMinimumSize(size);
BasicProgressBarUI ui = new BasicProgressBarUI() {
@Override
protected Color getSelectionForeground() {
return Color.BLACK;
}
@Override
protected Color getSelectionBackground() {
return Color.BLACK;
}
};
setUI(ui);
}
private void changeUI() {
setOpaque(false);
setUI(new BasicProgressBarUI() {
@Override
public void paint(Graphics g, JComponent c) {
Insets b = TiledHorizontalImageProgressBar.this.getInsets(); // area for border
int barRectWidth = TiledHorizontalImageProgressBar.this.getWidth() - (b.right + b.left);
int barRectHeight = TiledHorizontalImageProgressBar.this.getHeight() - (b.top + b.bottom);
int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
g.clipRect(b.left, b.top, amountFull, barRectHeight);
TiledImagePainter.paintComponent(TiledHorizontalImageProgressBar.this, g, tileImage);
g.setClip(null);
}
});
}
private void changeUI() {
setUI(new BasicProgressBarUI() {
@Override
public void paint(Graphics g, JComponent c) {
Insets b = TiledVerticalImageProgressBar.this.getInsets(); // area for border
int barRectWidth = TiledVerticalImageProgressBar.this.getWidth() - (b.right + b.left);
int barRectHeight = TiledVerticalImageProgressBar.this.getHeight() - (b.top + b.bottom);
int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
g.clipRect(b.left, b.top + barRectHeight - amountFull, barRectWidth, amountFull);
TiledImagePainter.paintComponent(TiledVerticalImageProgressBar.this, g, tileImage);
g.setClip(null);
}
});
}
@Override public void updateUI() {
super.updateUI();
setUI(new BasicProgressBarUI());
setStringPainted(true);
setString("");
setOpaque(false);
setBorder(BorderFactory.createEmptyBorder());
}
private SplashScreen() throws IOException
{
BufferedImage logo = ImageIO.read(SplashScreen.class.getResourceAsStream("runelite_transparent.png"));
setTitle("RuneLite Launcher");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setIconImage(logo);
setLayout(null);
Container pane = getContentPane();
pane.setBackground(DARKER_GRAY_COLOR);
Font font = new Font(Font.DIALOG, Font.PLAIN, 12);
JLabel logoLabel = new JLabel(new ImageIcon(logo));
pane.add(logoLabel);
logoLabel.setBounds(0, 0, WIDTH, WIDTH);
int y = WIDTH;
pane.add(action);
action.setForeground(Color.WHITE);
action.setBounds(0, y, WIDTH, 16);
action.setHorizontalAlignment(SwingConstants.CENTER);
action.setFont(font);
y += action.getHeight() + PAD;
pane.add(progress);
progress.setForeground(BRAND_ORANGE);
progress.setBackground(BRAND_ORANGE.darker().darker());
progress.setBorder(new EmptyBorder(0, 0, 0, 0));
progress.setBounds(0, y, WIDTH, 14);
progress.setFont(font);
progress.setUI(new BasicProgressBarUI()
{
@Override
protected Color getSelectionBackground()
{
return Color.BLACK;
}
@Override
protected Color getSelectionForeground()
{
return Color.BLACK;
}
});
y += 12 + PAD;
pane.add(subAction);
subAction.setForeground(Color.LIGHT_GRAY);
subAction.setBounds(0, y, WIDTH, 16);
subAction.setHorizontalAlignment(SwingConstants.CENTER);
subAction.setFont(font);
y += subAction.getHeight() + PAD;
setSize(WIDTH, y);
setLocationRelativeTo(null);
timer = new Timer(100, this);
timer.setRepeats(true);
timer.start();
setVisible(true);
}
private SplashScreen() throws IOException
{
BufferedImage logo = ImageUtil.getResourceStreamFromClass(SplashScreen.class, "runelite_transparent.png");
setTitle("RuneLite Launcher");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setIconImage(logo);
setLayout(null);
Container pane = getContentPane();
pane.setBackground(ColorScheme.DARKER_GRAY_COLOR);
Font font = new Font(Font.DIALOG, Font.PLAIN, 12);
JLabel logoLabel = new JLabel(new ImageIcon(logo));
pane.add(logoLabel);
logoLabel.setBounds(0, 0, WIDTH, WIDTH);
int y = WIDTH;
pane.add(action);
action.setForeground(Color.WHITE);
action.setBounds(0, y, WIDTH, 16);
action.setHorizontalAlignment(SwingConstants.CENTER);
action.setFont(font);
y += action.getHeight() + PAD;
pane.add(progress);
progress.setForeground(ColorScheme.BRAND_ORANGE);
progress.setBackground(ColorScheme.BRAND_ORANGE.darker().darker());
progress.setBorder(new EmptyBorder(0, 0, 0, 0));
progress.setBounds(0, y, WIDTH, 14);
progress.setFont(font);
progress.setUI(new BasicProgressBarUI()
{
@Override
protected Color getSelectionBackground()
{
return Color.BLACK;
}
@Override
protected Color getSelectionForeground()
{
return Color.BLACK;
}
});
y += 12 + PAD;
pane.add(subAction);
subAction.setForeground(Color.LIGHT_GRAY);
subAction.setBounds(0, y, WIDTH, 16);
subAction.setHorizontalAlignment(SwingConstants.CENTER);
subAction.setFont(font);
y += subAction.getHeight() + PAD;
setSize(WIDTH, y);
setLocationRelativeTo(null);
timer = new Timer(100, this);
timer.setRepeats(true);
timer.start();
setVisible(true);
}
private MainPanel() {
super(new BorderLayout());
BoundedRangeModel model = new DefaultBoundedRangeModel();
JProgressBar progressBar0 = new JProgressBar(model);
progressBar0.setStringPainted(true);
UIManager.put("ProgressBar.foreground", Color.RED);
UIManager.put("ProgressBar.selectionForeground", Color.ORANGE);
UIManager.put("ProgressBar.background", Color.WHITE);
UIManager.put("ProgressBar.selectionBackground", Color.RED);
JProgressBar progressBar1 = new JProgressBar(model);
progressBar1.setStringPainted(true);
JProgressBar progressBar2 = new JProgressBar(model);
progressBar2.setStringPainted(true);
progressBar2.setForeground(Color.BLUE);
progressBar2.setBackground(Color.CYAN.brighter());
progressBar2.setUI(new BasicProgressBarUI() {
@Override protected Color getSelectionForeground() {
return Color.PINK;
}
@Override protected Color getSelectionBackground() {
return Color.BLUE;
}
});
JPanel p = new JPanel(new GridLayout(5, 1));
p.add(makePanel(progressBar0));
p.add(makePanel(progressBar1));
p.add(makePanel(progressBar2));
JButton button = new JButton("Test start");
button.addActionListener(e -> {
if (Objects.nonNull(worker) && !worker.isDone()) {
worker.cancel(true);
}
worker = new BackgroundTask();
worker.addPropertyChangeListener(new ProgressListener(progressBar0));
worker.addPropertyChangeListener(new ProgressListener(progressBar1));
worker.addPropertyChangeListener(new ProgressListener(progressBar2));
worker.execute();
});
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(button);
box.add(Box.createHorizontalStrut(5));
addHierarchyListener(e -> {
boolean isDisplayableChanged = (e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0;
if (isDisplayableChanged && !e.getComponent().isDisplayable() && Objects.nonNull(worker)) {
System.out.println("DISPOSE_ON_CLOSE");
worker.cancel(true);
worker = null;
}
});
add(p);
add(box, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setPreferredSize(new Dimension(320, 240));
}
private void setup() {
this.setLayout(new MigLayout("ins 05, gapx 5, align left, "));
setBackground(new Color(51, 51, 51));
setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
skillLabel = new JLabel("SKILL:");
rankLabel = new JLabel("RANK:");
xpLabel = new JLabel("XP:");
xpToLevelLabel = new JLabel("XP TO LEVEL:");
xpToLevelBar = new JProgressBar(0);
xpToLevelBar.setStringPainted(true);
skill = new JLabel("");
rank = new JLabel("");
xp = new JLabel("");
xpToLevel = new JLabel("");
skillLabel.setFont(new Font(skillLabel.getFont().getFontName(), Font.BOLD, skillLabel.getFont().getSize()));
rankLabel.setFont(new Font(skillLabel.getFont().getFontName(), Font.BOLD, rankLabel.getFont().getSize()));
xpLabel.setFont(new Font(skillLabel.getFont().getFontName(), Font.BOLD, xpLabel.getFont().getSize()));
xpToLevelLabel.setFont(new Font(skillLabel.getFont().getFontName(), Font.BOLD, xpToLevelLabel.getFont().getSize()));
skill.setFont(new Font(skillLabel.getFont().getFontName(), Font.BOLD, skillLabel.getFont().getSize()));
rank.setFont(new Font(skillLabel.getFont().getFontName(), Font.BOLD, rankLabel.getFont().getSize()));
xp.setFont(new Font(skillLabel.getFont().getFontName(), Font.BOLD, xpLabel.getFont().getSize()));
xpToLevel.setFont(new Font(skillLabel.getFont().getFontName(), Font.BOLD, xpToLevelLabel.getFont().getSize()));
skillLabel.setForeground(Color.white);
rankLabel.setForeground(Color.white);
xpLabel.setForeground(Color.white);
xpToLevelLabel.setForeground(Color.white);
skill.setForeground(Color.white);
rank.setForeground(Color.white);
xp.setForeground(Color.white);
xpToLevel.setForeground(Color.white);
xpToLevelBar.setForeground(Color.green);
xpToLevelBar.setBackground(Color.red);
xpToLevelBar.setBorder(BorderFactory.createLineBorder(Color.black));
xpToLevelBar.setUI(new BasicProgressBarUI());
add(skillLabel, "cell 0 0,spanx ");
add(rankLabel, "cell 0 1,spanx ");
add(xpLabel, "cell 0 2, gap 0,spanx");
add(xpToLevelLabel, "cell 0 3, gap 0,spanx ");
add(skill, "cell 1 0,spanx, gap 45");
add(rank, "cell 1 1, spanx, gap 45");
add(xp, "cell 1 2,spanx, gap 25");
add(xpToLevel, "cell 1 3, spanx, gap 100");
add(xpToLevelBar, "cell 0 4,spanx, width 100% ");
}
/**
* Paints the progress string.
*
* @param g Graphics used for drawing.
* @param x x location of bounding box
* @param y y location of bounding box
* @param width width of bounding box
* @param height height of bounding box
* @param fillStart start location, in x or y depending on orientation,
* of the filled portion of the progress bar.
* @param amountFull size of the fill region, either width or height
* depending upon orientation.
* @param b Insets of the progress bar.
*/
private void paintString(Graphics g, int x, int y, int width, int height,
int fillStart, int amountFull, Insets b)
{
//* 由Jack Jiang修改:因父类中的本方法是private(其实完全可以弄成protected),而如果要全部把该方法拷贝过来
//* 则会因它调用了sun的非公开api而使得在不同的java版本里有兼容性问题(就是SwingUtilities2类,在1.5里
//* 位于com.sun.swing.**而在1.6及1.7里是放在sun.swing里的,而且以后果的版本它还会不会改位置也说不定),
//* 所以为了兼容性,此处利用反射来非正常的调用父类私有方法paintString(正常情况下子类是调用不到了父类的私有方法的撒)
ReflectHelper.invokeMethod(BasicProgressBarUI.class, this, "paintString"
, new Class[]{Graphics.class, int.class, int.class, int.class, int.class, int.class, int.class, Insets.class}
, new Object[]{g,x,y,width,height,fillStart,amountFull,b});
}