JAVA边界布局管理器BorderLayout程序问题 10
从网上找了一些关于java.awt包的程序,用myeclipse敲出来以后,有些地方会提示报错,仔细对照了一下,代码也没有抄错,麻烦大神帮忙看一下,告知报错原因。完整代码...
从网上找了一些关于java.awt包的程序,用myeclipse敲出来以后,有些地方会提示报错,仔细对照了一下,代码也没有抄错,麻烦大神帮忙看一下,告知报错原因。完整代码如下:
package com.awtstudy;
import java.awt.*;
import java.awt.event.*;
public class BorderLayoutDemo {
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.EAST,
BorderLayout.WEST, BorderLayout.CENTER };
private Button[] buttons = new Button[5];
// 构造函数
public BorderLayoutDemo(String title){
super(title);
final BorderLayout layout = (BorderLayout)this.getLayout();
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < buttons.length; 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.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");
}
}
具体报错提示如下: 展开
package com.awtstudy;
import java.awt.*;
import java.awt.event.*;
public class BorderLayoutDemo {
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.EAST,
BorderLayout.WEST, BorderLayout.CENTER };
private Button[] buttons = new Button[5];
// 构造函数
public BorderLayoutDemo(String title){
super(title);
final BorderLayout layout = (BorderLayout)this.getLayout();
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < buttons.length; 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.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");
}
}
具体报错提示如下: 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询