Anordnung von Icon und Text auf JButton

Shizzl_chrizzl

Erfahrenes Mitglied
Hi,

ich hab mal ne Frage wie ist es möglich auf einem JButton das Icon oben anzuzeigen und den Text darunter. Für gewöhnlich wird das Icon ja immer links und der Text rechts angezeigt. Nun hab ich gedacht das ich es evtl über den Zeilenumbruch \n hinbekommen hat aber leider nicht geklappt.

Bin ratlos :confused:
 
Tach,
laut API kommt doch das hier in Frage, oder?

Code:
public void setVerticalTextPosition(int textPosition)

    Sets the vertical position of the text relative to the icon.

    Parameters:
        textPosition - one of the following values:

            * SwingConstants.CENTER (the default)
            * SwingConstants.TOP
            * SwingConstants.BOTTOM
 
wenn ich das nehm wird bei mir kaum was geändert bei top wird der text zwar erhöht angezeigt aber immer noch rechts und bei bottom geschieht gar nichts.
 
Hallo!

Schau mal hier:
Code:
/**
 * 
 */
package de.tutorials;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

/**
 * @author Administrator
 * 
 */
public class JButtonExample extends JFrame {

	private JButton btn;

	public JButtonExample() {
		super("JButtonExample");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		btn = new JButton("Foo", new ImageIcon("c:/logoheader.gif"));
		btn.setVerticalTextPosition(SwingConstants.BOTTOM);
		btn.setHorizontalTextPosition(SwingConstants.CENTER);
		add(btn);

		pack();
		setVisible(true);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new JButtonExample();
	}

}

Gruß Tom
 
Hey!

Dieset Thread passt gut zu einer Frage von mir:

Kann man das Bild in einem JButton linksbündig und den Text zentriert machen?
 
Zurück