親ウィンドウのボタンを押して、子ウインドウが表示される仕組みなのですが、1 つ表示した後は、 表示させないようにしたいと考えコードを組みましたが、以下コードでは、null 例外が出てしまいます。 良い方法はありませんでしょうか? みなさん、どうぞよろしくお願い致します。 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class FramesTest { public static void main(String[] args) { JFrame frameMain = new JFrame("Main"); frameMain.setSize(256, 128); frameMain.getContentPane().add( new BT() ); frameMain.setVisible(true); } } class BT extends JPanel implements ActionListener { /** * */ private static final long serialVersionUID = 1L; /*-------------------------* * Variables. * *-------------------------*/ static String cmdName; static JButton button; static JFrame subJFrame; /*-------------------------* * Methods. * *-------------------------*/ BT() { super( new BorderLayout() ); button = new JButton("button"); button.addActionListener(this); add( button ); setPreferredSize(new Dimension(320, 100)); } void createFrame() { subJFrame = new JFrame( "sub" ); subJFrame.setVisible(true); subJFrame.setSize(256, 128); } @Override public void actionPerformed(ActionEvent e) { cmdName = e.getActionCommand(); if ("button".equals(cmdName)) { if ( !subJFrame.isActive() ) { createFrame(); } } } }
↧