首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java界面设计,布局有关问题

2013-01-18 
java界面设计,布局问题在上面这张图片里面,在我自己的定义中,JFrame是网格布局我是这样定义的:jf.setLayou

java界面设计,布局问题
java界面设计,布局有关问题


在上面这张图片里面,

在我自己的定义中,JFrame是网格布局
我是这样定义的:jf.setLayout(new GridLayout(3,1)) ;// jf 是JFrame的对象
然后来问题了,


问题1、:

怎么才能使得【用户账号】和【用户密码】界面两者的间隔变小

问题2、

我想在【用户账号】的上面加3行子,怎么加?我直接用Label貌似不给力,望指教!




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>";
  }
}

热点排行