JAVA: .setText and .setVisible (spurious) methods not working

I am coding in Eclipse from Java. I am having problems with .setText();

and .setVisible();

. I tried using the methods .revalidate();

and .repaint();

, but they didn't seem to do the job. When you click "Let go!" It doesn't get rid of the text or change the text I told him about. Here is my code:

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 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 are Jacks.  12 are Queens.  13 are Kings.  1 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 are Jacks.  12 are Queens.  13 are Kings.  1 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);

    }
}

      

+3


source to share


1 answer


The Play class will change the text of the JLabel declared in static JLabel text

, but the JLabel was never added to your frame because in your main method, you declare a second, new JLabel, and add that one instead of the frame:

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

      



Remove this line from your code and you will see the text change when the button is clicked.

+2


source







All Articles