JPanel 類別與 setBorder() 方法
本帖最後由 tonyh 於 2020-9-7 20:30 編輯
試使用 setBorder() 方法,為容器加上邊框。
- import java.awt.GridLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EtchedBorder;
- import javax.swing.border.TitledBorder;
- public class Ch01 {
- JFrame fm;
- JPanel pn1,pn2,pn3;
- JButton btn[]=new JButton[4];
- Ch01()
- {
- pn1=new JPanel();
- pn1.setBorder(new EtchedBorder());
- pn2=new JPanel();
- pn2.setBorder(new TitledBorder("第二區"));
- pn3=new JPanel();
- pn3.setLayout(new GridLayout(4, 1, 3, 3));
- pn3.setBorder(new TitledBorder(null, "第三區", TitledBorder.CENTER, TitledBorder.BOTTOM));
- for(int i=0; i<4; i++)
- {
- btn[i]=new JButton("按鈕"+(i+1));
- pn3.add(btn[i]);
- }
- fm=new JFrame("GridLayout");
- fm.setBounds(100, 100, 580, 435);
- fm.setVisible(true);
- fm.setLayout(new GridLayout());
- fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm.add(pn1);
- fm.add(pn2);
- fm.add(pn3);
- }
- public static void main(String[] args) {
- new Ch01();
- }
- }
複製代碼 |