Move the Label/Icon Pair in a JButton Component in Java
Description
The following code shows how to move the Label/Icon Pair in a JButton Component.
Example
//w ww . j a v a 2s .c o m
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
public class Main extends JFrame {
public Main() {
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button = new JButton("java2s.com");
// Place the contents in the northwest corner
button.setVerticalAlignment(SwingConstants.TOP);
button.setHorizontalAlignment(SwingConstants.LEFT);
/*
// Place the contents centered at the top
button.setVerticalAlignment(SwingConstants.TOP);
button.setHorizontalAlignment(SwingConstants.CENTER);
// Place the contents in the northeast corner
button.setVerticalAlignment(SwingConstants.BOTTOM);
button.setHorizontalAlignment(SwingConstants.RIGHT);
// Place the contents in the southwest corner
button.setVerticalAlignment(SwingConstants.BOTTOM);
button.setHorizontalAlignment(SwingConstants.LEFT);
// Place the contents centered at the bottom
button.setVerticalAlignment(SwingConstants.BOTTOM);
button.setHorizontalAlignment(SwingConstants.CENTER);
// Place the contents in the southeast corner
button.setVerticalAlignment(SwingConstants.BOTTOM);
button.setHorizontalAlignment(SwingConstants.RIGHT);
// Place the contents vertically centered on the left
button.setVerticalAlignment(SwingConstants.CENTER);
button.setHorizontalAlignment(SwingConstants.LEFT);
// Place the contents directly in the center
button.setVerticalAlignment(SwingConstants.CENTER);
button.setHorizontalAlignment(SwingConstants.CENTER);
// Place the contents vertically centered on the right
button.setVerticalAlignment(SwingConstants.CENTER);
button.setHorizontalAlignment(SwingConstants.RIGHT);
*/
add(button);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »