Nothing in my JFrame appears(我的 JFrame 中什么都没有出现)
问题描述
我正在练习使用 GUI.我一直在遵循一组说明,但是当我尝试运行程序时,只会出现一个空框架.框架内看不到任何信息.
I am practicing using GUIs. I have been following a set of instructions however when I try to run the program only an empty frame appears. No information can be seen inside the frame.
这是我的代码:
package practice528;
import java.util.Scanner;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class Practice528
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Rectangle Calculator");
frame.setVisible(true);
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lengthL, widthL, areaL, perimeterL;
lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);
areaL = new JLabel("The area is ", SwingConstants.RIGHT);
perimeterL = new JLabel("The perimeter is ", SwingConstants.RIGHT);
frame.setLayout(new GridLayout(5,2));
System.out.println();
} //end main
} //end class
推荐答案
将组件添加到内容窗格:)
Add the components to the content pane :)
这是一个使用多功能 MigLayout 的建议:
Here is a suggestion using the versatile MigLayout:
JPane panel = (JPanel) frame.getContentPane();
panel.setLayout(new MigLayout("fill, wrap 2", "[right][fill]"));
panel.add(lengthL);
panel.add(new JTextField());
panel.add(widthL);
panel.add(new JTextField());
panel.add(areaL);
panel.add(new JTextField());
panel.add(perimeterL);
panel.add(new JTextField());
这篇关于我的 JFrame 中什么都没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我的 JFrame 中什么都没有出现


基础教程推荐
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java Swing计时器未清除 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01