標題:
ImageIcon 類別 (三)
[打印本頁]
作者:
周政輝
時間:
2018-2-3 11:30
標題:
ImageIcon 類別 (三)
利用 JFrame 類別下的 setIconImage() 方法, 搭配 ImageIcon 類別下的 getImage() 方法, 設定視窗的小圖示.
小圖示可直接在 google 上搜尋取得, 或到提供大量免費圖片素材之Icon Archive網站取得.
[attach]3379[/attach]
作者:
張健勳
時間:
2018-2-3 11:34
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class Main extends JFrame {
private JPanel contentPane;
/**
* 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() {
setTitle("ImageIcon");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 314, 396);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Welcome");
lblNewLabel.setLabelFor(this);
lblNewLabel.setBounds(-1, 0, 296, 276);
contentPane.add(lblNewLabel);
JButton btn2 = new JButton("\u7761\u89BA");
btn2.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/parrot.png")));
btn2.setBounds(142, 274, 153, 39);
contentPane.add(btn2);
JButton btn4 = new JButton("\u554F\u865F");
btn4.setIcon(new ImageIcon("C:\\Users\\15LiveTW\\Downloads\\sleep.jfif"));
btn4.setBounds(142, 310, 153, 40);
contentPane.add(btn4);
JButton btn3 = new JButton("\u9E1A\u9D61");
btn3.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/qm.jpg")));
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btn3.setBounds(0, 310, 145, 40);
contentPane.add(btn3);
JButton btn1 = new JButton("\u8C93\u54AA");
btn1.setIcon(new ImageIcon("C:\\Users\\15LiveTW\\Downloads\\meow.jfif"));
btn1.setBounds(-1, 274, 145, 38);
contentPane.add(btn1);
JButton icon = new JButton("\u8C93\u54AA");
icon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
lblNewLabel.setIcon(new ImageIcon("meow.jifi"));
}
if(e.getSource()==btn2)
{
lblNewLabel.setIcon(new ImageIcon("parrot.png"));
}
if(e.getSource()==btn4)
{
lblNewLabel.setIcon(new ImageIcon("qm.jpg"));
}
if(e.getSource()==btn3)
{
lblNewLabel.setIcon(new ImageIcon("sleep.jifi"));
}
}
});
}
}
複製代碼
作者:
蔡季樺
時間:
2018-2-3 12:05
本帖最後由 蔡季樺 於 2018-2-9 21:42 編輯
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Window;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
public class Main {
private JFrame frame;
private JFrame frame_1;
private ImageIcon icon;
private String newIcon;
private JPanel contentPane;
private JLabel label;
private ImageIcon icon2;
private ImageIcon icon3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame_1.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame_1 = new JFrame();
frame_1.setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/bbs/istak/org/tw/earth-icon.png")));
frame_1.getContentPane().setLayout(null);
label = new JLabel();
label.setBounds(0, 0, 434, 239);
frame_1.getContentPane().add(label);
JButton btnNewButton = new JButton("");
btnNewButton.setIcon(main2("Koala.jpg"));
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
main("Koala.jpg");
}
});
btnNewButton.setBounds(0, 239, 115, 23);
frame_1.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("");
btnNewButton_1.setIcon(main2("Desert.jpg"));
btnNewButton_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
main("Desert.jpg");
}
});
btnNewButton_1.setBounds(116, 239, 107, 23);
frame_1.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("");
btnNewButton_2.setIcon(main2("Penguins.jpg"));
btnNewButton_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
main("Penguins.jpg");
}
});
btnNewButton_2.setBounds(222, 239, 115, 23);
frame_1.getContentPane().add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("");
btnNewButton_3.setIcon(main2("Lighthouse.jpg"));
btnNewButton_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
main("Lighthouse.jpg");
}
});
btnNewButton_3.setBounds(332, 239, 102, 23);
frame_1.getContentPane().add(btnNewButton_3);
}
public void main(String i)
{
icon = new ImageIcon(Main.class.getResource(i));
Image img = icon.getImage().getScaledInstance(399, 300, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
label.setIcon(newIcon);
}
public ImageIcon main2(String s)
{
icon2 = new ImageIcon(Main.class.getResource(s));
Image imgsm = icon2.getImage().getScaledInstance(45,25,Image.SCALE_FAST);
ImageIcon newicon2 = new ImageIcon(imgsm);
return newicon2;
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-2-3 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.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Test extends JFrame {
private JPanel contentPane;
private static ImageIcon icon;
private static ImageIcon iconsm;
private static JLabel lblNewLabel;
private static ImageIcon ICON1;
/**
* 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() {
ICON1 = new ImageIcon(Main.class.getResource("icon4.png"));
Image img1 = ICON1.getImage().getScaledInstance(376,208,Image.SCALE_FAST);
setIconImage(img1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 412, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblNewLabel = new JLabel();
lblNewLabel.setBounds(10, 10, 376, 208);
contentPane.add(lblNewLabel);
JButton btnNewButton = new JButton();
btnNewButton.setIcon(ciconsm("image3.jpg"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cicon("image3.jpg");
}
});
btnNewButton.setBounds(10, 229, 87, 30);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton();
btnNewButton_1.setIcon(ciconsm("image0.jpg"));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cicon("image0.jpg");
}
});
btnNewButton_1.setBounds(107, 229, 87, 30);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton();
btnNewButton_2.setIcon(ciconsm("image2.jpg"));
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cicon("image2.jpg");
}
});
btnNewButton_2.setBounds(201, 229, 87, 30);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton();
btnNewButton_3.setIcon(ciconsm("image1.jpg"));
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cicon("image1.jpg");
}
});
btnNewButton_3.setBounds(298, 229, 87, 30);
contentPane.add(btnNewButton_3);
}
public static void cicon (String str){
icon = new ImageIcon(Main.class.getResource(str));
Image img = icon.getImage().getScaledInstance(376,208,Image.SCALE_FAST);
ImageIcon newicon = new ImageIcon(img);
lblNewLabel.setIcon(newicon);
}
public static ImageIcon ciconsm (String str){
iconsm = new ImageIcon(Main.class.getResource(str));
Image imgsm = iconsm.getImage().getScaledInstance(47,26,Image.SCALE_FAST);
ImageIcon newiconsm = new ImageIcon(imgsm);
return newiconsm;
}
}
複製代碼
作者:
黃茂勛
時間:
2018-2-3 12:10
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main extends JFrame {
private JPanel contentPane;
private ImageIcon icon;
private ImageIcon img_fr;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
ImageIcon icon_btn = new ImageIcon(Main.class.getResource("earth.png"));
Image img_btn = icon_btn.getImage().getScaledInstance(45, 45, java.awt.Image.SCALE_FAST);
frame.setIconImage(img_btn);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
JLabel lblNewLabel;
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 444, 372);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblNewLabel = new JLabel();
lblNewLabel.setBounds(0, 0, 429, 293);
contentPane.add(lblNewLabel);
Changeicon("Koala.jpg");
JButton btnNewButton = new JButton(Btnimage("Koala.jpg"));
btnNewButton.setBounds(0, 293, 112, 41);
contentPane.add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Changeicon("Koala.jpg");
}
});
JButton btnNewButton_1 = new JButton(Btnimage("Chrysanthemum.jpg"));
btnNewButton_1.setBounds(110, 293, 112, 41);
contentPane.add(btnNewButton_1);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Changeicon("Chrysanthemum.jpg");
}
});
JButton btnNewButton_2 = new JButton(Btnimage("Desert.jpg"));
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Changeicon("Desert.jpg");
}
});
btnNewButton_2.setBounds(221, 293, 112, 41);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton(Btnimage("Lighthouse.jpg"));
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Changeicon("Lighthouse.jpg");
}
});
btnNewButton_3.setBounds(329, 293, 100, 41);
contentPane.add(btnNewButton_3);
}
public void Changeicon(String str)
{
icon = new ImageIcon(Main.class.getResource(str));
Image img = icon.getImage().getScaledInstance(429, 293, java.awt.Image.SCALE_FAST);
ImageIcon newicon = new ImageIcon(img);
lblNewLabel.setIcon(newicon);
}
public ImageIcon Btnimage(String str)
{
ImageIcon icon_btn = new ImageIcon(Main.class.getResource(str));
Image img_btn = icon_btn.getImage().getScaledInstance(45, 45, java.awt.Image.SCALE_FAST);
ImageIcon newicon_btn = new ImageIcon(img_btn);
return newicon_btn;
}
}
複製代碼
作者:
林侑成
時間:
2018-2-10 10:08
package asdf;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Asdf extends JFrame {
private JPanel contentPane;
private ImageIcon icon;
private ImageIcon img_fr;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Asdf frame = new Asdf();
frame.setVisible(true);
ImageIcon icon_btn = new ImageIcon(Asdf.class.getResource("earth.png"));
Image img_btn = icon_btn.getImage().getScaledInstance(45, 45, java.awt.Image.SCALE_FAST);
frame.setIconImage(img_btn);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
JLabel lblNewLabel;
public Asdf() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 444, 372);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblNewLabel = new JLabel();
lblNewLabel.setBounds(0, 0, 429, 293);
contentPane.add(lblNewLabel);
Changeicon("Koala.jpg");
JButton btnNewButton = new JButton(Btnimage("Koala.jpg"));
btnNewButton.setBounds(0, 293, 112, 41);
contentPane.add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Changeicon("Koala.jpg");
}
});
JButton btnNewButton_1 = new JButton(Btnimage("Chrysanthemum.jpg"));
btnNewButton_1.setBounds(110, 293, 112, 41);
contentPane.add(btnNewButton_1);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Changeicon("Chrysanthemum.jpg");
}
});
JButton btnNewButton_2 = new JButton(Btnimage("Desert.jpg"));
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Changeicon("Desert.jpg");
}
});
btnNewButton_2.setBounds(221, 293, 112, 41);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton(Btnimage("Lighthouse.jpg"));
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Changeicon("Lighthouse.jpg");
}
});
btnNewButton_3.setBounds(329, 293, 100, 41);
contentPane.add(btnNewButton_3);
}
public void Changeicon(String str)
{
icon = new ImageIcon(Asdf.class.getResource(str));
Image img = icon.getImage().getScaledInstance(429, 293, java.awt.Image.SCALE_FAST);
ImageIcon newicon = new ImageIcon(img);
lblNewLabel.setIcon(newicon);
}
public ImageIcon Btnimage(String str)
{
ImageIcon icon_btn = new ImageIcon(Asdf.class.getResource(str));
Image img_btn = icon_btn.getImage().getScaledInstance(45, 45, java.awt.Image.SCALE_FAST);
ImageIcon newicon_btn = new ImageIcon(img_btn);
return newicon_btn;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2