我目前正在我的程序中使用启动画面,然后我在其上显示信息,如下载文件的百分比。我想做JProgressBar
字符串,我的意思是,当蓝色进度超过字符串时,字符串的颜色会发生变化。
这是我目前正在使用的代码。
private static void splashProgress(float percent, String str)
{
if (mySplash != null && mySplash.isVisible())
{ // important to check here so no other methods need to know if there
// really is a Splash being displayed
float width = (186*percent)/100;
splashProgress = new Rectangle2D.Double(4., 211., width, 24.);
splashTextArea = new Rectangle2D.Double(width+4., 211., 186.-width, 24.);
splashGraphics.setColor(new Color(255,177,100,255));
splashGraphics.fill(splashProgress);
splashGraphics.setColor(new Color(175,25,25,255));
splashGraphics.fill(splashTextArea);
splashGraphics.setColor(new Color(25,25,25,255));
splashGraphics.drawString(str, 4,226);
// make sure it's displayed
mySplash.update();
}
}
进度条看起来像这样:
正如您所看到的,字符串与进度条的颜色相同,而我喜欢将隐藏部分的颜色设置为不同的颜色。
感谢您的帮助,对不起我的英语不好,这不是我的母语。
答案 0 :(得分:2)
您使用Graphics2D#setXORMode
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestGraphics {
public static void main(String[] args) {
new TestGraphics();
}
public TestGraphics() {
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 {
public TestPane() {
setBackground(new Color(175,25,25,255));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(new Color(255,177,100,255));
g2d.fillRect(0, 0, getWidth() / 2, getHeight());
g2d.setXORMode(new Color(175,25,25,255));
FontMetrics fm = g2d.getFontMetrics();
String text = "I'm some exciting text";
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight()- fm.getHeight()) / 2) + fm.getAscent();
g2d.drawString(text, x, y);
g2d.dispose();
}
}
}
答案 1 :(得分:0)
这是我试过的,但正如你在下面看到的那样,结果不是我的预期:D
http://i.stack.imgur.com/Lnghr.png
字符串消失,进度条无法正确重新加载。 如果你知道什么是错的,这是我的代码。
<local:TestControl2 VerticalAlignment="Top"></local:TestControl2>
并从那里调用此方法
private static void splashProgress(float percent, String str)
{
if (mySplash != null && mySplash.isVisible())
{ // important to check here so no other methods need to know if there
// really is a Splash being displayed
float width = (186*percent)/100;
splashProgress = new Rectangle2D.Double(4., 211., width, 24.);
splashTextArea = new Rectangle2D.Double(width+4., 211., 186.-width, 24.);
splashGraphics.setColor(new Color(255,177,100,255));
splashGraphics.fill(splashProgress);
splashGraphics.setColor(new Color(175,25,25,255));
splashGraphics.fill(splashTextArea);
splashGraphics.setXORMode(new Color(175,25,25,255));
splashGraphics.drawString(str, 4,226);
// make sure it's displayed
mySplash.update();
}
}
尽管如此,非常感谢你的帮助:)
编辑:我在我的while循环之外尝试while ((read = input.read(buffer)) > 0){
writeFile.write(buffer, 0, read);
writeFile.flush();
percent = (percent + read);
percentText = (int) ((percent*100)/length);
splashProgress((percent*100)/length, "Téléchargement : "+percentText+" %");
}
,现在这就是我得到的
http://i.stack.imgur.com/UrQcj.png
编辑2:好的,我解决了我的问题,实际上由于我的循环存在冲突,所以我只为绘制矩形添加了setXorMode
,然后为我的文本添加了splashGraphics.setPaintMode();
,它完美地运行。
非常感谢您的帮助和反应。