標題:
JLayeredPane 類別
[打印本頁]
作者:
tonyh
時間:
2020-8-24 19:22
標題:
JLayeredPane 類別
在視窗程式設計中,JFrame、JPanel、JLayeredPane都是所謂的容器。當視窗裡的元件需依照指定的順序前後交疊,我們可利用 JLayeredPane 容器來指定圖層的順序,透過該類別下的 add(Component comp, Object constraints) 方法,先將元件加入 JLayeredPane 容器,再加入 JFrame 容器。
在本單元練習中,視窗中有六個元件:房間、沙發、窗戶、掛鐘、嬰兒、小丸子。試將這六個元件放入 JLayeredPane 容器並指定圖層 1 到 6,使其可前後交疊,如範例輸出畫面。
各元件的設定如下:
JFrame:100, 100, 580, 435
JLayeredPane:0, 0, 576, 407
房間:0, 0, 576, 407
沙發:150, 238, 410, 193
窗戶:30, 20, 317, 210
掛鐘:420, 15, 106, 106
嬰兒:355, 220, 101, 141
小丸子:-5, 125, 199, 335
素材: pic.zip
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.ImageIcon;
public class Ch105 {
private JFrame fm;
private JLayeredPane lp;
private JLabel lb_baby,lb_clock,lb_girl,lb_room,lb_sofa,lb_window;
Ch105()
{
lb_baby=new JLabel(new ImageIcon(Ch105.class.getResource("pic/baby.png")));
lb_clock=new JLabel(new ImageIcon(Ch105.class.getResource("pic/clock.png")));
lb_girl=new JLabel(new ImageIcon(Ch105.class.getResource("pic/girl.png")));
lb_room=new JLabel(new ImageIcon(Ch105.class.getResource("pic/room.jpg")));
lb_sofa=new JLabel(new ImageIcon(Ch105.class.getResource("pic/sofa.png")));
lb_window=new JLabel(new ImageIcon(Ch105.class.getResource("pic/window.jpg")));
lb_baby.setBounds(355, 220, 101, 141);
lb_clock.setBounds(420, 15, 106, 106);
lb_girl.setBounds(-5, 125, 199, 335);
lb_room.setBounds(0, 0, 576, 407);
lb_sofa.setBounds(150, 238, 410, 193);
lb_window.setBounds(30, 20, 317, 210);
lp=new JLayeredPane();
lp.setBounds(0, 0, 576, 407);
lp.add(lb_room, new Integer(1));
lp.add(lb_sofa, new Integer(2));
lp.add(lb_window, new Integer(3));
lp.add(lb_clock, new Integer(4));
lp.add(lb_baby, new Integer(5));
lp.add(lb_girl, new Integer(6));
fm=new JFrame("JLayeredPane 練習");
fm.setBounds(100, 100, 580, 435);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lp);
}
public static void main(String[] args) {
new Ch105();
}
}
複製代碼
作者:
黃宇綸
時間:
2020-8-24 19:50
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.ImageIcon;
public class Ch01 {
private JFrame fm;
private JLayeredPane lp;
private JLabel b,c,g,r,s,w;
Ch01()
{
b=new JLabel(new ImageIcon(Ch01.class.getResource("pic/baby.png")));
c=new JLabel(new ImageIcon(Ch01.class.getResource("pic/clock.png")));
g=new JLabel(new ImageIcon(Ch01.class.getResource("pic/girl.png")));
r=new JLabel(new ImageIcon(Ch01.class.getResource("pic/room.jpg")));
s=new JLabel(new ImageIcon(Ch01.class.getResource("pic/sofa.png")));
w=new JLabel(new ImageIcon(Ch01.class.getResource("pic/window.jpg")));
b.setBounds(355, 220, 101, 141);
c.setBounds(420, 15, 106, 106);
g.setBounds(-5, 125, 199, 335);
r.setBounds(0, 0, 576, 407);
s.setBounds(150, 238, 410, 193);
w.setBounds(30, 20, 317, 210);
lp=new JLayeredPane();
lp.setBounds(0, 0, 576, 407);
lp.add(r, new Integer(1));
lp.add(s, new Integer(2));
lp.add(w, new Integer(3));
lp.add(c, new Integer(4));
lp.add(b, new Integer(5));
lp.add(g, new Integer(6));
fm=new JFrame("JLayeredPane 練習");
fm.setBounds(100, 100, 580, 435);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lp);
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
黃宇瑄
時間:
2020-8-24 20:00
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.ImageIcon;
public class Ch01 {
JFrame fm;
JLayeredPane lp;
JLabel b,c,g,r,s,w;
Ch01()
{
b=new JLabel(new ImageIcon(Ch01.class.getResource("pic/baby.png")));
c=new JLabel(new ImageIcon(Ch01.class.getResource("pic/clock.png")));
g=new JLabel(new ImageIcon(Ch01.class.getResource("pic/girl.png")));
r=new JLabel(new ImageIcon(Ch01.class.getResource("pic/room.jpg")));
s=new JLabel(new ImageIcon(Ch01.class.getResource("pic/sofa.png")));
w=new JLabel(new ImageIcon(Ch01.class.getResource("pic/window.jpg")));
b.setBounds(355, 220, 101, 141);
c.setBounds(420, 15, 106, 106);
g.setBounds(-5, 125, 199, 335);
r.setBounds(0, 0, 576, 407);
s.setBounds(150, 238, 410, 193);
w.setBounds(30, 20, 317, 210);
lp=new JLayeredPane();
lp.setBounds(0, 0, 576, 407);
lp.add(r, new Integer(1));
lp.add(s, new Integer(2));
lp.add(w, new Integer(3));
lp.add(c, new Integer(4));
lp.add(b, new Integer(5));
lp.add(g, new Integer(6));
fm=new JFrame("JLayeredPane 練習");
fm.setBounds(100, 100, 580, 435);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lp);
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
李沛昂
時間:
2020-8-24 20:01
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
public class Ch00 {
JFrame fm;
JLayeredPane lp;
JLabel lb_baby,lb_clock,lb_girl,lb_room,lb_sofa,lb_window;
Ch00()
{
lb_baby=new JLabel(new ImageIcon(Ch00.class.getResource("pic/baby.png")));
lb_clock=new JLabel(new ImageIcon(Ch00.class.getResource("pic/clock.png")));
lb_girl=new JLabel(new ImageIcon(Ch00.class.getResource("pic/girl.png")));
lb_room=new JLabel(new ImageIcon(Ch00.class.getResource("pic/room.jpg")));
lb_sofa=new JLabel(new ImageIcon(Ch00.class.getResource("pic/sofa.png")));
lb_window=new JLabel(new ImageIcon(Ch00.class.getResource("pic/window.jpg")));
lp=new JLayeredPane();
lp.setBounds(0, 0, 576, 407);
lp.add(lb_room, new Integer(1));
lp.add(lb_sofa, new Integer(2));
lp.add(lb_window, new Integer(3));
lp.add(lb_clock, new Integer(4));
lp.add(lb_baby, new Integer(5));
lp.add(lb_girl, new Integer(6));
fm=new JFrame("不知道是什麼名字");
fm.setBounds(100, 100, 580, 435);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lp);
}
public static void main(String[] args) {
new Ch00();
}
}
複製代碼
作者:
戴嘉禾
時間:
2020-8-24 20:05
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.ImageIcon;
public class Ch01 {
private JFrame fm;
private JLayeredPane lp;
private JLabel lb_baby,lb_clock,lb_girl,lb_room,lb_sofa,lb_window;
Ch01()
{
lb_baby=new JLabel(new ImageIcon(Ch01.class.getResource("pic/baby.png")));
lb_clock=new JLabel(new ImageIcon(Ch01.class.getResource("pic/clock.png")));
lb_girl=new JLabel(new ImageIcon(Ch01.class.getResource("pic/girl.png")));
lb_room=new JLabel(new ImageIcon(Ch01.class.getResource("pic/room.jpg")));
lb_sofa=new JLabel(new ImageIcon(Ch01.class.getResource("pic/sofa.png")));
lb_window=new JLabel(new ImageIcon(Ch01.class.getResource("pic/window.jpg")));
lb_baby.setBounds(355, 220, 101, 141);
lb_clock.setBounds(420, 15, 106, 106);
lb_girl.setBounds(-5, 125, 199, 335);
lb_room.setBounds(0, 0, 576, 407);
lb_sofa.setBounds(150, 238, 410, 193);
lb_window.setBounds(30, 20, 317, 210);
lp=new JLayeredPane();
lp.setBounds(0, 0, 576, 407);
lp.add(lb_room, new Integer(1));
lp.add(lb_sofa, new Integer(2));
lp.add(lb_window, new Integer(3));
lp.add(lb_clock, new Integer(4));
lp.add(lb_baby, new Integer(5));
lp.add(lb_girl, new Integer(6));
fm=new JFrame("jlayeredpane");
fm.setBounds(100, 100, 580, 435);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lp);
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
鄭楀諺
時間:
2020-8-27 19:57
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
public class Momo {
JFrame fm;
JLabel girl, clock, baby, room, window, sofa;
JLayeredPane jl;
Momo()
{
girl = new JLabel(new ImageIcon(Momo.class.getResource("pic/girl.png")));
baby = new JLabel(new ImageIcon(Momo.class.getResource("pic/baby.png")));
room = new JLabel(new ImageIcon(Momo.class.getResource("pic/room.jpg")));
window = new JLabel(new ImageIcon(Momo.class.getResource("pic/window.jpg")));
sofa = new JLabel(new ImageIcon(Momo.class.getResource("pic/sofa.png")));
clock = new JLabel(new ImageIcon(Momo.class.getResource("pic/clock.png")));
girl.setBounds(-5, 125, 199, 335);
baby.setBounds(355, 220, 101, 141);
room.setBounds(0, 0, 576, 407);
window.setBounds(30, 20, 317, 210);
sofa.setBounds(150, 238, 410, 193);
clock.setBounds(420, 15, 106, 106);
jl = new JLayeredPane();
jl.setBounds(0, 0, 576, 407);
jl.add(room, new Integer(1));
jl.add(window, new Integer(2));
jl.add(clock, new Integer(3));
jl.add(sofa, new Integer(4));
jl.add(baby, new Integer(5));
jl.add(girl, new Integer(6));
fm = new JFrame("JPanel");
fm.setBounds(100, 100, 580, 435);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(jl);
}
public static void main(String[] args){
new Momo();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2