如何在JPanel,JFrame中移动JButtons和JLabels的位置

我正在创建一个简单的彩票作为练习,我是Java的初学者。 除了一件事,我几乎完成了我想要的一切:将不同的文本和按钮移动到我希望它们的位置。

这是在Photoshop中编辑的图片,描述了我希望它如何:

http://i.gyazo.com/9587eb7e451b90ea0f920c4795a03cd5.png

按“播放”按钮后,彩票目前看起来像这样:

(正如您所看到的那样,按钮会向右移动,以便为按钮左侧的文本留出空间)

在此处输入图像描述

我想要实现的是按钮位置永不移动,并将文本放在按钮下。

我还想添加一个表格来描述您的潜在奖金以及会给您多少奖金的数字。

代码由2个Java类组成,请记住我是初学者,如果你看到任何简单的改进空间(我知道必须有很多),如果你给我一个提示或任何东西,我会很高兴:)

所以这是代码:

LotteryMain.Java

public class LotteryMain { /** **@author Samy */ public static void main(String[] args) { TheLottery n = new TheLottery(); n.TheLottery(); } } 

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  50) { winnings = ("You've won $100!"); } else if (lotteryChance  20) { winnings = ("You've won $500!"); } else if (lotteryChance  5) { winnings = ("You've won $2,000!"); } else if (lotteryChance  1) { winnings = ("You've won $5,000!"); } else if (lotteryChance  0.1) { winnings = ("You've won $25,000!"); } else if (lotteryChance  0.01) { winnings = ("You've won $50,000!"); } else if (lotteryChance  0.001) { winnings = ("You've won $250,000!"); } else if (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("Your number is: "+lotteryChance+" - "+winnings+""); } } 

您可以使用BorderLayout或GridLayout,而不是使用根据其大小和窗口大小自动重新排列项目的FlowLayout。 BorderLayout基本上将窗口分成5个区域,每个边界是一个用基本方向(北,东等)标识的单独区域,中心也是它自己的区域。 GridLayout允许您指定是否需要特定数量的行/列,同时让其他行根据需要进行扩展(例如,n个元素分为2列)。

从您的photoshopped期望结果的外观,您可以使用BorderLayout作为主框架,北部的按钮,中心的数字/结果和南部的表格。 还要记住,您可以将面板放在布局区域中,该面板具有自己的布局,例如北方按钮的GridLayout,或者更好的南方桌面。 希望有所帮助!

一些代码可以帮助您:

 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默认使用FlowLayout一个接一个地添加组件。 您也可以使用多个JPanel并添加另一个“ JPanel ”。

如果要在按钮下方显示文本,请使用BorderLayoutGridBagLayout

值得阅读关于如何使用各种布局管理器的 Swing教程

示例代码:

 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)); 

快照:

在此处输入图像描述