2014-12-23 72 views
3

我使用Java編寫Eclipse。我遇到了.setText();.setVisible();的問題。我試圖輸入.revalidate();.repaint();方法,但這些方法似乎沒有完成他們的工作。 當你按下「我們走吧!」按鈕,它不會擺脫文本或更改我告訴它的文本。這裏是我的代碼:JAVA:.setText和.setVisible(false)方法不起作用

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

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 



public class Jblackjack { 
// Creating static variables 

    static JButton card1 = new JButton(" "); 
    static JButton card2 = new JButton(" "); 
    static JButton card3 = new JButton(" "); 
    static JButton card4 = new JButton(" "); 
    static JButton compcard1 = new JButton(" "); 
    static JButton compcard2 =new JButton(" "); 
    static JButton compcard3 = new JButton(" "); 
    static JFrame frame = new JFrame("BlackJack"); 
    static JButton no = new JButton("Cancel"); 
    static JButton button = new JButton("Let's go!"); 
    static JButton hit = new JButton("hit"); 
    static JButton stand = new JButton("Stand"); 
    static JLabel text = new JLabel("We are going to play Blackjack."); 
    static JLabel text2 = new JLabel("This Blackjack does not have any splits, doubles, surrenders, or insurance. Just hits and stands."); 
    static JLabel text3 = new JLabel("This is a WIP"); 
    static JLabel text4 = new JLabel("11's are Jacks. 12's are Queens. 13's are Kings. 1's are Ones for now."); 
    static JLabel text5 = new JLabel("Are you ready?"); 



    static class Cancel implements ActionListener{ 
     public void actionPerformed (ActionEvent e){ 
      System.exit(0); 
     } 
    } 

    static class Play implements ActionListener{ 
     public void actionPerformed (ActionEvent e){ 

      text.setText("You have:"); 
      text2.setVisible(false); 
      text3.setVisible(false); 
      text4.setVisible(false); 
      text5.setVisible(false); 
      no.setVisible(false); 
      button.setVisible(false); 
      frame.add(card1); 
      text.setText("You have:"); 
      int a = (int)(Math.random()*4); 
      int b = (int)(Math.random()*13); 
      if(a==1){ 
       card1.setText(b + "Clubs"); 
      } else if (a==2){ 
       card1.setText(b + "Spades"); 
      } else if(a==3){ 
       card1.setText(b + "Hearts"); 
      } else if(a==4){ 
       card1.setText(b + "Hearts"); 
      } 
      frame.revalidate(); 
      frame.repaint(); 
     } 
    } 

    static class Hit implements ActionListener{ 
     public void actionPerformed (ActionEvent e){ 

     } 
    } 
    static class Hit2 implements ActionListener{ 
     public void actionPerformed (ActionEvent e){ 

     } 
    } 

    static class Hit3 implements ActionListener{ 
     public void actionPerformed (ActionEvent e){ 

     } 
    } 
    static class Stand implements ActionListener{ 
     public void actionPerformed (ActionEvent e){ 

     } 
    } 
    static class Stand2 implements ActionListener{ 
     public void actionPerformed (ActionEvent e){ 

     } 
    } 
    static class Stand3 implements ActionListener{ 
     public void actionPerformed (ActionEvent e){ 

     } 
    } 
    public static void main(String[] args){ 
     frame.setLayout(null); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 

     // Creating "JVariables" 
     JLabel text = new JLabel("We are going to play Blackjack."); 
     JLabel text2 = new JLabel("This Blackjack does not have any splits, doubles, surrenders, or insurance. Just hits and stands."); 
     JLabel text3 = new JLabel("This is a WIP"); 
     JLabel text4 = new JLabel("11's are Jacks. 12's are Queens. 13's are Kings. 1's are Ones for now."); 
     JLabel text5 = new JLabel("Are you ready?"); 
     JLabel background = new JLabel(new ImageIcon("BlackJackBackGround.png")); 

     // Adding JVariables and setting bounds 
     card1.setBounds(20, 50, 100, 150); 
     hit.setBounds(10, 300, 150, 50); 
     stand.setBounds(170, 300, 150, 50); 
     frame.add(background); 
     frame.add(text); 
     text.setBounds(5, 0, 250, 50); 
     frame.add(text2); 
     text2.setBounds(5, 25, 550, 50); 
     frame.add(text3); 
     text3.setBounds(5, 50, 150, 50); 
     frame.add(text4); 
     text4.setBounds(5, 75, 550, 50); 
     frame.add(text5); 
     text5.setBounds(5, 100, 150, 50); 
     frame.add(no); 
     no.setBounds(10, 150, 150, 50); 
     no.addActionListener(new Cancel()); 
     frame.add(button); 
     button.setBounds(170, 150, 150, 50); 
     button.addActionListener(new Play()); 
     frame.setSize(1000, 700); 

    } 
} 
+0

您應該繪製一個重繪方法 – jgr208

+0

什麼是重繪方法? – CadeLikesToCode

回答

2

您的Play類是改變的JLabel的文本static JLabel text聲明,但是一個JLabel從未被添加到您的框架,因爲在你的主要方法,您聲明第二,品牌新JLabel的,和您添加的一個框架,而不是:

JLabel text = new JLabel("We are going to play Blackjack."); 

從您的代碼刪除線,你將看到的文字變化按下按鈕時。

相關問題