gpt4 book ai didi

java - JProgressBar.stringPainted(true);不管用

转载 作者:太空宇宙 更新时间:2023-11-04 14:37:19 24 4
gpt4 key购买 nike

这是我的java代码的一部分,在这段代码中我编写了当我单击按钮时JProgressBar的值应该变成0stringPainted () 返回 true,但是当我单击按钮时,“string Paint” 不可见,请帮忙。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class R implements ActionListener {
static int y;
CustomProgressBar b = new CustomProgressBar();

public static void main(String arg[]) throws Exception {
new R();
}

public R() throws Exception {
JFrame f = new JFrame();
JButton btn = new JButton("Click");
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setUndecorated(true);
f.setLayout(new FlowLayout());
btn.addActionListener(this);
f.add(b);
f.add(btn);
f.setVisible(true);
}

class CustomProgressBar extends JProgressBar {
private static final long serialVersionUID = 1L;
private boolean isStringToBePainted = false;

public CustomProgressBar() {
super(JProgressBar.VERTICAL,0,100);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isStringToBePainted) {
Dimension size = CustomProgressBar.this.getSize();
if (CustomProgressBar.this.getPercentComplete() < 0.9) {
R.y = (int) (size.height - size.height * CustomProgressBar.this.getPercentComplete());
}
String text = getString();
g.setColor(Color.BLACK);
g.drawString(text, 0, R.y);
}
}

@Override
public void setStringPainted(boolean b) {
isStringToBePainted = b;
}
}

@Override
public void actionPerformed(ActionEvent e) {
b.setValue(0);
b.setStringPainted(true);
}
}

最佳答案

您需要在重写方法中调用 JProgressBar setStringPainted 方法:

public class TestGame implements ActionListener
{

static int y;
CustomProgressBar progressBar = new CustomProgressBar();

public static void main(String arg[]) throws Exception
{
new TestGame();
}

public TestGame() throws Exception
{
JFrame f = new JFrame();
JButton btn = new JButton("Click");
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setUndecorated(true);
f.setLayout(new FlowLayout());
btn.addActionListener(this);
f.add(progressBar);
f.add(btn);
f.setVisible(true);
}

class CustomProgressBar extends JProgressBar
{
private static final long serialVersionUID = 1L;
private boolean isStringToBePainted = false;

public CustomProgressBar()
{
super(JProgressBar.VERTICAL, 0, 100);
}

@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (isStringToBePainted)
{
Dimension size = this.getSize();
if (this.getPercentComplete() < 0.9)
TestGame.y = (int) (size.height - size.height * this.getPercentComplete());
String text = getString();
g.setColor(Color.BLACK);
g.drawString(text, 0, TestGame.y);
}
}

@Override
public void setStringPainted(boolean b)
{
super.setStringPainted(b);
isStringToBePainted = b;
}
}

@Override
public void actionPerformed(ActionEvent e)
{
progressBar.setValue(0);
progressBar.setStringPainted(true);
}

}

关于java - JProgressBar.stringPainted(true);不管用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25415177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com