tailieunhanh - Java All-in-One Desk Reference For Dummies phần 8

Để tạo một đối tượng GridBagConstraint, bạn gọi các nhà xây dựng GridBagConstraint, và sau đó thiết lập bất kỳ các lĩnh vực mà bạn muốn thay đổi từ giá trị mặc định. Ví dụ, đây là mã tạo ra một đối tượng GridBagConstraint để thêm tên trường văn bản được thể hiện trước đó trong Hình 5-5: | Using GridBag Layout 597 You also want to set the anchor field to indicate where you want the component placed if it doesn t fill the cell or cells allotted to it. Working with GridBagConstraints To create a GridBagConstraint object you call the GridBagConstraint constructor and then set any of the fields that you want to vary from the default values. For example here s code that creates a GridBagConstraint object to add the name text field that is shown earlier in Figure 5-5 GridBagConstraints nameconstraints new GridBagConstraints 1 0 2 1 new Insets 5 5 5 5 Then you can call the add method to add the name text field to the panel name nameConstraints Obviously this approach to controlling constraints is going to require a lot of coding. You have two common alternatives to creating a new constraint object for every component you add to the panel. The first is to create a single constraint object and reuse it for all the components in the panel. Then you simply change the fields that need to be changed for each component. For example here s code that adds all three text fields using a single constraint object GridBagConstraints gc new GridBagConstraints 0 0 1 1 new Insets 5 5 5 5 0 2 Book VI Chapter 5 Using Layout Managers 598 Using GridBag Layout add name gc 1 1 add phone gc 2 2 add address gc Here the first group of statements creates a GridBagConstraints object named gc and sets its values to the defaults that I want to apply to most of the components in the panel. .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.