按钮事件怎么触发不了???import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class BoxLayoutTest2
{
public static void main(String[] args)
{
BoxLayoutFrame2 frame=new BoxLayoutFrame2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class BoxLayoutFrame2 extends JFrame
{
public BoxLayoutFrame2()
{
setTitle("BoxLayoutTest");
setSize(300,200);
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
box3=Box.createVerticalBox();
Box tbox=Box.createHorizontalBox();
tbox.add(box1);
tbox.add(Box.createGlue());
tbox.add(box2);
tbox.add(Box.createGlue());
tbox.add(box3);
add(tbox,BorderLayout.NORTH);
makebox("RED",box1,Color.red);
makebox("Black",box1,Color.black);
makebox("Green",box2,Color.green);
makebox("Blue",box2,Color.blue);
makebox("Cyan",box3,Color.cyan);
makebox("Yellow",box3,Color.yellow);
}
void makebox(String name,Box box,final Color backcolor)
{
JButton button=new JButton(name);
box.add(button);
box.add(Box.createVerticalStrut(10));
button.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setBackground(backcolor);
}
});
}
private Box box1;
private Box box2;
private Box box3;
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class BoxLayoutTest2
{
public static void main(String[] args)
{
BoxLayoutFrame2 frame=new BoxLayoutFrame2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class BoxLayoutFrame2 extends JFrame
{
public BoxLayoutFrame2()
{
setTitle("BoxLayoutTest");
setSize(300,200);
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
box3=Box.createVerticalBox();
Box tbox=Box.createHorizontalBox();
tbox.add(box1);
tbox.add(Box.createGlue());
tbox.add(box2);
tbox.add(Box.createGlue());
tbox.add(box3);
add(tbox,BorderLayout.NORTH);
makebox("RED",box1,Color.red);
makebox("Black",box1,Color.black);
makebox("Green",box2,Color.green);
makebox("Blue",box2,Color.blue);
makebox("Cyan",box3,Color.cyan);
makebox("Yellow",box3,Color.yellow);
}
void makebox(String name,Box box,final Color backcolor)
{
JButton button=new JButton(name);
box.add(button);
box.add(Box.createVerticalStrut(10));
button.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setBackground(backcolor);
}
});
}
private Box box1;
private Box box2;
private Box box3;
}
那个监听好像是对按钮监听
但是你的按钮不是已经加到box里了吗?
那应该对box进行监听吧,不过这里加上box是什么作用呢?单纯的练习Box这个类啊。。