3

可能なアクションごとにいくつかのボタン (JButton) を備えたウェルカム (またはメニュー) ウィンドウ (JFrame) があります。これらはそれぞれ、新しいウィンドウを起動し、ようこそウィンドウを非表示にする必要があります。私はそれができることを知っていsetVisible(false);ます。しかし、私はまだそれを機能させることができません。

これは私が持っているコードの一例です:

    _startBtn.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            System.out.println("_startBtn pressed");
            // Code to hide this JFrame and initialize another
        }

私の質問は、このような匿名クラスを使用してそれを行うにはどうすればよいですか?

前もって感謝します!

4

1 に答える 1

2

私はあなたのために例を投稿しています。それがあなたに役立つことを願っています。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;


public class windows_test {
    JFrame login = null;
    JFrame inner_frame = null;

    public windows_test() {
        login = new JFrame();
        login.setBounds(10, 10, 300, 300);
        login.setLayout(new BorderLayout());

        JButton button = new JButton("Login");
        login.add(button, BorderLayout.CENTER);

        login.setVisible(true);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (inner_frame == null) {
                    inner_frame = new JFrame();
                }
                inner_frame.setLayout(new FlowLayout(FlowLayout.CENTER));
                inner_frame.add(new JButton("inner frame"));
                inner_frame.setVisible(true);
                login.setVisible(false);
                inner_frame.setBounds(10, 10, 300, 300);
            }
        });
    }
}

jframes の代わりに jpanel を使用することをお勧めしますが、フレームを求められたので、それらで作成しました。私がどこか間違っているか、理解できないかどうかを尋ねるのに役立つことを願っています.

于 2012-08-12T05:26:33.347 に答える