要实现的结果我用的网格4行2列,效果
球指点

解决方案 »

  1.   

    网格三行一列,第二,第三行装进Jpanel里,再对Jpanel进行布局。
      

  2.   

    第一行的用JPanel,白色部分可以用setBounds();更加达到你想要的效果
      

  3.   

    密码和按钮放JPanel?用户名放JFram?
      

  4.   

    听我的吧。你什么布局都别用。这样是最合适的。
    java的图形界面功能真的是太弱了,前几天我自己尝试写一个游戏界面,就找不到任何一个适合的布局。
    contentPane.setLayout(null);//设置布局界面为空
    Jpanel1.this.setBounds(x, y, h, w);//JPanle里存放你准备添加的组件。
    x,y指的是左上角那个点相对于你Jrame界面的位置,h,w就是高和宽了。
    JLabel2.this.setBounds(x, y, h, w);
    contentPane.add(Jpanel1);
    contentPane.add(JLabel1);
    只要实现量好了坐标,用这中方式可以实现任何形式的布局。
      

  5.   

    发一个相似的,只需要把前面的logintext改成图片就可以了。这个的缺点是不能改变Jpaenl的大小,不过这个我觉得应该用不到。毕竟没有谁会故意拖动登陆框的大小。public class LoginJpaenl extends JPanel{
    private JLabel logintext;//添加的组件

    private JLabel usernametext;
    private JLabel passwordtext;
    private JTextField password;
    private JButton loginBtn;
    private JButton registerBtn;
    private JButton exitBtn;

    public LoginJpaenl(JLabel errortext,JTextField username,JTextField password,JButton loginBtn,JButton registerBtn,JButton exiButton) {

    this.setBounds(250, 200, 300, 200);
    this.setLayout(null);
    this.setBorder(BorderFactory.createLineBorder(Color.black));
    this.loginBtn=loginBtn;
    this.registerBtn=registerBtn;
    this.exitBtn=exiButton;

    logintext=new JLabel();
    logintext.setBounds(130, 20, 100, 20);
    logintext.setText("用户登录");

    errortext.setBounds(30, 45, 200, 20);
    usernametext=new JLabel();
    usernametext.setBounds(20, 70, 60, 20);
    usernametext.setText("账户名:");

    username.setBounds(80, 70, 200, 20);
    passwordtext=new JLabel();
    passwordtext.setBounds(20, 110, 60, 20);
    passwordtext.setText("密    码:");

    password.setBounds(80, 110, 200, 20);
    loginBtn.setBounds(30, 150, 60, 30);
    registerBtn.setBounds(120, 150, 60, 30);
    exitBtn.setBounds(210, 150, 60, 30);
    this.add(logintext);
    this.add(errortext);
    this.add(usernametext);
    this.add(username);
    this.add(passwordtext);
    this.add(password);
    this.add(loginBtn);
    this.add(registerBtn);
    this.add(exitBtn);

    }
    }
      

  6.   

    那天系统的字体被改变了,显示也会变形。除非非常简单的界面,一般不会使用jre自带的布局管理器,不是太简单就是太难用。JGoodies FormLayout、或 MiGLayout 才是更好的选择。
      

  7.   

    第一行的用JPanel,白色部分可以用一个JPanel空布局,然后指定坐标;后面一个可以用一个JPanel用边界布局。更加达到你想要的效果 
      

  8.   

    楼主最好是布局设置为null 这样相当于不受任何布局控制 这样可以通过坐标想怎么显示就怎么显示 还有因为楼主的功能不需要自动伸缩 所以即使用坐标的话布局也不会乱的
      

  9.   

    两台设置了不同大小界面字体的系统怎么适应?一个使用了9pt,一个使用11pt
      

  10.   


    您是专业做 Swing 的?
      

  11.   

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    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 07/11/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();
            }
            
            JDialog dialog = new JDialog();
            dialog.setTitle(dialogTitle);
            dialog.setContentPane(new LoginPanel());
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
          }
        });
      }  public LoginPanel() {
        
        setLayout(new BorderLayout(5, 5));
        add(layoutCenterPanel(), BorderLayout.CENTER);
        add(layoutControlPanel(), BorderLayout.SOUTH);
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
      }
      
      private JPanel layoutCenterPanel() {
        
        JPanel panel = new JPanel(new GridBagLayout());
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST; //
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = new Insets(10, 10, 10, 10);
        
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.NONE;
        panel.add(new JLabel(usernameLabel), gbc);
        gbc.gridy++;
        panel.add(new JLabel(passwordLabel), gbc);
        
        gbc.gridx++;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        panel.add(new JTextField(20), gbc);
        gbc.gridy++;
        panel.add(new JPasswordField(20), gbc);
        
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        return panel;
      }
      
      private JPanel layoutControlPanel() {
        
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel.add(new JButton(ok));
        panel.add(new JButton(cancel));
        return panel;
      }
      
      private static final String dialogTitle = "xxxx 系统";
      private static final String usernameLabel = "用户名: ";
      private static final String passwordLabel = "密码: ";
      
      private static final String ok = "确定";
      private static final String cancel = "取消";
    }
      

  12.   

    我根本不知道JGoodies FormLayout、或 MiGLayout 是什么
      

  13.   

    最讨厌JAVA swt swing布局了,头疼建议楼主安个可视化插件吧,这样开发会方便些
      

  14.   

    估计楼主和我一样是新手,初学者,我觉的这个明显用网格布局的话应该设置Gridlayout(5,1),至于两个按钮,在一个面板中可以添加两个按钮的