[英]JButton doesn't appear until clicked
對於這個程序,JButton 似乎不會顯示,除非您單擊應該是 JButton 的區域; JFrame 啟動空白。 單擊按鈕的那一刻,相應的代碼就會運行,按鈕最終會顯示出來。
如何讓按鈕在啟動程序時顯示出來?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/*
The Amazing BlackJack Advisory Tool by JoshK,HieuV, and AlvinC.
Prepare to be amazed :O
*/
public class BlckJackUI {
//main class, will contain the JFrame of the program, as well as all the buttons.
public static void main(String args[])
{
//Creating the JFrame
JFrame GUI = new JFrame("Blackjack Advisor");
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setSize(1300, 900);
GUI.setContentPane(new JLabel(new ImageIcon("C:\\Users\\Hieu Vo\\Desktop\\Green Background.png")));
GUI.setVisible(true);
// Because each button needs to run through the Math class.
final Math math = new Math();
// The Ace Button:
ImageIcon Ace = new ImageIcon("/Users/computerscience2/Downloads/Ace.jpg");
JButton ace = new JButton(Ace);
ace.setSize(300, 100);
ace.setLocation(100, 100);
ace.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Automatically default the the Ace to 11, and if Bust, Ace becomes 1.
if (math.array.playerhandtotal <= 21)
{
math.cardvalue = math.cardvalue + 11;
}
else
{
math.cardvalue = math.cardvalue + 1;
}
math.array.clicktracker++;
math.calcResult();
JOptionPane.showMessageDialog(null,math.array.result);
}
});
GUI.add(ace);
ImageIcon Two = new ImageIcon("/Users/computerscience2/Downloads/2.jpg");
JButton two = new JButton(Two);
two.setSize(300, 100);
two.setLocation(100, 200);
two.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
/*
This generally repeats throughout the whole class, and the only
thing different is the changing cardvalue. When a button is pressed,
respective cardvalues are added into the playerhand ArrayList, and
totaled up to form playerhandtotal, which is a major factor in
bringing up the advice.
*/
math.cardvalue = math.cardvalue + 2;
math.array.clicktracker++;
math.calcResult();
JOptionPane.showMessageDialog(null,math.array.result);
}
});
GUI.add(two);
等等,等等……只是一堆相同的東西,更多按鈕的編碼方式與 JButton 2 完全相同,但關聯的值不同。
JButton start = new JButton("Start/Reset");
start.setSize(300, 100);
start.setLocation(500,500);
start.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
/*
The start button also acts like a reset button, and the concept is fairly
simple. If we reset all the important values to 0 or "null," then the
program acts as if it was just opened.
*/
Arrays array = new Arrays();
array.playerhand.clear();
array.dealer = 0;
math.array.starttracker++;
math.array.clicktracker = 0;
array.playerhandtotal = 0;
math.cardvalue = 0;
array.result = null;
JOptionPane.showMessageDialog(null,"Please select the card \nthat the dealer is showing :)");
}
});
GUI.add(start);
GUI.setLayout(null);
這一切都在同一個 class 中,我知道布局會更好,但也許有一種方法可以使用我現在擁有的解決此問題?
程序開始時是空白的,因為您在添加組件之前調用了setVisible
。 添加組件后(在構造函數的末尾)調用setVisible
,它應該可以正常工作。 此外,避免絕對定位和調用 set|Preferred|Minimum|MaximumSize 方法為您的組件。 了解如何使用布局管理器。
編寫 GUI.validate(); 將所有組件添加到框架后。 任何時候你添加一些東西到一個框架你必須像這樣驗證它。 否則,您將得到您所描述的行為。
否。問題是由布局引起的。 在將任何內容添加到 JFrame 或 JPanel 之前,您希望它具有絕對位置,您必須 setLayout(null)。 否則,您將一直有意想不到的行為。 經過三個小時的實驗,我自己才弄明白了; 突然間,我將您的帖子與其他不同的主題聯系起來,並且奏效了,但這在 Internet 上還沒有得到很好的解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.