这个Java程序错在什么地方(BorderLayout布局)
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassBorderLayoutDe...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BorderLayoutDemo extends JFrame implementsActionListener{
private JButton buttons[];
private final String names[] = {"North","South","East","West","Center"};
private BorderLayout layout;
private Container container;
public BorderLayoutDemo(){
super("BoderLayout布局");
layout = new BorderLayout(5,5);
container = getContentPane();
container.setLayout(layout);
buttons = new JButton[names.length];
for (int count = 0;count<names.length;count++){
buttons[count] = new JButton(names[count]);
buttons[count].addActionListener(this);
}
container.add(buttons[0],BorderLayout.NORTH);
container.add(buttons[1],BorderLayout.SOUTH);
container.add(buttons[2],BorderLayout.EAST);
container.add(buttons[3],BorderLayout.WEST);
container.add(buttons[4],BorderLayout.CENTER);
setSize(300,200);setVisible(true);
}
public void actionPerformed(ActionEvent event){
for(int count = 0;count<buttons.length;count++)
if (event.getSource()==buttons[count])
buttons[count].setVisible(false);
else
buttons[count].setVisible(true);
layout.layoutContainer(container);
}
public static void main(String []args){
BorderLayoutDemo application = new BorderLayoutDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} 展开
import java.awt.event.*;
import javax.swing.*;
public class BorderLayoutDemo extends JFrame implementsActionListener{
private JButton buttons[];
private final String names[] = {"North","South","East","West","Center"};
private BorderLayout layout;
private Container container;
public BorderLayoutDemo(){
super("BoderLayout布局");
layout = new BorderLayout(5,5);
container = getContentPane();
container.setLayout(layout);
buttons = new JButton[names.length];
for (int count = 0;count<names.length;count++){
buttons[count] = new JButton(names[count]);
buttons[count].addActionListener(this);
}
container.add(buttons[0],BorderLayout.NORTH);
container.add(buttons[1],BorderLayout.SOUTH);
container.add(buttons[2],BorderLayout.EAST);
container.add(buttons[3],BorderLayout.WEST);
container.add(buttons[4],BorderLayout.CENTER);
setSize(300,200);setVisible(true);
}
public void actionPerformed(ActionEvent event){
for(int count = 0;count<buttons.length;count++)
if (event.getSource()==buttons[count])
buttons[count].setVisible(false);
else
buttons[count].setVisible(true);
layout.layoutContainer(container);
}
public static void main(String []args){
BorderLayoutDemo application = new BorderLayoutDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} 展开
2个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BorderLayoutDemo extends JFrame implements ActionListener {
private JButton buttons[];
private final String names[] = { "North", "South", "East", "West", "Center" };
private BorderLayout layout;
private Container container;
public BorderLayoutDemo() {
super("BoderLayout布局");
layout = new BorderLayout(5, 5);
container = getContentPane();
container.setLayout(layout);
buttons = new JButton[names.length];
for (int count = 0; count < names.length; count++) {
buttons[count] = new JButton(names[count]);
buttons[count].addActionListener(this);
}
container.add(buttons[0], BorderLayout.NORTH);
container.add(buttons[1], BorderLayout.SOUTH);
container.add(buttons[2], BorderLayout.EAST);
container.add(buttons[3], BorderLayout.WEST);
container.add(buttons[4], BorderLayout.CENTER);
setSize(300, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
for (int count = 0; count < buttons.length; count++)
if (event.getSource() == buttons[count])
buttons[count].setVisible(false);
else
buttons[count].setVisible(true);
layout.layoutContainer(container);
}
public static void main(String[] args) {
BorderLayoutDemo application = new BorderLayoutDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
正确的
import java.awt.event.*;
import javax.swing.*;
public class BorderLayoutDemo extends JFrame implements ActionListener {
private JButton buttons[];
private final String names[] = { "North", "South", "East", "West", "Center" };
private BorderLayout layout;
private Container container;
public BorderLayoutDemo() {
super("BoderLayout布局");
layout = new BorderLayout(5, 5);
container = getContentPane();
container.setLayout(layout);
buttons = new JButton[names.length];
for (int count = 0; count < names.length; count++) {
buttons[count] = new JButton(names[count]);
buttons[count].addActionListener(this);
}
container.add(buttons[0], BorderLayout.NORTH);
container.add(buttons[1], BorderLayout.SOUTH);
container.add(buttons[2], BorderLayout.EAST);
container.add(buttons[3], BorderLayout.WEST);
container.add(buttons[4], BorderLayout.CENTER);
setSize(300, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
for (int count = 0; count < buttons.length; count++)
if (event.getSource() == buttons[count])
buttons[count].setVisible(false);
else
buttons[count].setVisible(true);
layout.layoutContainer(container);
}
public static void main(String[] args) {
BorderLayoutDemo application = new BorderLayoutDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
正确的
展开全部
public class BorderLayoutDemo extends JFrame implementsActionListener
java不支持多从继承
按钮可以用匿名内部类来加
buttons[count].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//按钮的响应事件.自己写吧
}
});
}
把下边的按键事件响应想到上边
for(int count = 0;count<buttons.length;count++)
if (event.getSource()==buttons[count])
buttons[count].setVisible(false);
else
buttons[count].setVisible(true);
layout.layoutContainer(container);
}
java不支持多从继承
按钮可以用匿名内部类来加
buttons[count].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//按钮的响应事件.自己写吧
}
});
}
把下边的按键事件响应想到上边
for(int count = 0;count<buttons.length;count++)
if (event.getSource()==buttons[count])
buttons[count].setVisible(false);
else
buttons[count].setVisible(true);
layout.layoutContainer(container);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询