我试图创建一个自定义的JComponent,它将成为一个自定义的进度条,但是我无法让它显示在现有的JPanel(确实显示正确)上,不确定是什么问题。
public class JCustomProgressBar extends JComponent{
private final Dimension SIZE = new Dimension( 120, 120 );
public JCustomProgressBar() {
super();
this.setVisible(true);
}
int progress = 50; // temporary keeps progress at 50% for now while I debug
public void updateProgress (int progress){
this.progress = progress;
}
@Override
public Dimension getPreferredSize() {
return SIZE;
}
@Override
public void paintComponent (Graphics g){
//super.paintComponent(g);
System.out.println("called");
Graphics2D g2D = (Graphics2D) g.create();
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2D.translate(this.getWidth()/2, this.getHeight()/2);
g2D.rotate(Math.toRadians(270));
Arc2D.Float arc = new Arc2D.Float (Arc2D.PIE);
Ellipse2D circle = new Ellipse2D.Float(0, 0, 110, 110);
arc.setFrameFromCenter (new Point(0,0), new Point (120, 120));
circle.setFrameFromCenter(new Point(0,0), new Point (110, 110));
arc.setAngleExtent(progress*360/100);
g2D.setColor(Color.green);
g2D.draw(arc);
g2D.fill(arc);
g2D.setColor(Color.gray);
g2D.draw(circle);
g2D.fill(circle);
g2D.setColor(Color.black);
g2D.rotate(Math.toRadians(90));
g2D.setFont(new Font("Verdana", Font.PLAIN, 50));
FontMetrics fm = g2D.getFontMetrics();
Rectangle2D r = fm.getStringBounds(progress + "%", g);
int x = (0 - (int) r.getWidth())/2;
int y = (0 - (int) r.getHeight())/2 +fm.getAscent();
g2D.drawString(progress + "%", x, y);
g2D.dispose();
}
这样我就在单击按钮时调用了我的draw方法(可以正常工作并正确绘制我的JPanel)。
/**Called when a button created using createButton() is clicked. This method
should return true if it handled the click event, and false if the click
should be passed on to the parent.
Overrides: onButtonClick(...) in GuiScreen*/
@Override
protected boolean onButtonClick(String id) {
if(id.equals("btnIDConfirmL")) {
drawLeftFillProgressBar();
}
return false;
}
在我的draw方法中,我将JPanel添加到我的父级JPanel中,为其指定了边界,颜色和边框。它可以正确显示在屏幕上,并且此时一切都很好,但是当我尝试添加自己制作的JComponent时,它永远不会显示在屏幕上。我可以在此处创建的JPanel中添加另一个JPanel,也可以出于我的JComponent不会显示的原因而添加JLabel。
private JComponent customProgressBarL;
private JPanel backgroundPanel;
private void drawLeftFillProgressBar(){
leftFillProgressJPanel = createPanel(this.backgroundPanel, new Rectangle(0,backgroundPanel.getHeight()/2 - (this.getPanel().getWidth()/3 - 2*10)/2, this.getPanel().getWidth()/3 - 2*10, this.getPanel().getWidth()/3 + 87));
leftFillProgressJPanel.setBackground(Color.white);
leftFillProgressJPanel.setBorder(BorderFactory.createLineBorder(Color.black, 1));
customProgressBarL = new JCustomProgressBar();
leftFillProgressJPanel.add(customProgressBarL);
customProgressBarL.setVisible(true);
customProgressBarL.validate();
}
我的createPanel方法位于我的GuiScreen类内部,如下所示:
protected JPanel createPanel(JComponent parent, Rectangle bounds){
JPanel panel = new JPanel();
panel.setLayout(null);
if(bounds != null){
panel.setBounds(bounds);
}
if(parent != null){
parent.add(panel);
}
return panel;
}
答案 0 :(得分:0)
(代表问题作者发布了解决方案)。
我通过在draw方法中添加以下内容解决了这个问题:事实证明,我必须使用setBounds方法指定范围,以便在将其添加到我的父级JPanel后,范围包含整个JComponent。
customProgressBarL = new JCustomProgressBar();
leftFillProgressJPanel.add(customProgressBarL);
int width = 190;
int height = 290;
customProgressBarL.setBounds(new Rectangle(leftFillProgressJPanel.getWidth()/2 - width/2,leftFillProgressJPanel.getHeight()/2 - height/2, width, height));