gpt4 book ai didi

java - JTextField 未出现

转载 作者:行者123 更新时间:2023-12-02 03:57:25 24 4
gpt4 key购买 nike

我正在尝试向我的 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.setResizable(false); 更改为 frame.setResizable(true); 并尝试调整框架大小,字段应该出现。您可以在添加字段后调用 setVisible 或调用 repaint,这“可能”有效

我鼓励您避免使用 null 布局,像素完美布局是现代 UI 设计中的一种幻觉。影响组件个体尺寸的因素太多,您无法控制其中任何一个。 Swing 的设计目的是与核心的布局管理器一起工作,放弃这些将导致无休止的问题和问题,您将花费越来越多的时间来尝试纠正

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

我还鼓励您查看Code Conventions for the Java Programming Language因为你的代码读起来很困惑

关于java - JTextField 未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com