简体   繁体   中英

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

I have a problem with elements showing up on my 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. 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);

after you've created your panel. Then call

box.setVisible(true) after that.

You should also not set your layout manager to null. Leaving it at the default FlowLayout is ok to start with.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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