JButton addBtn = new JButton("+");
addBtn.setBounds(x_pos, y_pos, 30, 25);
addBtn.setBorder(new RoundedBorder(10)); //10 is the radius
addBtn.setForeground(Color.BLUE);
addBtn.setBorder(new RoundedBorder(10));
publi class RoundedBorder implements Border {
private int radius;
RoundedBorder(int radius) {
this.radius = radius;
}
public Insets getBorderInsets(Component c) {
return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.drawRoundRect(x, y, width-1, height-1, radius, radius);
}
}
import javax.swing.border.; // Die Schreibweise kenne ich nicht, geht das?
import javax.swing.border.Border; // Versuchs mal so ...
import javax.swing.border.*; // ... oder so ...
Vielleicht sowas.Ich brauche aber wirklich runde Buttons, andere Ideen? :/
public class RoundButton extends JButton {
private Shape shape;
private int arc;
private boolean isQuadratic;
public RoundButton(String text, int arc) {
this(text, arc, true);
}
public RoundButton(String text, int arc, boolean isQuadratic) {
super(text);
this.arc = arc;
this.isQuadratic= isQuadratic;
initComponent();
}
private void initComponent() {
if(isQuadratic) {
final Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width, size.height);
setPreferredSize(size);
}
setContentAreaFilled(false);
setFocusable(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setColor(getModel().isArmed() ? Color.orange: getBackground());
g2d.fill(new RoundRectangle2D.Double(0, 0, getWidth()-1, getHeight()-1, arc, arc));
super.paintComponent(g);
}
@Override
protected void paintBorder(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setColor(getBackground().darker());
g2d.draw(new RoundRectangle2D.Double(0, 0, getWidth()-1, getHeight()-1, arc, arc));
}
@Override
public boolean contains( int x, int y ) {
if(shape == null || !shape.getBounds().equals(getBounds())) {
this.shape = new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), arc, arc);
}
return shape.contains(x, y);
}
}
JButton btn = new RoundButton("Circle", 75);
btn.setBackground(Color.RED);
frame.add(btn);
JButton btn2 = new RoundButton("Quadrat", 20, false);
btn2.setBackground(Color.BLUE);
frame.add(btn2);
RoundButton btn = new RoundButton("Hallo", 45, true);
btn.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed( MouseEvent mouseEvent ) {
super.mousePressed(mouseEvent);
if(mouseEvent.getButton() == MouseEvent.BUTTON1) {
System.out.println("Left Click");
} else if(mouseEvent.getButton() == MouseEvent.BUTTON2) {
System.out.println("Middle Click");
} else if(mouseEvent.getButton() == MouseEvent.BUTTON3) {
System.out.println("Right Click");
}
}
});