標題:
ImageIcon 類別 (一)
[打印本頁]
作者:
周政輝
時間:
2018-1-27 10:12
標題:
ImageIcon 類別 (一)
本帖最後由 周政輝 於 2018-1-27 11:37 編輯
試利用 javax.swing 套件下的 ImageIcon 類別, 在視窗中置入圖片.
[設計導引]
1. 視窗的 左、右、下方 的邊框佔 3 像素 ; 上方的標題列佔 25 像素
( setResizable() 方法的參數設為 false 所展現的數據 )
2. 圖片大小 399x300 ; 標籤大小 399x300 ; 視窗大小 405x328
[attach]3256[/attach]
package kuas.mis;
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;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
icon = new ImageIcon(Main.class.getResource("Koala.jpg"));
Image img = icon.getImage().getScaledInstance(399, 300, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 477, 393);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel(newIcon);
lblNewLabel.setBounds(0, 0, 300, 300);
contentPane.add(lblNewLabel, BorderLayout.NORTH);
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-1-27 12:08
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;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
icon = new ImageIcon(Main.class.getResource("Hydrangeas.jpg"));
Image img =icon.getImage().getScaledInstance(399, 300, Image.SCALE_FAST);
ImageIcon newicon = new ImageIcon(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel(newicon);
lblNewLabel.setBounds(0,0,300,399);
contentPane.add(lblNewLabel, BorderLayout.CENTER);
}
}
複製代碼
作者:
黃茂勛
時間:
2018-1-27 14:09
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;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
icon = new ImageIcon(Main.class.getResource("Koala.jpg"));
Image img = icon.getImage().getScaledInstance(399, 300, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 477, 393);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel(newIcon);
lblNewLabel.setBounds(0, 0, 300, 300);
contentPane.add(lblNewLabel, BorderLayout.NORTH);
}
}
複製代碼
作者:
林侑成
時間:
2018-2-2 00:01
package asdf;
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 asdf extends JFrame {
private JPanel contentPane;
private ImageIcon icon;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
asdf frame = new asdf();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public asdf() {
icon = new ImageIcon(asdf.class.getResource("Koala.jpg"));
Image img = icon.getImage().getScaledInstance(399, 300, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 477, 393);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel(newIcon);
lblNewLabel.setBounds(0, 0, 300, 300);
contentPane.add(lblNewLabel, BorderLayout.NORTH);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2