簡體   English   中英

Jlabel、Jtextfield、Jbutton 沒有立即顯示在 GUI 中我瘋了

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

我的 GUI 上顯示的元素有問題。 我正在創建一個非常簡單的程序,我要求用戶在三個不同的框中輸入三個不同的整數,並在按下按鈕時將數字相加。 現在,我已經創建了一個按鈕、一個文本字段和一個標簽,但是有一個問題:按鈕、文本字段和標簽沒有出現在 GUI 上。 我必須將鼠標懸停在按鈕區域上才能出現並且與文本字段相同。 即使我將鼠標懸停在應有的位置,標簽也根本不顯示。 發生這種情況的任何原因? 這是我的代碼

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

    }

}

在此先感謝那些審查代碼的人!

調用box.getContentPane().add(panel);

創建面板后。 然后打電話

box.setVisible(true)之后。

您也不應該將布局管理器設置為 null。 將它保留在默認的FlowLayout開始就可以了。

在此處輸入圖片說明

暫無
暫無

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

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