[英]Java Swing GUI buttons and textfields do not appear.This is a test code containing a Jbutton and a JTextField
这是主要类。首先不显示按钮,但是当您将鼠标悬停在按钮的位置时,它会显示。 但是在任何情况下都不会显示的文本字段不是这种情况。
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);
}
}
试试这个 (最后使用f.setVisible(true)
)。
运行程序后,尝试扩大程序窗口。 然后将出现按钮和文本字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.