bjohibeoieb
第二
Java是一门坟墓
Java不好
packageday26;
importjava.awt.BorderLayout;
importjava.awt.EventQueue;
importjava.awt.Image;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
publicclassImageSwapExampleimplementsActionListener {
privateJFrameframe;
privateJPanelpanel;
privateJButtonbutton;
privateJLabelimageLabel;
privateImageIconimage1;
privateImageIconimage2;
publicstaticvoidmain(String[] args) {
EventQueue.invokeLater(newRunnable() {
publicvoidrun() {
try {
ImageSwapExamplewindow=newImageSwapExample();
window.frame.setVisible(true);
} catch (Exceptione) {
e.printStackTrace();
}
}
});
}
publicImageSwapExample() {
// Create the window
frame=newJFrame("Image Swap Example");
frame.setBounds(100, 100, 800, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(newBorderLayout());
// Create the panel
panel=newJPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
// Create the button
button=newJButton("Swap Image");
button.addActionListener(this);
panel.add(button);
// Load the images
image1=newImageIcon("src/无功德.png");
image2=newImageIcon("src/功德.png");
// Create the initial image label
imageLabel=newJLabel(image1);
panel.add(imageLabel);
}
publicvoidactionPerformed(ActionEvente) {
// Swap the images
if (imageLabel.getIcon() ==image1) {
imageLabel.setIcon(image2);
} else {
imageLabel.setIcon(image1);
}
}
}