日期:2014-05-20 浏览次数:20713 次
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JToggleButton; public class FrameA extends JFrame implements ActionListener { int i=1; public FrameB fb[]; JToggleButton toggleButton[]; /** * Launch the application * @param args */ public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { FrameA frame = new FrameA(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame */ public FrameA() { super(); getContentPane().setLayout(null); fb = new FrameB[10]; JToggleButton toggleButton[] = new JToggleButton[3]; setBounds(100, 100, 195, 180); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for(int i=0; i<3; i++){ toggleButton[i] = new JToggleButton(); toggleButton[i].setText("ToggleButton"); toggleButton[i].setBounds(10, 25+i*50, 144, 26); getContentPane().add(toggleButton[i]); toggleButton[i].addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent arg0) { JToggleButton newtb1 = (JToggleButton)arg0.getSource(); if(newtb1.isSelected()) { System.out.println("on"); fb[i]= new FrameB(); //问题出在这里,说是要把i变成final }else{ System.out.println("off"); fb[i].dispose(); //同样,i的问题 } } }); } } }