开发者

Painting a custom JButton and a text line

开发者 https://www.devze.com 2023-01-23 09:52 出处:网络
I\'m designing an optimization system for public transport in a big city. So I have a map with some points on it, but don\'t care about it)

I'm designing an optimization system for public transport in a big city. So I have a map with some points on it, but don't care about it)

All I need is: my own JButton, which looks like a color-filled circle and a small text tag near it. I got some problems while overriding the paintComponent() method.. the round button is painted correctly, but not the text.

BUT, when i'm resizing the window manually, the text appears for a second, then it gets repainted again and dissapears. Hope you guys understood my needs, thanks for help ;)

import java.awt.*;  
import javax.swing.*;  


public class JRoundButton extends JButton { 

String label;
Color color;
int x,y;
public JRoundButton(Color color,int x,int y,String str)
{
    label=str;
    this.x=x;
    this.y=y;   
 开发者_运维知识库   this.color=color;       
}

protected void paintComponent(Graphics g)  
    {
    super.paintComponent(g);
    Dimension size = getPreferredSize();
    setPreferredSize(size);
    this.setBounds(0, 0, 10, 10);
    setContentAreaFilled(false);

    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawChars(label.toCharArray(), 0, label.length(), 12,12);
    g.fillOval(0,0,8,8);
}

public void paintBorder(Graphics g)
    {
    g.setColor(Color.white);
    g.drawOval(0,0, 9, 9);
}
public static void main(String[] args)  
    {
    JButton button = new JRoundButton(Color.GRAY,150,150,"Times Square");

    JFrame frame = new JFrame();
    frame.getContentPane().setBackground(Color.black);
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.add(button);
}

}


Seems that the call to 'setBounds( 0, 0, 10, 10 )' sets a component footprint that is too small to accomodate the text string. Extending the bounds to 100px wide and bringing down the point size to 6 looks to work okay.


1) NEVER set properties of the button in the paintComponent() method.

Dimension size = getPreferredSize(); 
setPreferredSize(size); 
this.setBounds(0, 0, 10, 10); 
setContentAreaFilled(false); 

Get rid of the above code.

2) Dont set the Font of the Graphics object in the paintComponent() method. Thats what the setFont(...) method is used for.

3) There is no need to do any custom painting. If you want a circle, then add an Icon to the JLabel.

4) Don't override the paintBorder() method. If you want a Border then create a custom border and add it to the button using the setBorder() method.

In short there is no need to extend the button. Get rid of your JRoundButton class. Your code should simply look something like:

JButton = new JButton("Times Square");
button.setFont( new Font("Arial",Font.BOLD,14) );
button.setIcon( new OvalIcon(Color.WHITE, iconSize) );

Of course you will need to create an OvalIcon class but that is easy to implement since there are only three methods and you already know what the painting code should be.


I'd just cheat and use a unicode circle in the JButton's text. E.g.:

import javax.swing.*;

JFrame frame = new JFrame();
frame.getContentPane().add(new JButton("<html><font size='+10' color='red'>&#x25CF;</font> I'm next to a red circle!</html>"));
frame.pack();
frame.show();
0

精彩评论

暂无评论...
验证码 换一张
取 消