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



我正在使用Java在Eclipse中编码。 我在.setText();.setVisible();方面遇到了问题。 我试图输入.revalidate();.repaint();方法,但这些似乎没有发挥作用。当您按下"Let's go!"按钮时,它不会删除文本或更改我告诉它的文本。 这是我的代码:

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);
    }
}
您的 Play 类正在更改 static JLabel text 中声明的 JLabel 的文本,但该 JLabel 从未添加到您的框架中,

因为在您的主方法中,您正在声明第二个全新的 JLabel,而是将该 JLabel 添加到框架中:

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

从代码中删除该行,按下按钮时,您将看到文本更改。

最新更新