標題:
ImageIcon 類別 (五)
[打印本頁]
作者:
周政輝
時間:
2018-2-10 11:23
標題:
ImageIcon 類別 (五)
試以陣列方式布置大量元件.
[attach]3395[/attach]
[attach]3396[/attach]
作者:
黃茂勛
時間:
2018-2-10 12:01
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class Main extends JFrame {
private JPanel contentPane;
private ImageIcon icon_1[] = new ImageIcon[6];
private JLabel lb[] = new JLabel[6];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
ImageIcon icon = new ImageIcon(Main.class.getResource("icon.png"));
Image img = icon.getImage().getScaledInstance(45, 45, java.awt.Image.SCALE_FAST);
frame.setIconImage(img);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setTitle("\u4EE5\u9663\u5217\u7684\u65B9\u5F0F\u5E03\u7F6E\u5927\u91CF\u5143\u4EF6");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 487, 346);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
for(int i=0; i<6; i++)
icon_1[i] = new ImageIcon(Main.class.getResource(String.format("%02d.png", i+1)));
JLabel lblNewLabel = new JLabel();
lblNewLabel.setBounds(40, 40, 107, 101);
lblNewLabel.setIcon(icon_1[0]);
contentPane.add(lblNewLabel);
JLabel label = new JLabel();
label.setBounds(40, 167, 107, 101);
label.setIcon(icon_1[1]);
contentPane.add(label);
JLabel label_1 = new JLabel();
label_1.setBounds(164, 40, 107, 101);
label_1.setIcon(icon_1[2]);
contentPane.add(label_1);
JLabel label_2 = new JLabel();
label_2.setBounds(312, 40, 107, 101);
label_2.setIcon(icon_1[3]);
contentPane.add(label_2);
JLabel label_3 = new JLabel();
label_3.setBounds(164, 167, 107, 101);
label_3.setIcon(icon_1[4]);
contentPane.add(label_3);
JLabel label_4 = new JLabel();
label_4.setBounds(312, 167, 107, 101);
label_4.setIcon(icon_1[5]);
contentPane.add(label_4);
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-2-10 12:07
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class Test extends JFrame {
private JPanel contentPane;
private ImageIcon icon,picon;
private ImageIcon[] pic,newpic;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
icon = new ImageIcon(Test.class.getResource("icon.png"));
Image img = icon.getImage().getScaledInstance(376,208,Image.SCALE_FAST);
setIconImage(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label_0 = new JLabel("New label");
label_0.setBounds(10, 10, 132, 108);
contentPane.add(label_0);
JLabel label_1 = new JLabel("New label");
label_1.setBounds(10, 128, 132, 108);
contentPane.add(label_1);
JLabel label_2 = new JLabel("New label");
label_2.setBounds(152, 10, 132, 108);
contentPane.add(label_2);
JLabel label_3 = new JLabel("New label");
label_3.setBounds(152, 128, 132, 108);
contentPane.add(label_3);
JLabel label_4 = new JLabel("New label");
label_4.setBounds(302, 10, 132, 108);
contentPane.add(label_4);
JLabel label_5 = new JLabel("New label");
label_5.setBounds(302, 128, 132, 108);
contentPane.add(label_5);
pic = new ImageIcon[6];
newpic = new ImageIcon[6];
for(int i = 0 ; i < 6 ; i++){
pic[i] = new ImageIcon(Test.class.getResource("0"+String.valueOf(i+1)+"png"));
}
for(int i = 0 ; i < 6 ; i++){
Image pimg = pic[i].getImage().getScaledInstance(132,108,Image.SCALE_FAST);
newpic[i] = new ImageIcon(pimg);
}
}
}
複製代碼
作者:
張健勳
時間:
2018-2-24 00:01
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class Main extends JFrame {
private JPanel contentPane;
private ImageIcon Iicon[] = new ImageIcon[6];
private JLabel lb[] = new JLabel[6];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
ImageIcon icon = new ImageIcon(Main.class.getResource("icon.png"));
Image img = icon.getImage().getScaledInstance(45, 45, java.awt.Image.SCALE_FAST);
frame.setIconImage(img);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setTitle("\u4EE5\u9663\u5217\u7684\u65B9\u5F0F\u5E03\u7F6E\u5927\u91CF\u5143\u4EF6");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 487, 346);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
for(int i=0; i<6; i++)
Iicon[i] = new ImageIcon(Main.class.getResource(String.format("%02d.png", i+1)));
JLabel lb1 = new JLabel();
lb1.setBounds(40, 40, 107, 101);
lb1.setIcon(Iicon[0]);
contentPane.add(lb1);
JLabel lb2 = new JLabel();
lb2.setBounds(40, 167, 107, 101);
lb2.setIcon(Iicon[1]);
contentPane.add(lb2);
JLabel lb3 = new JLabel();
lb3.setBounds(164, 40, 107, 101);
lb3.setIcon(Iicon[2]);
contentPane.add(lb3);
JLabel lb4 = new JLabel();
lb4.setBounds(312, 40, 107, 101);
lb4.setIcon(Iicon[3]);
contentPane.add(lb4);
JLabel lb5 = new JLabel();
lb5.setBounds(164, 167, 107, 101);
lb5.setIcon(Iicon[4]);
contentPane.add(lb5);
JLabel lb6 = new JLabel();
lb6.setBounds(312, 167, 107, 101);
lb6.setIcon(Iicon[5]);
contentPane.add(lb6);
Iicon = new ImageIcon[6];
Iicon = new ImageIcon[6];
for(int i = 0 ; i < 6 ; i++){
Iicon[i] = new ImageIcon(Main.class.getResource("0"+String.valueOf(i+1)+"png"));
}
for(int i = 0 ; i < 6 ; i++){
Image pimg = Iicon[i].getImage().getScaledInstance(132,108,Image.SCALE_FAST);
Iicon[i] = new ImageIcon(pimg);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2