问题描述
我正在创建一个简单的彩票练习,我是Java的初学者.除了一件事情,我几乎实现了我想要的所有东西:将不同的文本和按钮移动到我希望它们处于的位置.
I am creating a simple lottery as a practice, I am a beginner of Java. I have achieved almost everything that I want apart from one thing: Moving the different texts and buttons to the position that I'd like them to be.
这是在Photoshop中编辑的图片,描述了我想要的样子:
Here is a picture edited in Photoshop describing how I would like it to be:
按下播放"按钮后,彩票当前如下所示:
The Lottery currently looks like this after you press the Play button:
(如您所见,按钮向右移动,以便为压在按钮左侧的文本留出空间)
(As you can see the buttons get shifted to the right in order to make space for the text that is squished in to the left of the buttons)
我要实现的目标是使按钮位置永远不会动,并将文本放置在按钮下方.
What I'm looking to achieve is for the buttons position to never move, and to put the text under the buttons.
我还想添加一张表格,描述您的潜在奖金以及可以给您带来多少奖金的数字.
I would also like to add a table that describes your potential winnings and what number will give you what winnings.
该代码由2个Java类组成,请记住我是一个初学者,如果您看到任何简单的改进空间(我知道肯定有很多地方),如果您给我一些提示或其他内容,我将非常高兴:)
The code consists of 2 Java Classes, and please remember that I am a beginner and if you see any simple room for improvement (I know there must be plenty) I would be glad if you gave me a tips or anything :)
这是代码:
LotteryMain.Java
LotteryMain.Java
public class LotteryMain {
/**
**@author Samy
*/
public static void main(String[] args) {
TheLottery n = new TheLottery();
n.TheLottery();
}
}
TheLottery.java
TheLottery.java
import java.awt.Color;
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;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
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");
JButton exit = new JButton("Exit");
JLabel label = new JLabel();
private static final long serialVersionUID = 1L;
public void TheLottery() {
panel.add(label);
int width = 720;
int height = width/16*9;
frame.setSize(width,height);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.add(panel);
panel.setBackground(new Color(0x222222));
panel.setBorder(new EtchedBorder(new Color(0xAAAAAA),new Color(0x666666)));
panel.add(play);
panel.add(exit);
play.addActionListener(this);
exit.addActionListener(this);
play.setToolTipText("Click me to play the lottery!");
exit.setToolTipText("Click me to exit.");
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object action = e.getSource();
String winnings = null;
double lotteryChance = Math.random()*100;
if(action == play) {
if (lotteryChance > 80) {
winnings = ("You lost! Luckily this lottery is free to play.");
} else if (lotteryChance < 80 && 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 OF $1,000,000!");
} else winnings = ("Something went wrong, no winnings this round.");
System.out.println("Your number is: "+lotteryChance);
System.out.println(winnings);
}
if(action == exit) {
System.exit(1);
}
label.setText("<html><font color='white'>Your number is: "+lotteryChance+" - "+winnings+"</font></html>");
}
}
推荐答案
您可以使用BorderLayout或GridLayout,而不是使用FlowLayout(它会根据项目的大小和窗口大小自动重新排列项目). BorderLayout基本上将窗口划分为5个区域,每个边界是一个用基本方向(北,东等)标识的单独区域,而中心也是它自己的区域. GridLayout允许您指定是否要特定数量的行/列,同时让其他行/列根据需要扩展(例如,将n个元素分成2列).
Instead of using a FlowLayout, which rearranges items automatically based on their sizes and the window size, you can use a BorderLayout or GridLayout. A BorderLayout basically splits the window into 5 areas, each border is a separate area identified with a cardinal direction (north, east, etc.), and the center is it's own area as well. A GridLayout allows you to specify if you want a particular number of rows/columns, while letting the other expand as necessary (n elements into 2 columns, for example).
从购买所需效果的外观看,您可以将BorderLayout用于主框架,其北部的按钮,中间的数字/结果以及南部的表格.还要记住,您可以将面板放置在布局区域中,并使该面板具有自己的布局,例如用于北部按钮的GridLayout,或者用于南部表格的更好的布局.希望有帮助!
From the look of your photoshopped desired outcome, you can use a BorderLayout for the main frame, with the buttons in the north, the number/results in the center, and the table in the south. Also remember that you can put a panel in a layout area, with that panel having its own layout, such as a GridLayout for the buttons in the north, or even better, for the table in the south. Hope that helps!
一些可以帮助您的代码:
Some code to assist you:
JFrame frame = new JFrame("The Lottery");
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel northPanel = new JPanel();
JButton play = new JButton("Play");
JButton exit = new JButton("Exit");
northPanel.add(play);
northPanel.add(exit);
mainPanel.add(northPanel, BorderLayout.NORTH);
这篇关于如何在JPanel,JFrame中移动JButton和JLabels位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!