为什么JLabel中的图片怎么显示不了啊
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassExcese8_1exten...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Excese8_1 extends JFrame
{
static final int A=4;
private JButton jbutton[]=new JButton[4];
private JComboBox combobox;
private JLabel label[]=new JLabel[A];
private JPanel panel2[]=new JPanel[A];
private JPanel panel[]=new JPanel[3];
Container container;
Excese8_1()
{
super("card");
container=getContentPane();
jbutton[0]=new JButton("第一张");//创建jbutton对象
jbutton[1]=new JButton("下一张");
jbutton[2]=new JButton("上一张");
jbutton[3]=new JButton("最后一张");
for(int i=0;i<label.length;i++)//循环创建label对象
{
label[i]=new JLabel(new ImageIcon("E:\\java图片\\i+1.gif"));
}
for(int i=0;i<panel.length;i++)//创建panel对象
{
panel[i]=new JPanel();
}
container.setLayout(new BorderLayout());
panel[1].setLayout(new CardLayout());
panel[2].setLayout(new FlowLayout());
String names[]={"图片1","图片2","图片3","图片4"};
combobox=new JComboBox(names);//创建combobox对象
panel[0].setBackground(Color.red);
panel[2].setBackground(Color.green);
panel[0].add(combobox);
for(int i=0;i<panel2.length;i++)//创建panel2对象
{
panel2[i]=new JPanel();
panel2[i].add(label[i]);
panel[1].add("图片i",panel2[i]);
}
for(int i=0;i<jbutton.length;i++)
panel[2].add(jbutton[i]);
//为combobox注册事件监听
combobox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
CardLayout c=(CardLayout)panel[1].getLayout();
c.show(panel[1],(String)e.getItem());
}
});
for(int i=0;i<jbutton.length;i++)//为jbutton注册事件监听
{
jbutton[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbutton[0])
{
CardLayout cl=(CardLayout)panel[1].getLayout();
cl.first(panel[1]);
}
if(e.getSource()==jbutton[1])
{
CardLayout cl=(CardLayout)panel[1].getLayout();
cl.next(panel[1]);
}
if(e.getSource()==jbutton[2])
{
CardLayout cl=(CardLayout)panel[1].getLayout();
cl.previous(panel[1]);
}
if(e.getSource()==jbutton[3])
{
CardLayout cl=(CardLayout)panel[1].getLayout();
cl.last(panel[1]);
}
}
});
}
container.add(panel[0],BorderLayout.WEST);
container.add(panel[1],BorderLayout.EAST);
container.add(panel[2],BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String args[])
{
Excese8_1 bl=new Excese8_1();
bl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} 展开
import javax.swing.*;
import java.awt.event.*;
public class Excese8_1 extends JFrame
{
static final int A=4;
private JButton jbutton[]=new JButton[4];
private JComboBox combobox;
private JLabel label[]=new JLabel[A];
private JPanel panel2[]=new JPanel[A];
private JPanel panel[]=new JPanel[3];
Container container;
Excese8_1()
{
super("card");
container=getContentPane();
jbutton[0]=new JButton("第一张");//创建jbutton对象
jbutton[1]=new JButton("下一张");
jbutton[2]=new JButton("上一张");
jbutton[3]=new JButton("最后一张");
for(int i=0;i<label.length;i++)//循环创建label对象
{
label[i]=new JLabel(new ImageIcon("E:\\java图片\\i+1.gif"));
}
for(int i=0;i<panel.length;i++)//创建panel对象
{
panel[i]=new JPanel();
}
container.setLayout(new BorderLayout());
panel[1].setLayout(new CardLayout());
panel[2].setLayout(new FlowLayout());
String names[]={"图片1","图片2","图片3","图片4"};
combobox=new JComboBox(names);//创建combobox对象
panel[0].setBackground(Color.red);
panel[2].setBackground(Color.green);
panel[0].add(combobox);
for(int i=0;i<panel2.length;i++)//创建panel2对象
{
panel2[i]=new JPanel();
panel2[i].add(label[i]);
panel[1].add("图片i",panel2[i]);
}
for(int i=0;i<jbutton.length;i++)
panel[2].add(jbutton[i]);
//为combobox注册事件监听
combobox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
CardLayout c=(CardLayout)panel[1].getLayout();
c.show(panel[1],(String)e.getItem());
}
});
for(int i=0;i<jbutton.length;i++)//为jbutton注册事件监听
{
jbutton[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbutton[0])
{
CardLayout cl=(CardLayout)panel[1].getLayout();
cl.first(panel[1]);
}
if(e.getSource()==jbutton[1])
{
CardLayout cl=(CardLayout)panel[1].getLayout();
cl.next(panel[1]);
}
if(e.getSource()==jbutton[2])
{
CardLayout cl=(CardLayout)panel[1].getLayout();
cl.previous(panel[1]);
}
if(e.getSource()==jbutton[3])
{
CardLayout cl=(CardLayout)panel[1].getLayout();
cl.last(panel[1]);
}
}
});
}
container.add(panel[0],BorderLayout.WEST);
container.add(panel[1],BorderLayout.EAST);
container.add(panel[2],BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String args[])
{
Excese8_1 bl=new Excese8_1();
bl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} 展开
2个回答
展开全部
label[i]=new JLabel(new ImageIcon("E:\\java图片\\i+1.gif"));
地址写错了,你的图片是1.gif 2.gif 3.gif 一直顺序下去的吧。
i+1这里不应该放在字符串里面 "E:\\java图片\\“+(i+1)+".gif" 这样写
地址写错了,你的图片是1.gif 2.gif 3.gif 一直顺序下去的吧。
i+1这里不应该放在字符串里面 "E:\\java图片\\“+(i+1)+".gif" 这样写
更多追问追答
追问
"E:\\java图片\\"+(i+1)+".gif" 是这样的吧 还是显示不了
追答
我用你的程序显示没问题,看看你图片的位置是不是在e盘的“java图片”文件夹下面,还有名字是不是1.gif 2.gif 3.gif 4.gif
对啦,你程序里还有个错误,combobox切不了图片,原因是这个panel[1].add("图片i",panel2[i]);
同样的错误,应改为panel[1].add("图片"+(i+1),panel2[i]);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询