標題:
[隨堂測驗]ImageIcon 類別 (二)
[打印本頁]
作者:
周政輝
時間:
2018-2-3 10:22
標題:
[隨堂測驗]ImageIcon 類別 (二)
[attach]3376[/attach]
[attach]3377[/attach]
[attach]3378[/attach]
作者:
蔡季樺
時間:
2018-2-3 10:47
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Window;
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 javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
public class Main {
private JFrame frame;
private ImageIcon icon;
private String newIcon;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.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.getContentPane().setLayout(null);
JLabel label = new JLabel();
label.setBounds(0, 0, 434, 239);
frame.getContentPane().add(label);
JButton btnNewButton = new JButton("無尾熊");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
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);
label.setIcon(newIcon);
}
});
btnNewButton.setBounds(0, 239, 115, 23);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("沙漠");
btnNewButton_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
icon = new ImageIcon(Main.class.getResource("Desert.jpg"));
Image img = icon.getImage().getScaledInstance(399, 300, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
label.setIcon(newIcon);
}
});
btnNewButton_1.setBounds(116, 239, 107, 23);
frame.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("企鵝");
btnNewButton_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
icon = new ImageIcon(Main.class.getResource("penguins.jpg"));
Image img = icon.getImage().getScaledInstance(399, 300, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
label.setIcon(newIcon);
}
});
btnNewButton_2.setBounds(222, 239, 115, 23);
frame.getContentPane().add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("燈塔");
btnNewButton_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
icon = new ImageIcon(Main.class.getResource("Lighthouse.jpg"));
Image img = icon.getImage().getScaledInstance(399, 300, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
label.setIcon(newIcon);
}
});
btnNewButton_3.setBounds(332, 239, 102, 23);
frame.getContentPane().add(btnNewButton_3);
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-2-3 10:48
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 ImageIcon icon0;
private ImageIcon icon1;
private ImageIcon icon2;
private ImageIcon icon3;
/**
* 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() {
icon3 = new ImageIcon(Main.class.getResource("image3.jpg"));
Image img3 = icon3.getImage().getScaledInstance(376,208,Image.SCALE_FAST);
ImageIcon newicon3 = new ImageIcon(img3);
icon0 = new ImageIcon(Main.class.getResource("image0.jpg"));
Image img0 = icon0.getImage().getScaledInstance(376,208,Image.SCALE_FAST);
ImageIcon newicon0 = new ImageIcon(img0);
icon2 = new ImageIcon(Main.class.getResource("image2.jpg"));
Image img2 = icon2.getImage().getScaledInstance(376,208,Image.SCALE_FAST);
ImageIcon newicon2 = new ImageIcon(img2);
icon1 = new ImageIcon(Main.class.getResource("image1.jpg"));
Image img1 = icon1.getImage().getScaledInstance(376,208,Image.SCALE_FAST);
ImageIcon newicon1 = new ImageIcon(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);
JLabel lblNewLabel = new JLabel(newicon3);
lblNewLabel.setBounds(10, 10, 376, 208);
contentPane.add(lblNewLabel);
JButton btnNewButton = new JButton("\u8001\u76AE");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
lblNewLabel.setIcon(newicon3);
}
});
btnNewButton.setBounds(10, 229, 87, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u96F7\u8332");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblNewLabel.setIcon(newicon0);
}
});
btnNewButton_1.setBounds(107, 229, 87, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("\u8D85\u7D1A\u9A0E\u58EB");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblNewLabel.setIcon(newicon2);
}
});
btnNewButton_2.setBounds(201, 229, 87, 23);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("\u5716\u502B");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblNewLabel.setIcon(newicon1);
}
});
btnNewButton_3.setBounds(298, 229, 87, 23);
contentPane.add(btnNewButton_3);
}
}
複製代碼
作者:
林侑成
時間:
2018-2-3 10:54
package asdf;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Window;
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 javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Zxcv {
private JFrame frame;
private ImageIcon icon;
private String newIcon;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Zxcv window = new Zxcv();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Zxcv() {
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.getContentPane().setLayout(null);
JLabel label = new JLabel();
label.setBounds(0, 0, 434, 239);
frame.getContentPane().add(label);
JButton btnNewButton = new JButton("無尾熊");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
icon = new ImageIcon(Zxcv.class.getResource("Koala.jpg"));
Image img = icon.getImage().getScaledInstance(434, 239, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
label.setIcon(newIcon);
}
});
btnNewButton.setBounds(0, 227, 115, 35);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("沙漠");
btnNewButton_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
icon = new ImageIcon(Zxcv.class.getResource("Desert.jpg"));
Image img = icon.getImage().getScaledInstance(434, 239, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
label.setIcon(newIcon);
}
});
btnNewButton_1.setBounds(116, 227, 107, 35);
frame.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("企鵝");
btnNewButton_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
icon = new ImageIcon(Zxcv.class.getResource("penguins.jpg"));
Image img = icon.getImage().getScaledInstance(434, 239, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
label.setIcon(newIcon);
}
});
btnNewButton_2.setBounds(222, 227, 115, 35);
frame.getContentPane().add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("燈塔");
btnNewButton_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
icon = new ImageIcon(Zxcv.class.getResource("Lighthouse.jpg"));
Image img = icon.getImage().getScaledInstance(434, 239, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
label.setIcon(newIcon);
}
});
btnNewButton_3.setBounds(332, 227, 102, 35);
frame.getContentPane().add(btnNewButton_3);
}
}
複製代碼
作者:
張健勳
時間:
2018-2-3 10:56
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.setBounds(142, 274, 153, 39);
contentPane.add(btn2);
JButton btn4 = new JButton("\u554F\u865F");
btn4.setBounds(142, 310, 153, 40);
contentPane.add(btn4);
JButton btn3 = new JButton("\u9E1A\u9D61");
btn3.setBounds(0, 310, 145, 40);
contentPane.add(btn3);
JButton btn1 = new JButton("\u8C93\u54AA");
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 11:10
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.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JProgressBar;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PHOTO extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PHOTO frame = new PHOTO();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PHOTO() {
setTitle("ImageIcon\u985E\u5225");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label_image = new JLabel("");
label_image.setBounds(0, 10, 431, 198);
contentPane.add(label_image);
JButton button = new JButton("\u7121\u5C3E\u718A");
button.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e)
{
ImageIcon icon = new ImageIcon(PHOTO.class.getResource("Koala.jpg"));
Image img = icon.getImage().getScaledInstance(431,198,Image.SCALE_FAST);
ImageIcon newIcon = new ImageIcon(img);
label_image.setIcon(newIcon);
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button.setBounds(5, 218, 100, 39);
JButton button_1 = new JButton("\u4F01\u9D5D");
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
ImageIcon icon = new ImageIcon(PHOTO.class.getResource("Penguins.jpg"));
Image img = icon.getImage().getScaledInstance(431,198,Image.SCALE_FAST);
ImageIcon newIcon = new ImageIcon(img);
label_image.setIcon(newIcon);
}
});
button_1.setBounds(115, 218, 100, 39);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
JButton button_2 = new JButton("\u6C99\u6F20");
button_2.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
ImageIcon icon = new ImageIcon(PHOTO.class.getResource("Desert.jpg"));
Image img = icon.getImage().getScaledInstance(431,198,Image.SCALE_FAST);
ImageIcon newIcon = new ImageIcon(img);
label_image.setIcon(newIcon);
}
});
button_2.setBounds(225, 218, 100, 39);
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
JButton button_3 = new JButton("\u71C8\u5854");
button_3.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
ImageIcon icon = new ImageIcon(PHOTO.class.getResource("Lighthouse.jpg"));
Image img = icon.getImage().getScaledInstance(431,198,Image.SCALE_FAST);
ImageIcon newIcon = new ImageIcon(img);
label_image.setIcon(newIcon);
}
});
button_3.setBounds(331, 218, 100, 39);
contentPane.setLayout(null);
contentPane.add(button);
contentPane.add(button_1);
contentPane.add(button_2);
contentPane.add(button_3);
}
}
複製代碼
作者:
黃茂勛
時間:
2018-2-3 11:38
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;
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();
}
}
});
}
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("無尾熊");
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("菊花");
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("沙漠");
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("燈塔");
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);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2