Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTextField没有出现_Java_Swing - Fatal编程技术网

Java JTextField没有出现

Java JTextField没有出现,java,swing,Java,Swing,我正在尝试向JFrame添加状态栏。我试图添加它,但它不会出现在我的JFrame上。调用此命令是因为JFrame显示了exitButton,但状态栏在站点中没有位置!请帮忙 JFrame代码: package AnimalThing; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; public class window { public static JFrame frame;

我正在尝试向JFrame添加状态栏。我试图添加它,但它不会出现在我的JFrame上。调用此命令是因为JFrame显示了exitButton,但状态栏在站点中没有位置!请帮忙

JFrame代码:

package AnimalThing;

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class window {
    public static JFrame frame;
    public static void create(){
        frame = new JFrame("Probe Controller: Exodus I");
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1600, 900);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setForeground(Color.CYAN);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        winComp.Add(frame); 
    }
}
winComp代码:

package AnimalThing;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class winComp {
    public static JTextField statusBar;
    public static JButton exitButton, clickedButton;
    public static void Add(JFrame frame){
            AddExitButton(frame);
            AddstatusBar(frame);
    }

    private static void AddstatusBar(JFrame frame) {
            statusBar = new JTextField(1);
            statusBar.setBounds(500, 500, 100, 100);
            statusBar.setText("Hello");
            frame.add(statusBar);
    }

    public static void AddExitButton(JFrame frame){
        exitButton = new JButton("Exit");
        exitButton.setBounds(1, 1, 100, 33);
        exitButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Exit Button Hit: " + LocalDateTime.now());
                System.exit(0);
            }});
        Font buttonFont = new Font("Thing", Font.BOLD, 16);
        exitButton.setFont(buttonFont);
        exitButton.setFocusPainted( false );
        exitButton.setBackground(Color.BLACK);
        exitButton.setForeground(Color.RED);
        exitButton.setBorderPainted(false);
        frame.add(exitButton);


    }

}

基本上,在添加任何组件之前,您已经调用了
setVisible
。更改
frame.setresizeable(false)
帧。可设置大小(真)
并尝试调整帧的大小,应显示字段。您可以在添加字段后调用
setVisible
,也可以调用
repaint
,这“可能”有用

我建议您避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题

一般来说,
winComp.Add(frame)没有意义,因为它看起来像是要将框架添加到
winComp
中,有点让人困惑。方法的名称可以更改为其他名称,以便更容易理解方法的意图


我还鼓励您查看一下,因为您的代码读起来很混乱

基本上,在添加任何组件之前,您已经调用了
setVisible
。更改
frame.setresizeable(false)
帧。可设置大小(真)
并尝试调整帧的大小,应显示字段。您可以在添加字段后调用
setVisible
,也可以调用
repaint
,这“可能”有用

我建议您避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题

一般来说,
winComp.Add(frame)没有意义,因为它看起来像是要将框架添加到
winComp
中,有点让人困惑。方法的名称可以更改为其他名称,以便更容易理解方法的意图


我还鼓励你看一看,因为你的代码读起来很混乱

你没有将组件添加到框架中,而是将它们添加到
JPanel
中,然后将
JPanel
添加到框架中。你使用
null
布局将再次困扰你,如果你不将组件添加到框架中,就将它们添加到框架中
JPanel
并将
JPanel
添加到框架中。您对
null
布局的使用将再次困扰您的