编译时有一个错误,但我找不到....请帮我看一下.....最好自己也编译一下.....先谢了....
//UseJLabel.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UseJLabel extends JFrame{
private int n=0;
private CardLayout cl=new CardLayout();
private JLabel cardLabel;
private Icon[] icons=new ImageIcon[12];
private JButton b1=new JButton("第一张");
private JButton b2=new JButton("下一张");
private JButton b3=new JButton("上一张");
private JButton b4=new JButton("最后一张");
public UseJLabel()
{
setLayout(new BorderLayout());
for(int i=0;i<12;i++)
icons[i]=new ImageIcon("images/T"+i+".gif");
cardLabel=new JLabel(icon[0]);
add (cardLabel,BordLayout.CENTER);
JPanel jp=new JPanel();
jp.setLayout(new FlowLayout());
jp.add(b1);
jp.add(b2);
jp.add(b3);
jp.add(b4);
b3.setEnabled(false);
add(jp,BordLayout.SOUTH);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
n=0;
cardLabel.setIcon(icon[n]);
b2.setEnabled(true);
b3.setEnabled(false);
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLabel.setIcon(icon[++n]);
if(n==1) b3.setEnabled(true);
if(n==icon.length-1) b2.setEnabled(false);
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLabel.setIcon(icon[--n]);
if(n==icon.length-2) b2.setEnabled(true);
if(n==0) b3.setEnabled(false);
}
});
b4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
n=icon.length-1;
cardLabel.setIcon(icon[n]);
b2.setEnabled(false);
b3.setEnabled(true);
}
});
}
public static void main(Sring[] args){
UseJLabel frame=new UseJLabel();
frame.setTitle("测试JLabel");
frame.setSize(400,300);
frame.setVisible(true);
frmae.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
參考答案:import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UseJLabel extends JFrame{
private int n=0;
private CardLayout cl=new CardLayout();
private JLabel cardLabel;
private Icon[] icons=new ImageIcon[12];
private JButton b1=new JButton("第一张");
private JButton b2=new JButton("下一张");
private JButton b3=new JButton("上一张");
private JButton b4=new JButton("最后一张");
public UseJLabel()
{
setLayout(new BorderLayout());
for(int i=0;i<12;i++)
icons[i]=new ImageIcon("images/T"+i+".gif");
cardLabel=new JLabel(icons[0]);
add (cardLabel);
JPanel jp=new JPanel();
jp.setLayout(new FlowLayout());
jp.add(b1);
jp.add(b2);
jp.add(b3);
jp.add(b4);
b3.setEnabled(false);
add(jp,BorderLayout.SOUTH);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
n=0;
cardLabel.setIcon(icons[n]);
b2.setEnabled(true);
b3.setEnabled(false);
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLabel.setIcon(icons[++n]);
if(n==1) b3.setEnabled(true);
if(n==icons.length-1) b2.setEnabled(false);
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLabel.setIcon(icons[--n]);
if(n==icons.length-2) b2.setEnabled(true);
if(n==0) b3.setEnabled(false);
}
});
b4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
n=icons.length-1;
cardLabel.setIcon(icons[n]);
b2.setEnabled(false);
b3.setEnabled(true);
}
});
}
public static void main(String[] args){
UseJLabel frame=new UseJLabel();
frame.setTitle("测试JLabel");
frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
调好了,竟然报了20多处错误