Dos lineas de texto en un Boton?

Chicode24
02 de Septiembre del 2005
Se pueden meter dos lineas de texto dentro de un JButton ancho?

He probado con un 'setText ("linea1n"+"linea2") ' pero me las junta en una sola..

Gracias. Saludos

chuidiang
02 de Septiembre del 2005
Hola:

Creo que los label (y botones) tragan texto html reducido. Prueba a meter un retorno de carro html

setText ("linea1<br>linea2");

Se bueno.

Chicode24
02 de Septiembre del 2005
No ha servido pero gracias por la respuesta.

Me tendre que conformar con botones de una sola linea..

Saludos

PLlamosas
02 de Septiembre del 2005
Prueba esto, lo estoy copiando de los foros de Sun.

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

public class CustomButton extends JButton {

private static final int BUTTON_HEIGHT = 36;
private String text1, text2;
private int prefHeight = -1;

public CustomButton(String text1, String text2) {
super("");
this.text1 = text1;
this.text2 = text2;
}

public Dimension getPreferredSize() {
int w = getWidth();
int h = prefHeight;
if (h == -1) h = BUTTON_HEIGHT;
return new Dimension(w,h);
}

// Paint the button
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

int w = getWidth();
int h = getHeight();

FontMetrics fm = g.getFontMetrics();
int textw1 = fm.stringWidth(text1);
int textw2 = fm.stringWidth(text2);

FontRenderContext context = g2.getFontRenderContext();
LineMetrics lm = getFont().getLineMetrics(text1, context);
int texth = (int)lm.getHeight();
prefHeight = texth;

int x1 = (w - textw1) / 2;
int x2 = (w - textw2) / 2;
int th = (texth * 2);
int dh = ((h - th) / 2);
int y1 = dh + texth - 3;
int y2 = y1 + texth;

// Draw texts
g.setColor(getForeground());
g.drawString(text1, x1, y1);
g.drawString(text2, x2, y2);
}

public static void main(String[] args) {
CustomButton b1 = new CustomButton("multi line", "button");
JButton b2 = new JButton("Simple button");
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(b1, BorderLayout.NORTH);
f.getContentPane().add(b2, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}

Chicode24
02 de Septiembre del 2005
Interesante, ya voy a probar.

Gracias ;)

Chicode24
02 de Septiembre del 2005
Parece que funciona aunque a medias, ya que al add un Actionlistener el texto del boton es "" con lo que para cada boton habria que crear una clase cambian el "" por un texto diferente cada vez. Pero bueno, que esta bien, gracias

Chicode24
02 de Septiembre del 2005
Esto ultimo que he dicho parece que no funciona del todo, pero a seguir probando..

chuidiang
02 de Septiembre del 2005
Hola:

He tenido un pequeño fallo. Esto si funciona:

JButton boton = new JButton ("<html>a<br>b</html>");

Se bueno.

Chicode24
02 de Septiembre del 2005
Pues eres un genio, funciona perfecto.

Gracias ;) Saludos