gpt4 book ai didi

java - 将文本添加到 JFrame

转载 作者:行者123 更新时间:2023-12-01 12:44:13 26 4
gpt4 key购买 nike

我是java初学者,我使用JFrame制作了一个彩票系统,该系统打开一个窗口,让您单击“玩”按钮来运行彩票。目前,您的奖金和号码显示在控制台中,因为我一直无法找到在 JFrame 中显示文本的方法,尽管我进行了大量搜索,但我总是遇到无法修复的错误,或者根本没有得到任何工作结果。 (如前所述,我是初学者)

我想要实现的基本上是一张表格或一段文本,告诉您可能的奖金以及哪些数字将带来哪些奖金。每次按下“玩”按钮后,还会显示您的号码和奖金的区域。

我并不是在寻找任何 super 花哨的东西,只是在寻找基本有效的东西,因为我是初学者。

这就是现在的样子:

http://i.gyazo.com/1ffda0067a033bd6d5fd92dbf6bc8fed.png

系统由 2 个文件组成,它们如下所示:

LotteryMain.java

public class LotteryMain {
/**
**@author Samy
*/
public static void main(String[] args) {

TheLottery n = new TheLottery();
n.TheLottery();

}
}

TheLottery.java

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

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TheLottery extends JFrame implements ActionListener {
/**
**author Samy
*/
JFrame frame = new JFrame("The Lottery");
JPanel panel = new JPanel(new FlowLayout());
JButton play = new JButton("Play");

private static final long serialVersionUID = 1L;



public void TheLottery() {




int width = 720;
int height = width/16*9;

frame.setVisible(true);
frame.setSize(width,height);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(panel);

panel.add(play);



play.addActionListener(this);


play.setToolTipText("Click me to play the lottery!");




}


@Override
public void actionPerformed(ActionEvent e) {

Object action = e.getSource();

String winnings;
double lotteryChance = Math.random()*100;

if(action == play) {

if (lotteryChance > 50) {
winnings = ("You've won $100!");
} else if (lotteryChance < 50 && lotteryChance > 20) {
winnings = ("You've won $500!");
} else if (lotteryChance < 20 && lotteryChance > 5) {
winnings = ("You've won $2,000!");
} else if (lotteryChance < 5 && lotteryChance > 1) {
winnings = ("You've won $5,000!");
} else if (lotteryChance < 1 && lotteryChance > 0.1) {
winnings = ("You've won $25,000!");
} else if (lotteryChance < 0.1 && lotteryChance > 0.01) {
winnings = ("You've won $50,000!");
} else if (lotteryChance < 0.01 && lotteryChance > 0.001) {
winnings = ("You've won $250,000!");
} else if (lotteryChance < 0.001 && lotteryChance > 0) {
winnings = ("YOU'VE WON THE JACKPOT! $1,000,000!!!");
} else winnings = ("Something went wrong, no winnings this round.");

System.out.println("Your number is: "+lotteryChance);
System.out.println(winnings);

}
}
}

预先感谢您的帮助!

编辑:如何添加一段每次运行彩票时都会更改的文本?

最佳答案

我认为你想要的是 JLabel

JLabels 显示一些由您定义的文本:

String winnings = "You've won $200!";
JLabel label = new JLabel(winnings);
panel.add(label);

然后,每当奖金发生变化时,您都可以像这样更新它:

winnings = "You've won $1,000"; // Winnings changed
label.setText(winnings);

关于java - 将文本添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24841177/

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