我的 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());
相关文章