1

画像の上に JButton を配置する前は、画像が適切に表示されていました。コードに JButton を追加したので、画像が表示されません。ActionPerformed メソッドで、ボタンにsetVisbible(false). ボタンをクリックすると消えて、その後ろにあるのは背景だけです。

public class Main extends JFrame implements ActionListener {

public static void main(String[] args) {
    Main main = new Main();

}

ImageIcon GIF = new ImageIcon("src/Zombie Steve gif.gif");
JButton button = new JButton("Click me!");
JLabel Label = new JLabel(GIF);

public Main() {

    button.addActionListener(this);

    Label.setHorizontalAlignment(0);

    JFrame Frame = new JFrame("zombieSteveGIF");
    Frame.setSize(650, 650);
    Frame.setVisible(true);
    Frame.add(Label);
    Frame.add(button);
    Frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    while (true) {
        Frame.getContentPane().setBackground(Color.BLUE);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Frame.getContentPane().setBackground(Color.GREEN);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Frame.getContentPane().setBackground(Color.RED);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

public void actionPerformed(ActionEvent e) {
    button.setVisible(false);

}

}
4

1 に答える 1

3

問題は、BorderLayout(s のデフォルトJFrame) があり、同じ位置に 2 つのコンポーネントを追加していることです。デフォルトはBorderLayout.CENTERで、デフォルトの制約だけで 2 つのコンポーネントを追加すると、最初のコンポーネントが削除され、2 番目のコンポーネントがその場所に配置されます。

あなたの問題を解決することに関して、あなたは何を達成したいですか? コンポーネントを重ねて表示する場合は、OverlayLayout. これが望ましくない場合は、他のレイアウト マネージャーを試してください。

于 2013-07-28T08:52:44.213 に答える