欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java Swing組件setBounds()簡單用法實(shí)例分析

 更新時(shí)間:2017年11月13日 11:52:28   作者:pzy4447  
這篇文章主要介紹了java Swing組件setBounds()簡單用法,結(jié)合實(shí)例形式分析了Swing組件setBounds()方法的功能與簡單使用方法,需要的朋友可以參考下

本文實(shí)例講述了java Swing組件setBounds()簡單用法。分享給大家供大家參考,具體如下:

先看API:

public void setBounds(Rectangle r)

移動(dòng)組件并調(diào)整其大小,使其符合新的有界矩形 r。由 r.x 和 r.y 指定組件的新位置,由 r.width 和 r.height 指定組件的新大小

參數(shù): r - 此組件的新的有界矩形

從API來看,該方法的作用相當(dāng)于setLocation()setSize()的總和。在實(shí)際使用時(shí),需將容器的layout設(shè)置為null,因?yàn)槭褂貌季止芾砥鲿r(shí),控件的位置與尺寸是由布局管理器來分配的。需要注意的是,這時(shí)必須手動(dòng)指定容器的尺寸,因?yàn)榭盏牟季止芾砥鲿?huì)將容器自身的PreferredSize清零,導(dǎo)致容器無法在GUI上顯示。因此,如果容器在上級(jí)容器中使用布局管理器排列,那么需使用setPreferredSize(),如果容器在上級(jí)容器中仍然手動(dòng)排列,那么對(duì)容器使用setBounds()即可。

下面是測試demo:

package awtDemo;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class setBoundsDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    //設(shè)置panel的layout以及sieze
    JPanel jpanel = new JPanel();
    System.out.println("default PreferredSize is " + jpanel.getPreferredSize());
    System.out.println("default Size is " + jpanel.getSize());
    jpanel.setLayout(null);
    System.out.println("In null layout, the PreferredSize is " + jpanel.getPreferredSize());
    System.out.println("In null layout, the Size is " + jpanel.getSize());
    jpanel.setPreferredSize(new Dimension(400, 400));
    //添加按鈕
    JButton button11 = new JButton("setBounds");
    JButton button12 = new JButton("setLocationAndSetSize");
    button11.setBounds(20, 20, 100, 100);
    button12.setLocation(250, 250);
    button12.setSize(100, 100);
    jpanel.add(button11);
    jpanel.add(button12);
    // 設(shè)置窗體屬性
    JFrame frame = new JFrame("setBoundsDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(jpanel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

運(yùn)行效果如下:

程序輸出如下:

default PreferredSize is java.awt.Dimension[width=10,height=10]
default Size is java.awt.Dimension[width=0,height=0]
In null layout, the PreferredSize is java.awt.Dimension[width=0,height=0]
In null layout, the Size is java.awt.Dimension[width=0,height=0]

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論