簡體   English   中英

如何在JPanel,JFrame中移動JButton和JLabels位置

[英]How to move JButtons and JLabels position in a JPanel, JFrame

我正在創建一個簡單的彩票實踐,我是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 < 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>");

}
}

我要實現的目標是使按鈕位置永不移動,並將文本放置在按鈕下方。

默認情況下, 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));

快照:

在此處輸入圖片說明

您可以使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM