java边界布局的一个例子,求发现错在哪儿?
packagecom.abc;importjava.awt.*;importjava.awt.event.ActionEvent;publicclassBorderLay...
package com.abc;
import java.awt.*;
import java.awt.event.ActionEvent;
public class BorderLayoutDemo extends Frame
{
private final String names[]={"Hide North","Hide South","Hide East","Hide West","Hide Center"};
private final String locations[]={BorderLayout.NORTH,BorderLayout.SOUTH,BorderLayout.EAST,BorderLayout.WEST,BorderLayout.CENTER};
private Button[] buttons=new Button[5];
public BorderLayoutDemo(String title)
{
super(title);
final BorderLayout layout=(BorderLayout)this.getLayout();
int i;
ActionListener listener=new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
for(int i=1;i<buttons;i++)
if(event.getSource()==buttons[i])
buttons[i].setVisible(false);
else
buttons[i].setVisible(true);
layout.layoutContainer(BorderLayoutDemo.this);
}
};
for(int i=0;i<buttons;i++)
{
buttons[i]=new Button(names[i]);
buttons[i].addActionListener(listener);
add(buttons[i],locations[i]);
}
setSize(250,250);
setVisible(true);
}
public static void main(String[] args)
{
new BorderLayoutDemo("Hello");
}
} 展开
import java.awt.*;
import java.awt.event.ActionEvent;
public class BorderLayoutDemo extends Frame
{
private final String names[]={"Hide North","Hide South","Hide East","Hide West","Hide Center"};
private final String locations[]={BorderLayout.NORTH,BorderLayout.SOUTH,BorderLayout.EAST,BorderLayout.WEST,BorderLayout.CENTER};
private Button[] buttons=new Button[5];
public BorderLayoutDemo(String title)
{
super(title);
final BorderLayout layout=(BorderLayout)this.getLayout();
int i;
ActionListener listener=new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
for(int i=1;i<buttons;i++)
if(event.getSource()==buttons[i])
buttons[i].setVisible(false);
else
buttons[i].setVisible(true);
layout.layoutContainer(BorderLayoutDemo.this);
}
};
for(int i=0;i<buttons;i++)
{
buttons[i]=new Button(names[i]);
buttons[i].addActionListener(listener);
add(buttons[i],locations[i]);
}
setSize(250,250);
setVisible(true);
}
public static void main(String[] args)
{
new BorderLayoutDemo("Hello");
}
} 展开
展开全部
// buttons是数组,取它的长度要用 buttons.length
// i这个变量已经在前面定义了。 在这里直接赋值使用就OK,不要再定义。
正确的代码:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BorderLayoutDemo extends Frame {
private final String names[] = { "Hide North", "Hide South", "Hide East",
"Hide West", "Hide Center" };
private final String locations[] = { BorderLayout.NORTH,
BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST,
BorderLayout.CENTER };
private Button[] buttons = new Button[5];
public BorderLayoutDemo(String title) {
super(title);
final BorderLayout layout = (BorderLayout) this.getLayout();
int i;
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
// buttons是数组,取它的长度要用 buttons.length
for (int i = 1; i < buttons.length; i++)
if (event.getSource() == buttons[i])
buttons[i].setVisible(false);
else
buttons[i].setVisible(true);
layout.layoutContainer(BorderLayoutDemo.this);
}
};
// buttons是数组,取它的长度要用 buttons.length
// i这个变量已经在前面定义了。 在这里直接赋值使用就OK,不要再定义。
for (i = 0; i < buttons.length; i++) {
buttons[i] = new Button(names[i]);
buttons[i].addActionListener(listener);
add(buttons[i], locations[i]);
}
setSize(250, 250);
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutDemo("Hello");
}
}
追问
谢谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询