標題:
JPanel 類別與 setBorder() 方法
[打印本頁]
作者:
tonyh
時間:
2020-9-7 19:59
標題:
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();
}
}
複製代碼
作者:
黃宇瑄
時間:
2020-9-7 20:43
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 p1,p2,p3;
JButton b[]=new JButton[4];
Ch01()
{
p1=new JPanel();
p1.setBorder(new EtchedBorder());
p2=new JPanel();
p2.setBorder(new TitledBorder("第二區"));
p3=new JPanel();
p3.setLayout(new GridLayout(4, 1, 3, 3));
p3.setBorder(new TitledBorder(null, "第三區", TitledBorder.CENTER, TitledBorder.BOTTOM));
for(int i=0; i<4; i++)
{
b[i]=new JButton("按鈕"+(i+1));
p3.add(b[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(p1);
fm.add(p2);
fm.add(p3);
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
黃宇綸
時間:
2020-9-7 20:43
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();
}
}
複製代碼
作者:
戴嘉禾
時間:
2020-9-14 20:02
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();
}
}
複製代碼
作者:
鄭楀諺
時間:
2020-9-18 20:15
import java.awt.BorderLayout;
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 Ch130 {
JFrame fn;
JButton btn1, btn2, btn3, btn4;
JPanel pn1, pn2, pn3;
Ch130()
{
btn1 = new JButton("btn1");
btn2 = new JButton("btn2");
btn3 = new JButton("btn3");
btn4 = new JButton("btn4");
pn1 = new JPanel();
pn1.setBorder(new EtchedBorder());
pn2 = new JPanel();
pn2.setBorder(new TitledBorder("Part 2"));
pn3 = new JPanel();
pn3.setLayout(new GridLayout(4, 1, 3, 3));
pn3.setBorder(new TitledBorder(null, "Part3", TitledBorder.CENTER, TitledBorder.BOTTOM));
pn3.add(btn1);
pn3.add(btn2);
pn3.add(btn3);
pn3.add(btn4);
fn = new JFrame("Grid");
fn.setBounds(100, 100, 300, 200);
fn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fn.setVisible(true);
fn.setLayout(new GridLayout());
fn.add(pn1);
fn.add(pn2);
fn.add(pn3);
}
public static void main(String[] args) {
new Ch130();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2