| |
14. 15. 6. 水平对齐 |
|
By default, the text alignment is left-justified. |
public void setHorizontalAlignment(int alignment) method takes an argument of |
- JTextField.LEFT,
- JTextField.CENTER,
- JTextField.RIGHT,
- JTextField.LEADING (the default), or
- JTextField.TRAILING.
|
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LabelSampleHorizontalAlignment {
public static void main(String args[]) {
JFrame frame = new JFrame("Label Focus Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Name: ");
label.setDisplayedMnemonic(KeyEvent.VK_N);
JTextField textField = new JTextField();
textField.setHorizontalAlignment(JTextField.CENTER) ;
label.setLabelFor(textField);
panel.add(label, BorderLayout.WEST);
panel.add(textField, BorderLayout.CENTER);
frame.add(panel, BorderLayout.NORTH);
frame.setSize(250, 150);
frame.setVisible(true);
}
}
|
|
|