希望能帮到楼主:
很多人喜欢绝对定位,也就是 setLayout(null),大概是因为 Swing 的 LayoutManager 不是特别友好,算是个学习的小障碍。但是稍微专业一点的UI程序员都不会推荐绝对定位,尤其是控件上有文字的时候,甚至在使用了LayoutManager以后,都不推荐你手动去 setPreferredSize(),因为——你不能假设文字的大小是固定的,你什么时候见过专业的用户界面,会因为你调整了系统的字体大小就变得很难看或者干脆文字都变成一堆“……”?
下面是布局代码,效果见图:
- import java.awt.BorderLayout;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.util.Arrays;
- import java.util.List;
- import javax.swing.AbstractAction;
- import javax.swing.Action;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- import javax.swing.UIManager;
- /**
- *
- * @date 20/12/2012
- */
- public class LoginPanel extends JPanel {
-
- public static void main(String[] args) {
-
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
-
- try {
-
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- catch(Exception e) {
-
- e.printStackTrace();
- }
-
- JFrame test = new JFrame("欢迎登入在线销售系统");
-
- test.setContentPane(new LoginPanel());
- test.pack();
- test.setResizable(false);
-
- test.setLocationRelativeTo(null);
- test.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- test.setVisible(true);
- }
- });
- }
-
- private static final String LABEL_USERNAME = "用户账号:";
- private static final String LABEL_PASSWORD = "用户密码:";
-
- private static final String LABEL_OK = "确定";
- private static final String LABEL_Cancel = "取消";
-
- private static final int FIELD_COLS = 20;
-
- private JTextField fieldUsername;
- private JPasswordField fieldPassword;
-
- private Action actionOK;
- private Action actionCancel;
-
- LoginPanel() {
-
- super(new BorderLayout(5, 5));
-
- assert SwingUtilities.isEventDispatchThread();
-
- fieldUsername = new JTextField(FIELD_COLS);
- fieldPassword = new JPasswordField(FIELD_COLS);
-
- actionOK = new AbstractAction(LABEL_OK) {
- @Override
- public void actionPerformed(ActionEvent e) {
-
- //@TODO to be implemented.
- }
- };
-
- actionCancel = new AbstractAction(LABEL_Cancel) {
- @Override
- public void actionPerformed(ActionEvent e) {
-
- //@TODO to be implemented.
- }
- };
-
- add(layoutText(Arrays.asList(
-
- "第一行文字",
- "第二行文字",
- "第三行文字"
- )), BorderLayout.NORTH);
- add(layoutFields(), BorderLayout.CENTER);
- add(layoutControl(), BorderLayout.SOUTH);
-
- setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
- }
-
- private JComponent layoutControl() {
-
- JComponent result = new JPanel();
-
- result.add(new JButton(actionOK));
- result.add(new JButton(actionCancel));
-
- return result;
- }
-
- private JComponent layoutFields() {
-
- JComponent result = new JPanel(new GridBagLayout());
- GridBagConstraints gbc = new GridBagConstraints();
-
- gbc.gridx = 0;
- gbc.gridy = 0;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.NONE;
- gbc.insets = new Insets(5, 5, 5, 5);
- result.add(new JLabel(getBoldHTML(LABEL_USERNAME)), gbc);
- gbc.gridy++;
- result.add(new JLabel(getBoldHTML(LABEL_PASSWORD)), gbc);
-
- gbc.gridx++;
- gbc.gridy = 0;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- result.add(fieldUsername, gbc);
- gbc.gridy++;
- result.add(fieldPassword, gbc);
-
- return result;
- }
-
- private JComponent layoutText(List<String> lines) {
-
- assert lines != null;
-
- JComponent result = new JPanel(new GridLayout(lines.size(), 1));
- for(String line : lines)
- result.add(new JLabel(line));
-
- result.setBorder(BorderFactory.createEmptyBorder(5, 10, 0, 10));
- return result;
- }
-
- private String getBoldHTML(String s) {
-
- return "<html><b>" + s + "</b></html>";
- }
- }
复制代码 |