Swing文本域的编辑
1、.setEditable(false); 设置文本域不可编辑
2、.setHorizontalAlignment(JTextField.CENTER); // 设置文本的水平对齐方式
有效值包括:
JTextField.LEFT 居左
JTextField.CENTER 居中
JTextField.RIGHT 靠右
JTextField.LEADING 靠左上
JTextField.TRAILING 靠右下
3、.setColumns(18); 设置此 TextArea 中的列数为18
4、.setVgap(10); 设置组件之间的垂直距离为10(int型)
5、.setHgap(10); 设置组件之间的水平距离为10(int型)
1 package Com.MySwing; 2 import java.awt.BorderLayout; 3 import java.awt.Dimension; 4 import java.awt.EventQueue; 5 import java.awt.Toolkit; 6 import java.awt.event.WindowAdapter; 7 import java.awt.event.WindowEvent; 8 import javax.swing.JFrame; 9 import javax.swing.JPanel; 10 import javax.swing.border.EmptyBorder; 11 12 13 public class EightSwing extends JFrame { 14 private static final long serialVersionUID = 8256394807443698879L; 15 private JPanel contentPane; 16 public static void main(String[] args) { 17 EventQueue.invokeLater(new Runnable() { 18 public void run() { 19 try { 20 EightSwing frame = new EightSwing(); 21 frame.setVisible(true); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 } 26 }); 27 } 28 public EightSwing() { 29 addWindowListener(new WindowAdapter() { 30 @Override 31 public void windowOpened(WindowEvent e) { 32 do_this_windowOpened(e); 33 } 34 }); 35 setTitle("\u6839\u636E\u684C\u9762\u5927\u5C0F\u8C03\u6574\u7A97\u4F53\u5927\u5C0F"); 36 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 37 setBounds(100, 100, 450, 300); 38 contentPane = new JPanel(); 39 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 40 contentPane.setLayout(new BorderLayout(0, 0)); 41 setContentPane(contentPane); 42 } 43 protected void do_this_windowOpened(WindowEvent e) { 44 Toolkit toolkit = getToolkit();// 获得窗体工具包 45 Dimension screenSize = toolkit.getScreenSize();// 获取屏幕大小 46 int width = (int) (screenSize.width * 0.8);// 计算窗体新宽度 47 int height = (int) (screenSize.height * 0.8);// 计算窗体新宽度 48 setSize(width, height);// 设置窗体大小 49 } 50 }