2013-10-07 24 views
0

我有這個代碼,它可以處理所有事情,直到按下底部中間按鈕。爲什麼按下按鈕號碼8時圖形不起作用?

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(); 
    } 

} 
+0

1-畫有JFrame'的''不... paintComponent'對不起 – MadProgrammer

+0

,但你會如何去做? – user2853125

回答

1

memory延伸JFrame。 Swing不會讓你添加一個窗口到另一個窗口。

您應該避免從頂級容器擴展(這只是原因),而應該使用類似JPanel的東西。

這使您可以添加該組件到任何容器,你喜歡

JFrame沒有paintComponent方法,所以它永遠不會被調用。它編譯的原因是你已經調用了super.paintComponents(注意最後的s)。

相反,你應該從什麼樣延伸和JPanel重寫它的paintComponent方法和執行您的自定義從那裏

+0

你如何重寫paintComponent?對不起,我對這件事真的很陌生... – user2853125

+1

@ user2853125查看更多詳情,請查看[Performing custom painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) – MadProgrammer

+0

ok所以我認爲我修好了,但現在不開放,所以我該怎麼辦?我所做的只是改變它來擴展JPanel並在這行的頂部添加@Override'public void paintComponent(Graphics g){'我該怎麼做?有人請幫忙 – user2853125

相關問題