Java中的JPanel的函数setBounds()的用法误解

本文详细阐述了在使用JPanel创建GUI界面时遇到的问题,特别是当尝试将子面板添加到主面板中后,部分按钮未正确显示的情况。通过深入分析布局管理器的工作原理,作者揭示了问题的根源在于按钮的位置设置相对子面板而非整个窗口。解决方案包括将按钮的位置设置改为基于主面板的坐标系,确保所有元素按照预期布局。本文不仅提供了解决该特定问题的方法,还加深了读者对Java GUI编程中布局管理的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       建立一个JPanel,1024*512后,把下面的CoraseButtonPanel和Joint1ButtonPanel添加进去,然后运行,发现Joint1ButtonPanel里的按钮不显示,调试发现setBounds的大小是相对于自己的Panel而言的,把Joint1ButtonPanel里的按钮的setBounds横坐标换成512以内的数就可以了。

 class CoraseButtonPanel extends JPanel{


     public CoraseButtonPanel(){




         this.setLayout(null);
         this.setBounds(0,0,512,512);
         this.setBorder( BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,1),
         "Coarse Adjustment",TitledBorder.DEFAULT_POSITION,TitledBorder.TOP,new java.awt.Font("Times New Roman",0,20)));


        CoraseStartButton=new JButton("Start");
         CoraseStartButton.setSize(80,30);
        CoraseStartButton.setBounds(180,460,80,30);
        CorasePreceedButton = new JButton("Preceed");
        CorasePreceedButton.setSize(100,30);
        CorasePreceedButton.setBounds(280,460,100,30);
        CoraseCancleButton = new JButton("Cancle");
        CoraseCancleButton .setSize(80,30);
        CoraseCancleButton.setBounds(400,460,80,30);


        CorasePreceedButton.setEnabled(false);
        CoraseCancleButton.setEnabled(false);
         CoraseStartButton.addActionListener(new CoraseStartAction());
         CorasePreceedButton.addActionListener(new CorasepreceedAction());
         CoraseCancleButton.addActionListener(new CorasecancleAction());


       add(CoraseStartButton);
       add(CorasePreceedButton);
       add(CoraseCancleButton);

     }

   }


  class Joint1ButtonPanel extends JPanel{




    public Joint1ButtonPanel(){


          this.setLayout(null);
        this.setBounds(512,0,512,512);
       this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,1),
      "Joint1 Adjustment",TitledBorder.DEFAULT_POSITION,TitledBorder.TOP,new java.awt.Font("Times New Roman",0,20)));

       Joint1BackButton=new JButton("Back");
       Joint1BackButton.setSize(80,30);
       Joint1BackButton.setBounds(700,460,80,30);


       Joint1PreceedButton = new JButton("Preceed");
       Joint1PreceedButton.setSize(100,30);
        Joint1PreceedButton.setBounds(800,460,100,30);


      Joint1CancleButton = new JButton("Cancle");
      Joint1CancleButton.setSize(80,30);
      Joint1CancleButton.setBounds(900,460,80,30);


      Joint1BackButton.setEnabled(false);
      Joint1PreceedButton.setEnabled(false);
      Joint1CancleButton .setEnabled(false);
     Joint1BackButton.addActionListener(new Joint1BackAction());
     Joint1PreceedButton.addActionListener(new Joint1PreceedAction());
     Joint1CancleButton.addActionListener(new Joint1CancleAction());


     add(Joint1BackButton);
          add(Joint1PreceedButton);
      add(Joint1CancleButton);
    }


  }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值