import javax.swing.*;
import java.awt.*;
public class example10_8{
public static void main(String args[]){
new MyWin();
}
}
class MyWin extends JFrame{
JTabbedPane p;
Icon[];
String imageName[]={"a.jpg","b.jpg","c.jpg","d.jpg","e.jpg"};
public MyWin(){
setBounds(100,100,500,300);
setVisible(true);
icon=new Icon[imageName];
for(int i=0;i<icon.length;i++);
icon[i]=new ImageIcon(imageName[i]);
p=new JTabbedPane(JTabbedPane.LEFT);
for(int i=0;i<icon.length;i++){
int m=i+1;
p.add("观看第"+m+"个图片",new.JButton(icon[i]));
}
p.validate();
add(p.BorderLayout.CENTER);
validate();
setDefaultCloseOperation(JFrame.dispose_ON_CLOSE);
}
}
------解决方案--------------------------------------------------------
照书上抄的吧,很多大小写和标点符号错误。
正确代码如下:
- Java code
import javax.swing.*;import java.awt.*;public class example10_8 { public static void main(String args[]) { new MyWin(); }}class MyWin extends JFrame { JTabbedPane p; Icon[] icon; String imageName[] = { "a.jpg", "b.jpg", "c.jpg", "d.jpg", "e.jpg" }; public MyWin() { setBounds(100, 100, 500, 300); setVisible(true); icon = new Icon[imageName.length]; for (int i = 0; i < icon.length; i++) icon[i] = new ImageIcon(imageName[i]); p = new JTabbedPane(JTabbedPane.LEFT); for (int i = 0; i < icon.length; i++) { int m = i + 1; p.add("观看第" + m + "个图片", new JButton(icon[i])); } p.validate(); add(p, BorderLayout.CENTER); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); }}
------解决方案--------------------------------------------------------
import javax.swing.*;
import java.awt.*;
public class example10_8{
public static void main(String args[]){
new MyWin();
}
}
class MyWin extends JFrame{
JTabbedPane p;
Icon[] icon;//定义数组引用需要加上名称,这和定义int i一样,不可以就写一个int,就结束了
String imageName[]={"a.jpg","b.jpg","c.jpg","d.jpg","e.jpg"};
public MyWin(){
setBounds(100,100,500,300);
setVisible(true);
icon=new Icon[imageName.length];//
for(int i=0;i<icon.length;i++) //如果该语句加上“;”,表示for结束,下面的语句就不可以使用i了
icon[i]=new ImageIcon(imageName[i]);
p=new JTabbedPane(JTabbedPane.LEFT);
for(int i=0;i<icon.length;i++)
{
int m=i+1;
p.add("观看第"+m+"个图片",new JButton(icon[i]));//new后面应该是空格,不该是“.”
}
p.validate();
//add(BorderLayout.CENTER);//括号中不需使用p;另外好像没有add(String s)这样的方法--参见1楼改好了,确实是符号写错,add(p,BorderLayout.CENTER);,我也学习一下,
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//java区分大小写的,常量都是使用大写
}
}