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

时间:2014-07-19 18:25:37

标签: java swing jbutton jlabel layout-manager

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

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

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

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

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

enter image description here

我希望实现的目标是让按钮位置永不移动,并将文字放在按钮下。

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

代码由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 < 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>");

}
}

2 个答案:

答案 0 :(得分:1)

  

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

JPanel默认使用FlowLayout一个接一个地添加组件。您也可以使用多个JPanel并添加另一个``JPanel`。

如果您想在按钮下方显示文字,请使用BorderLayoutGridBagLayout

值得在How to Use Various Layout Managers

上阅读 Swing Tutorial

示例代码:

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

快照:

enter image description here

答案 1 :(得分:1)

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

从你的照片所需的结果看,你可以使用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);