gpt4 book ai didi

java - Jprogressbar.setStringpainted(true);正在画两个字符串

转载 作者:行者123 更新时间:2023-11-30 03:46:56 29 4
gpt4 key购买 nike

这段代码产生了一个问题,当我点击按钮时,两个字符串被绘制一个水平和一个垂直,但只需要绘制水平字符串,所以请告诉我应该做什么???

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) {
super.setStringPainted(b);
isStringToBePainted=b;
}
}



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

}

最佳答案

问题是,属性 setStringPainted 已由组件定义,并且具有预定义的功能...您知道似乎想要更改...

您需要定义一个自己可以控制的新属性,或者更改 UI 委托(delegate)的工作方式,这是一项艰巨的工作,因为您需要为您可能想要的每种外观和感觉提供一个属性支持...

您可以(稍微)作弊,而不是进行自定义绘画...并使用 JLabel 代替,例如...

Progress

class CustomProgressBar extends JProgressBar {

private static final long serialVersionUID = 1L;
private boolean isStringToBePainted = false;

private JLabel progress;

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

progress = new JLabel("0%");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.SOUTH;
add(progress, gbc);
}

@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
Dimension labelSize = progress.getPreferredSize();
Insets insets = getInsets();
if (size.width < labelSize.width) {
size.width = insets.left + insets.right + labelSize.width;
}
return size;
}

}

您仍然需要提供自己的属性来打开或关闭它,但这是一个想法......

(ps-我快速浏览了一下尝试实现我自己的 UI 委托(delegate),在复制并粘贴我的第三种方法后,我放弃了,因为这只是比提供奖励更多的工作)

关于java - Jprogressbar.setStringpainted(true);正在画两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25416336/

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