Change button Horizontal Text Position in Java
Description
The following code shows how to change button Horizontal Text Position.
Example
/* w w w . j av a 2 s . co m*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Main extends JFrame {
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JButton b = new JButton("Test");
b.setHorizontalTextPosition(SwingConstants.LEFT);
b.setIcon(new ImageIcon("r.gif"));
b.setRolloverIcon(new ImageIcon("b.gif"));
b.setRolloverEnabled(true);
b.setMnemonic('t');
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button pressed");
}
});
p.add(b);
getContentPane().add(p);
pack();
}
public static void main(String[] args) {
Main sb = new Main();
sb.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »