jackscsnd
2015-05-17 06:53
采纳率: 92.9%
浏览 14.2k
已采纳

java swing 如何控制组件的想要位置

图片说明
我做的这个是这样的 人家是这样的 我想弄人家那样的
图片说明
试了试弄不好 不会啊

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 独善其琛 2015-05-17 07:43
    已采纳
          JPanel panel = new JPanel();      
          panel.add(你的文字对应的Label名);
          panel.add(你的文本框对应的Textfield名);
          panel.setAlignmentX(Component.LEFT_ALIGNMENT);   
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridy = sliderPanel.getComponentCount();    //gridx/gridy:组件的横纵坐标
          gbc.anchor = GridBagConstraints.WEST;     
          //当组件不能填满其格时,通过 anchor来设置组件的位置,anchor有两种值,绝对和相对的值分别有 若干个
          你的能大panel.add(panel, gbc);    
    
    
    

    这个是我做的滑动条的部分代码,你用的话要改。

    
    
    点赞 评论
  • 1921634254 2015-05-17 07:17

    可以采取空布局的方式控制具体的位置:
    frame.setLayout(null);
    frame.add(button1);
    button1.setBounds(100,50,80,30);//修改控件位置和大小信息

    你可以试试。

    点赞 2 评论
  • 妙歌love 2015-05-18 07:23

    private JPanel getInfoPane() {

        JPanel infoPane = new JPanel();
        infoPane.setBorder(BorderFactory.createTitledBorder(""));
        infoPane.setLayout(new GridBagLayout());
        // row 1
        infoPane.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(nameTextField, new GridBagConstraints(1, 0, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(codeLabel, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(codeTextField, new GridBagConstraints(3, 0, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        // row 2
        infoPane.add(areaLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(areaComboBox, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(levelLabel, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(levelComboBox, new GridBagConstraints(3, 1, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        // row 3
        infoPane.add(latitudeLabel, new GridBagConstraints(0, 2, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(latitudeTextField, new GridBagConstraints(1, 2, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(longitudeLabel, new GridBagConstraints(2, 2, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(longitudeTextField, new GridBagConstraints(3, 2, 1, 1,
                0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        if (alarmStart) {
            // row 4
            infoPane.add(radarTypeLabel, new GridBagConstraints(0, 3, 1, 1,
                    0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS,
                    0, 0));
            infoPane.add(radarTypeField, new GridBagConstraints(1, 3, 1, 1,
                    0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS,
                    0, 0));
            infoPane.add(radarProLabel, new GridBagConstraints(2, 3, 1, 1, 0.0,
                    0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS,
                    0, 0));
            infoPane.add(radarProField, new GridBagConstraints(3, 3, 1, 1, 0.0,
                    0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS,
                    0, 0));
        }
        return infoPane;
    }
        定义相关的标签和文本框,运行后是这样的
        ![图片说明](https://img-ask.csdn.net/upload/201505/18/1431933753_265790.png)
    
    点赞 评论
  • 妙歌love 2015-05-18 07:23

    private JPanel getInfoPane() {

        JPanel infoPane = new JPanel();
        infoPane.setBorder(BorderFactory.createTitledBorder(""));
        infoPane.setLayout(new GridBagLayout());
        // row 1
        infoPane.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(nameTextField, new GridBagConstraints(1, 0, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(codeLabel, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(codeTextField, new GridBagConstraints(3, 0, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        // row 2
        infoPane.add(areaLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(areaComboBox, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(levelLabel, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(levelComboBox, new GridBagConstraints(3, 1, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        // row 3
        infoPane.add(latitudeLabel, new GridBagConstraints(0, 2, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(latitudeTextField, new GridBagConstraints(1, 2, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(longitudeLabel, new GridBagConstraints(2, 2, 1, 1, 0.0,
                0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        infoPane.add(longitudeTextField, new GridBagConstraints(3, 2, 1, 1,
                0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS, 0, 0));
        if (alarmStart) {
            // row 4
            infoPane.add(radarTypeLabel, new GridBagConstraints(0, 3, 1, 1,
                    0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS,
                    0, 0));
            infoPane.add(radarTypeField, new GridBagConstraints(1, 3, 1, 1,
                    0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS,
                    0, 0));
            infoPane.add(radarProLabel, new GridBagConstraints(2, 3, 1, 1, 0.0,
                    0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS,
                    0, 0));
            infoPane.add(radarProField, new GridBagConstraints(3, 3, 1, 1, 0.0,
                    0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, GuiUtil.DFLT_SPACE_INSETS,
                    0, 0));
        }
        return infoPane;
    }
        定义相关的标签和文本框,运行后是这样的
        ![图片说明](https://img-ask.csdn.net/upload/201505/18/1431933753_265790.png)
    
    点赞 评论

相关推荐 更多相似问题