0

I have this code and it works with everything until the bottom middle button is pressed.

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JLabel;

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

public class memory extends JFrame {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(new Color(156, 93, 82));
        g.fill3DRect(21, 3, 7, 12, true);
        g.setColor(new Color(156, 23, 134));
        g.fillOval(1, 15, 15, 15);
        g.fillOval(16, 15, 15, 15);
        g.fillOval(31, 15, 15, 15);
        g.fillOval(7, 31, 15, 15);
        g.fillOval(22, 31, 15, 15);
        g.fillOval(16, 47, 15, 15);
    }

    public memory() {

        GridLayout h = new GridLayout(3, 3);
        final JFrame frame = new JFrame();
        final JPanel pan = new JPanel(h);
        frame.add(pan);
        //pan=new JPanel(h);
        pan.setBackground(new Color(130, 224, 190));
        setFont(new Font("Serif", Font.BOLD, 28));
        JButton button1 = new JButton();
        pan.add(button1);
        final JLabel label1 = new JLabel("hi");
        label1.setVisible(false);
        pan.add(label1);
        JButton button2 = new JButton();
        pan.add(button2);
        final JLabel label2 = new JLabel("hi");
        label2.setVisible(false);
        pan.add(label2);
        JButton button3 = new JButton();
        pan.add(button3);
        final JLabel label3 = new JLabel("hi");
        label3.setVisible(false);
        pan.add(label3);
        JButton button4 = new JButton();
        pan.add(button4);
        final JLabel label4 = new JLabel("hi");
        label4.setVisible(false);
        pan.add(label4);
        JButton button5 = new JButton();
        pan.add(button5);
        final JLabel label5 = new JLabel("hi");
        label5.setVisible(false);
        pan.add(label5);
        JButton button6 = new JButton();
        pan.add(button6);
        final JLabel label6 = new JLabel("hi");
        label6.setVisible(false);
        pan.add(label6);
        JButton button7 = new JButton();
        pan.add(button7);
        final JLabel label7 = new JLabel("hi");
        label7.setVisible(false);
        pan.add(label7);
        JButton button8 = new JButton();
        pan.add(button8);
        final JLabel label8 = new JLabel("hi");
        label8.setVisible(false);
        pan.add(label8);
        JButton button9 = new JButton();
        pan.add(button9);
        final JButton button10 = new JButton("Exit");
        pan.add(button10);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("memory Game");
        setLayout(new BorderLayout());
        add(pan, BorderLayout.CENTER);
        add(button10, BorderLayout.SOUTH);
        setSize(600, 600);
        setVisible(true);
        final JLabel label9 = new JLabel("hi");
        label9.setVisible(false);
        pan.add(label9);
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label1.setVisible(true);
            }
        });
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label2.setVisible(true);
            }
        });
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label3.setVisible(true);
            }
        });
        button4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label4.setVisible(true);
            }
        });
        button5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label5.setVisible(true);
            }
        });
        button6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label6.setVisible(true);
            }
        });
        button7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label7.setVisible(true);
            }
        });
//this is where I thought it was going to do something if it was pressed.
        button8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label8.setVisible(true);
                frame.getContentPane().add(new memory());
                setVisible(true);

            }
        }
                );
        button9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label9.setVisible(true);
            }
        }
                );
        button10.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (button10.getSize() != null) {
                    System.exit(0);
                }
            }
        });

    }

    public static void main(String args[]) {
        new memory();
    }

}
4

1 回答 1

1

memory extends JFrame. Swing won't let you add a window to another window.

You should avoid extending from top level containers (this is just reason) and instead use something like JPanel.

This allows you to add the component to whatever container you like

JFrame does not have a paintComponent method, so it will never be called. The reason it compiles is that you've called super.paintComponents (note the s at the end).

Instead, you should, extend from something like JPanel and override it's paintComponent method and perform you custom painting from there

于 2013-10-07T03:20:47.817 回答