123
作者:互联网
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class b extends JFrame{
private JPanel cardPanel;
private JFrame f;
private CardLayout cardLayout;
public static void main(String args[]){
b ac = new b();
ac.setVisible(true);
}
public b(){
super();
setTitle("card");
setBounds(100,100,600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f = new JFrame();
cardLayout = new CardLayout();
cardPanel = new JPanel(cardLayout);
ImageIcon m1 = new ImageIcon("C:\图片A.jpeg");
ImageIcon m2 = new ImageIcon("C:\图片B.jpg");
ImageIcon m3 = new ImageIcon("C:\图片C.jpeg");
JLabel label1 = new JLabel(m1);
JLabel label2 = new JLabel(m2);
JLabel label3 = new JLabel(m3);
cardPanel.add(label1,"图片A");
cardPanel.add(label2,"图片B");
cardPanel.add(label3,"图片C");
f.add(cardPanel,BorderLayout.CENTER);
getContentPane().add(cardPanel,BorderLayout.CENTER);
final JPanel buttonPanel = new JPanel();
getContentPane().add(buttonPanel,BorderLayout.SOUTH);
String[] buttonNames = {"第一个","前一个","图片A","图片B","图片C","后一个","最后一个"};
for( int i = 0;i<buttonNames.length;i++){
final JButton button = new JButton(buttonNames[i]);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String buttonText = button.getText();
if(buttonText.equals("第一个"))
cardLayout.first(cardPanel);
else if(buttonText.equals("前一个"))
cardLayout.previous(cardPanel);
else if(buttonText.equals("图片A"))
cardLayout.show(cardPanel, "图片A");
else if(buttonText.equals("图片B"))
cardLayout.show(cardPanel, "图片B");
else if(buttonText.equals("图片C"))
cardLayout.show(cardPanel, "图片C");
else if(buttonText.equals("后一个"))
cardLayout.next(cardPanel);
else
cardLayout.last(cardPanel);
}
});
buttonPanel.add(button);
}
}
}
标签:ImageIcon,cardPanel,add,123,new,JLabel,图片 来源: https://www.cnblogs.com/1786722420yang/p/11810152.html