Java 如何在 JPanel、JFrame 中移动 JButtons 和 JLabels 位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24843611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move JButtons and JLabels position in a JPanel, JFrame
提问by Samy
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.
我正在创建一个简单的彩票作为练习,我是Java的初学者。除了一件事之外,我几乎实现了我想要的一切:将不同的文本和按钮移动到我希望它们所处的位置。
Here is a picture edited in Photoshop describing how I would like it to be:
这是一张在 Photoshop 中编辑的图片,描述了我想要的样子:
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.
我还想添加一个表格来描述您的潜在奖金以及哪个数字会给您带来多少奖金。
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 :)
代码由 2 个 Java 类组成,请记住我是初学者,如果您看到任何简单的改进空间(我知道肯定有很多),如果您给我提示或任何东西,我会很高兴:)
So this is the code:
所以这是代码:
LotteryMain.Java
LotteryMain.Java
public class LotteryMain {
/**
**@author Samy
*/
public static void main(String[] args) {
TheLottery n = new TheLottery();
n.TheLottery();
}
}
TheLottery.java
彩票.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 0!");
} else if (lotteryChance < 50 && lotteryChance > 20) {
winnings = ("You've won 0!");
} else if (lotteryChance < 20 && lotteryChance > 5) {
winnings = ("You've won ,000!");
} else if (lotteryChance < 5 && lotteryChance > 1) {
winnings = ("You've won ,000!");
} else if (lotteryChance < 1 && lotteryChance > 0.1) {
winnings = ("You've won ,000!");
} else if (lotteryChance < 0.1 && lotteryChance > 0.01) {
winnings = ("You've won ,000!");
} else if (lotteryChance < 0.01 && lotteryChance > 0.001) {
winnings = ("You've won 0,000!");
} else if (lotteryChance < 0.001 && lotteryChance > 0) {
winnings = ("YOU'VE WON THE HymanPOT OF ,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>");
}
}
采纳答案by Helloworld00
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,而不是使用 FlowLayout,它会根据项目的大小和窗口大小自动重新排列项目。BorderLayout 基本上将窗口分成 5 个区域,每个边框是一个单独的区域,用基本方向(北、东等)标识,中心也是它自己的区域。GridLayout 允许您指定是否需要特定数量的行/列,同时让另一个根据需要扩展(例如,将 n 个元素分成 2 列)。
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!
从您的photoshopped 所需结果的外观来看,您可以为主框架使用BorderLayout,按钮在北,数字/结果在中心,表格在南。还请记住,您可以将面板放置在布局区域中,该面板具有自己的布局,例如用于北方按钮的 GridLayout,或者更好用于南方表格的 GridLayout。希望有帮助!
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);
回答by Braj
What I'm looking to achieve is for the buttons position to never move, and to put the text under the buttons.
我希望实现的是按钮位置永远不会移动,并将文本放在按钮下方。
JPanel
by default use FlowLayout
that adds the component one after another. You can use multiple JPanel
as well and add in another ``JPanel`.
JPanel
默认情况下使用FlowLayout
它一个接一个地添加组件。您也可以使用多个JPanel
并添加另一个“JPanel”。
If you want to display text below the button then use BorderLayout
or GridBagLayout
.
如果要在按钮下方显示文本,请使用BorderLayout
或GridBagLayout
。
It's worth reading Swing Tutorialon How to Use Various Layout Managers
关于如何使用各种布局管理器的Swing 教程值得一读
Sample code:
示例代码:
JPanel topPanel = new JPanel();
topPanel.add(new JButton("Play"));
topPanel.add(new JButton("Exit"));
JPanel panel = new JPanel(new BorderLayout());
panel.add(topPanel, BorderLayout.NORTH);
panel.add(new JLabel("Hello", JLabel.CENTER));
snapshot:
快照: