写了一个代码,一个JFrame( 称为 A )上面安装了一个 BorderLayout, 上面有4个JPanel,在其中的一个JPanel(称为 B)上面又有一个JPanel (称为 C ), 我在C上面实现了 ActionListener 的 actionPerformed() 但是好像捉不到消息,大家帮我看看是怎么会回事啊,我把相关的代买贴上来了谢谢各位了// JPanle C 上面的public class VirtualWorldManagerSettingPanel extends JPanel implements
ActionListener { JLabel statue = new JLabel("off-line");
JRadioButton VisSimulation = new JRadioButton("Vis Simulation");
JRadioButton GenSimulation = new JRadioButton("Gen Simulation"); ButtonGroup group = new ButtonGroup();
public VirtualWorldManagerSettingPanel() { this.setBorder(BorderFactory
.createTitledBorder("Virtual World Manager"));
// this.setSize(new Dimension(400,200));
group.add(VisSimulation);
group.add(GenSimulation);
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(2, 0));
jp.add(VisSimulation);
jp.add(GenSimulation);
GenSimulation.setSelected(true);
this.add(jp); } @Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if( arg0.getSource().equals("Vis Simulation")){
System.out.println("Vis Simulation");
}
if( arg0.getSource().equals("Gen Simulation")){
System.out.println("Vis Simulation");
}
}}// JPanle B 和 JFrame 都写在一起了public class MASSimulator extends JFrame { private static MASSimulator INSTANCE; // panels that are the basical layout of main frame
private JPanel North; // the panels that private VirtualWorldManagerSettingPanel vwmsp = new VirtualWorldManagerSettingPanel(); private MASSimulator() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
private void buildInterface() { this.buildNorthPanel();
} private void buildNorthPanel() {
this.North = new JPanel();
this.North.add(this.vwmsp); this.add(this.North, BorderLayout.NORTH);
}
@Override
protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) { Calendar cal = Calendar.getInstance();
String name = cal.getTime().getHours() + "_"
+ cal.getTime().getMinutes() + "_"
+ cal.getTime().getSeconds(); ProcessLogStorage.getInstance().saveSimulationLogAsXML(
"./Logs/" + name + ".mxml");
} super.processWindowEvent(e); } public static MASSimulator getInstance() {
if (INSTANCE == null) {
INSTANCE = new MASSimulator();
}
return INSTANCE;
} public static void main(String[] arg) {
getInstance().setVisible(true);
}}
ActionListener { JLabel statue = new JLabel("off-line");
JRadioButton VisSimulation = new JRadioButton("Vis Simulation");
JRadioButton GenSimulation = new JRadioButton("Gen Simulation"); ButtonGroup group = new ButtonGroup();
public VirtualWorldManagerSettingPanel() { this.setBorder(BorderFactory
.createTitledBorder("Virtual World Manager"));
// this.setSize(new Dimension(400,200));
group.add(VisSimulation);
group.add(GenSimulation);
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(2, 0));
jp.add(VisSimulation);
jp.add(GenSimulation);
GenSimulation.setSelected(true);
this.add(jp); } @Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if( arg0.getSource().equals("Vis Simulation")){
System.out.println("Vis Simulation");
}
if( arg0.getSource().equals("Gen Simulation")){
System.out.println("Vis Simulation");
}
}}// JPanle B 和 JFrame 都写在一起了public class MASSimulator extends JFrame { private static MASSimulator INSTANCE; // panels that are the basical layout of main frame
private JPanel North; // the panels that private VirtualWorldManagerSettingPanel vwmsp = new VirtualWorldManagerSettingPanel(); private MASSimulator() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
private void buildInterface() { this.buildNorthPanel();
} private void buildNorthPanel() {
this.North = new JPanel();
this.North.add(this.vwmsp); this.add(this.North, BorderLayout.NORTH);
}
@Override
protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) { Calendar cal = Calendar.getInstance();
String name = cal.getTime().getHours() + "_"
+ cal.getTime().getMinutes() + "_"
+ cal.getTime().getSeconds(); ProcessLogStorage.getInstance().saveSimulationLogAsXML(
"./Logs/" + name + ".mxml");
} super.processWindowEvent(e); } public static MASSimulator getInstance() {
if (INSTANCE == null) {
INSTANCE = new MASSimulator();
}
return INSTANCE;
} public static void main(String[] arg) {
getInstance().setVisible(true);
}}
arg0.getSource()
获得的是产生事件的控件“对象”
而不是产生事件的控件“对象名称”
所以你应该写成
arg0.getSource().equals(VisSimulation)
或者
arg0.getActionCommand().equals("Vis Simulation")二、
你的面板实现了ActionListener,
却没有把这个ActionListener注册到具体的控件上
增加这样的语句:
VisSimulation.addActionListener(this);
GenSimulation.addActionListener(this);