- 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;
- }
- }
複製代碼 |