简体   繁体   English

JButton 直到被点击才出现

[英]JButton doesn't appear until clicked

For this program, the JButton doesn't seem to show up unless you click the area where the JButton should be;对于这个程序,JButton 似乎不会显示,除非您单击应该是 JButton 的区域; the JFrame starts up blank. JFrame 启动空白。 The moment you click the button, the respective code runs and the button finally shows up.单击按钮的那一刻,相应的代码就会运行,按钮最终会显示出来。

How do I get the buttons to show up upon starting the program?如何让按钮在启动程序时显示出来?

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

Et cetera, Et cetera... Just a bunch of the same stuff, more buttons coded the same exact way as JButton two, but with different value associated to them.等等,等等……只是一堆相同的东西,更多按钮的编码方式与 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);

This is all in the same class, and I understand a layout would be nicer, but perhaps there's a way to fix this issue using what I have now?这一切都在同一个 class 中,我知道布局会更好,但也许有一种方法可以使用我现在拥有的解决此问题?

The program starts blank because you are calling setVisible before you add components.程序开始时是空白的,因为您在添加组件之前调用了setVisible Call setVisible after you add components (in the end of contructor) and it should work fine.添加组件后(在构造函数的末尾)调用setVisible ,它应该可以正常工作。 Also, avoid absolute positioning and call of set|Preferred|Minimum|MaximumSize methods for your components.此外,避免绝对定位和调用 set|Preferred|Minimum|MaximumSize 方法为您的组件。 Learn how to use layout managers.了解如何使用布局管理器。

Write GUI.validate();编写 GUI.validate(); after you add all the components to your frame.将所有组件添加到框架后。 Any time you add something to a frame you have to validate it like this.任何时候你添加一些东西到一个框架你必须像这样验证它。 Otherwise, you will get the behavior that you have described.否则,您将得到您所描述的行为。

No. The problem is caused by the layout.否。问题是由布局引起的。 Before adding anything to a JFrame or a JPanel which you want it to have an absolute position, you have to setLayout(null).在将任何内容添加到 JFrame 或 JPanel 之前,您希望它具有绝对位置,您必须 setLayout(null)。 Otherwise, you'll have unexpected behaviours all the time.否则,您将一直有意想不到的行为。 I just figured it out by myself after three hours of experimenting;经过三个小时的实验,我自己才弄明白了; suddenly, I connected your post with other different subject, and it worked, but this isn't well explained on the Internet yet.突然间,我将您的帖子与其他不同的主题联系起来,并且奏效了,但这在 Internet 上还没有得到很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM