JAVA:.setText和.setVisible(false)方法不起作用

时间:2014-12-23 01:25:42

标签: java

我使用Java在Eclipse中编码。我在使用.setText();.setVisible();时遇到问题。我尝试使用.revalidate();.repaint();方法,但那些似乎没有完成他们的工作。 当你按下"让我们走!"按钮,它没有删除文本或更改我告诉它的文本。这是我的代码:

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

    }
}

1 个答案:

答案 0 :(得分:2)

您的Play类正在更改static JLabel text中声明的JLabel的文本,但JLabel从未添加到您的框架中,因为在您的main方法中,您宣布第二个全新的JLabel,并且您是将该值添加到帧中:

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

从代码中删除该行,按下按钮后,您将看到文本发生变化。