简体   繁体   English

Java Swing GUI按钮和文本字段不会出现。这是一个包含Jbutton和JTextField的测试代码

[英]Java Swing GUI buttons and textfields do not appear.This is a test code containing a Jbutton and a JTextField

this is the main class.button first don't show up first but when you hover the the mouse over the position of button it shows up. 这是主要类。首先不显示按钮,但是当您将鼠标悬停在按钮的位置时,它会显示。 but this is not the case with the textfield which is not showing up in any case. 但是在任何情况下都不会显示的文本字段不是这种情况。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test {
    public static void main(String[] args){

        JFrame f=new JFrame("Calculator");
        f.setSize(450,450);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        JPanel p=new JPanel();
        f.add(p);

        JTextField t=new JTextField(20);
        t.setBounds(5, 5, 0, 0);
        t.setLayout(null);
        p.add(t);

        JButton clear=new JButton("C");        
        clear.setBounds(5,100,50,50);
        clear.setSize(50,40);
        p.add(clear); 

    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test {
    public static void main(String[] args){

        JFrame f=new JFrame("Calculator");
        f.setSize(450,450);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p=new JPanel();
        f.add(p);

        JTextField t=new JTextField(20);
        t.setBounds(5, 5, 0, 0);
        t.setLayout(null);
        p.add(t);

        JButton clear=new JButton("C");        
        clear.setBounds(5,100,50,50);
        clear.setSize(50,40);
        p.add(clear); 
        f.setVisible(true);

    }
}

Try this (use f.setVisible(true) last). 试试这个 (最后使用f.setVisible(true) )。

Try enlarging the program window once you run the program. 运行程序后,尝试扩大程序窗口。 The button and text field should appear then. 然后将出现按钮和文本字段。

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

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