0

所以我以为我有这个代码能够工作,但它不工作。我不知道该怎么做,我已经尝试查看每个人的建议,但我只是不知道该怎么做,所以任何帮助将不胜感激!

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 JPanel{

/**
 * 
 */
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(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);
    setVisible(false);}


public memory()
{

GridLayout h =new GridLayout(3,3);
final JFrame frame = new JFrame();
final JPanel pan = new JPanel(h);
frame.add(pan);
pan.setBackground(new Color(130,224,190));
pan.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);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Memory Game");
frame.setSize(500,500);
frame.setVisible(true);
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);
    }
});
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

2 回答 2

2

正如我在上一个问题中所说,您需要将面板添加到JFrame...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new memory());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

花一些时间阅读使用 JFC/Swing 创建 GUI

您还需要删除

final JFrame frame = new JFrame();
final JPanel pan = new JPanel(h);
frame.add(pan);

从构造函数和简单的将您的组件直接添加到 ( memory) 面板

你还需要setVisible(false);从你的paintComponent方法中删除......这解释了为什么你有这么多问题......

于 2013-10-07T04:19:48.657 回答
1

您必须有一个 JFrame 并将其展示给一个摇摆应用程序。我没有看到那样的东西。

你的主要应该是这样的:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {         
        @Override
        public void run() {
            JFrame frame = new JFrame();
            frame.add(new memory());
            frame.setSize(500, 400);
            frame.setVisible(true);             
        }
    });
}
于 2013-10-07T04:19:46.160 回答