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