简体   繁体   English

Jlabel、Jtextfield、Jbutton 没有立即显示在 GUI 中我疯了

[英]Jlabel, Jtextfield, Jbutton do not immediately show up in the GUI I mad

I have a problem with elements showing up on my GUI.我的 GUI 上显示的元素有问题。 I am creating a very simple program where I ask the user to input three different integers in three different boxes and a button to add the numbers together when it is pressed.我正在创建一个非常简单的程序,我要求用户在三个不同的框中输入三个不同的整数,并在按下按钮时将数字相加。 Right now, I have created a button, a textfield, and a label but there's a problem: The button, textfield, and label does not appear on the GUI.现在,我已经创建了一个按钮、一个文本字段和一个标签,但是有一个问题:按钮、文本字段和标签没有出现在 GUI 上。 I have to hover over the button area for it to appear and same with the textfield.我必须将鼠标悬停在按钮区域上才能出现并且与文本字段相同。 The label doesn't show up at all even when I hover where it's supposed to be.即使我将鼠标悬停在应有的位置,标签也根本不显示。 Any reason why this is happening?发生这种情况的任何原因? Here's my code这是我的代码

import javax.swing.*;

public class ButtonPractice extends javax.swing.JFrame
{

    public static void main(String[] args)
    {

        JFrame box = new JFrame("Simple addition");
        box.setVisible(true);
        box.setSize(500,500);
        box.setResizable(false);
        box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        box.add(panel);

        JTextField inputNumOne = new JTextField("Integer here");
        inputNumOne.setBounds(225,50,200,30);
        panel.add(inputNumOne);

        JLabel labelNumOne = new JLabel("First integer");
        labelNumOne.setBounds(100,50,150,50);
        panel.add(labelNumOne);


        JButton combiner = new JButton("Concactentate");
        //positioning of the button on the panel
        combiner.setBounds(175,300,125,50);

        panel.add(combiner);

    }

}

Thanks in advance to those who review the code!在此先感谢那些审查代码的人!

Call box.getContentPane().add(panel);调用box.getContentPane().add(panel);

after you've created your panel.创建面板后。 Then call然后打电话

box.setVisible(true) after that. box.setVisible(true)之后。

You should also not set your layout manager to null.您也不应该将布局管理器设置为 null。 Leaving it at the default FlowLayout is ok to start with.将它保留在默认的FlowLayout开始就可以了。

在此处输入图片说明

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

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