Dos lineas de texto en un Boton?
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
He probado con un 'setText ("linea1n"+"linea2") ' pero me las junta en una sola..
Gracias. Saludos
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.
Creo que los label (y botones) tragan texto html reducido. Prueba a meter un retorno de carro html
setText ("linea1<br>linea2");
Se bueno.
No ha servido pero gracias por la respuesta.
Me tendre que conformar con botones de una sola linea..
Saludos
Me tendre que conformar con botones de una sola linea..
Saludos
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);
}
}
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);
}
}
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
Esto ultimo que he dicho parece que no funciona del todo, pero a seguir probando..
Hola:
He tenido un pequeño fallo. Esto si funciona:
JButton boton = new JButton ("<html>a<br>b</html>");
Se bueno.
He tenido un pequeño fallo. Esto si funciona:
JButton boton = new JButton ("<html>a<br>b</html>");
Se bueno.