2013-10-07 29 views
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(); 
     }; 



} 
+0

沒有工作意味着什麼?您是否收到任何錯誤?定義不工作 – SpringLearner

+0

當我點擊運行,沒有什麼是它無按鍵沒有一個灰色的框彈出。 – user2853125

回答

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);    
     } 
    }); 
} 
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)面板

您還需要從您的paintComponent方法......這也解釋了爲什麼你有這麼多的問題,刪除setVisible(false);。 ..

相關問題