gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:07 24 4
gpt4 key购买 nike

我正在使用 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);

}
}

最佳答案

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

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

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

关于JAVA:.setText 和 .setVisible(false) 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27612882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com