所以我认为我有这个代码可以工作,但它无法正常工作。 我不知道该怎么做,我试着看看每个人都建议的东西,但我只是不知道该怎么做,所以任何帮助都将不胜感激!
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();
};
}
答案 0 :(得分:2)
正如我在last question中所说,您需要将面板添加到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);
}
});
}
花一些时间阅读Creating a GUI With JFC/Swing
您还想删除
final JFrame frame = new JFrame();
final JPanel pan = new JPanel(h);
frame.add(pan);
从构造函数中简单地将组件直接添加到(memory
)面板
您还需要从setVisible(false);
方法中移除paintComponent
...这解释了您遇到这么多问题的原因......
答案 1 :(得分:1)
您必须拥有一个JFrame并为swing应用程序显示它。我没有看到类似的东西。
你的主要应该是这样的:
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);
}
});
}