无法在 Java 中将文本字段添加到接口



我不能将这个TextField添加到界面,所产生的窗口是朴素的,没有任何细节,尽管我尝试了很多找到问题所在。代码如下:进口java.awt.ComponentOrientation;进口属性;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class Calculator extends JFrame {
    private JTextField display;
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("Could not load system interfacen");
        }
        new Calculator();
    }
    public Calculator() {
        super("Calculator");
        sendDisplay();
        sendUI(this);
    }
    private void sendDisplay() {
        display = new JTextField("0");
        display.setBounds(10, 10, 324, 50);
        display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        display.setEditable(false);
        display.setFont(new Font("Arial", Font.PLAIN, 30));
        display.setVisible(true);
        add(display);
    }
    private void sendUI(Calculator app) {
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setVisible(true);
        app.setSize(400, 600);
        app.setLayout(null);
        app.setResizable(false);
        app.setLocationRelativeTo(null);
    }
}

如果有人能找到问题所在,我将不胜感激

使setVisible(true)sendUI方法的最后一条语句

private void sendUI(Calculator app) {
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    app.setSize(400,600);
    app.setLayout(null);
    app.setResizable(false);
    app.setLocationRelativeTo(null);
    app.setVisible(true);               
}

良好实践

  • 避免使用绝对定位(空布局)。

最新更新