Alguem sabe se é possível eu escrever o texto de um botão em 2 lisnhas?
Voce pode utilizar codigo em html para montagem do botão, veja o seguinte exemplo :
import javax.swing.;
import java.awt.;
public class Teste {
public Teste() {
JFrame frame = new JFrame(“Teste”);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JButton("<html><body>linha 1<br>linha 2</body></html>"));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String args[]) {
new Teste();
}
}
[color=“blue”][/color]
import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.util.Hashtable;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.awt.font.TextAttribute;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class LineBreakButton extends JButton {
private LineBreakMeasurer lineMeasurer;
private int paragraphStart;
private int paragraphEnd;
private int x, y;
public LineBreakButton() {
super();
}
public LineBreakButton (AttributedString text, int width, int height) {
this();
setPreferredSize( new Dimension( width, height ) );
AttributedCharacterIterator paragraph = text.getIterator();
paragraphStart = paragraph.getBeginIndex();
paragraphEnd = paragraph.getEndIndex();
// Create a new LineBreakMeasurer from the paragraph.
lineMeasurer = new LineBreakMeasurer(paragraph,
new FontRenderContext(null, false, false));
this.x = width;
this.y = height;
}
public LineBreakButton (AttributedString text, int width, int height, float alignX, float alignY) {
this( text, width, height );
setAlignmentX( alignX );
setAlignmentY( alignY );
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics2D = (Graphics2D) g;
Dimension size = getSize();
float formatWidth = (float) size.width;
float drawPosX = 0;
float drawPosY = 1;
lineMeasurer.setPosition(paragraphStart);
while (lineMeasurer.getPosition() < paragraphEnd) {
TextLayout layout = lineMeasurer.nextLayout(formatWidth);
drawPosY += layout.getAscent();
if (layout.isLeftToRight()) {
drawPosX = 1;
}
else {
drawPosX = formatWidth - layout.getAdvance();
}
layout.draw(graphics2D, drawPosX, drawPosY);
drawPosY += layout.getDescent() + layout.getLeading();
}
setSize( x, y);
}
}
Bom trabalho