1

I can`t add this TextField to the interface, the resulting window is plain without any details on it , altough I tried a lot to find where the problem is.. here is the code: import java.awt.ComponentOrientation; import java.awt.Font;

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 interface\n");
        }
        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);
    }
}

I would be grateful if someone could find the problem

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
  • 3
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) `new Font("Arial",Font.PLAIN,30)` would better be `new Font(Font.SANS_SERIF,Font.PLAIN,30)` for cross platform robustness and compile time checking. – Andrew Thompson Oct 09 '15 at 23:21
  • 3
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Oct 09 '15 at 23:21

1 Answers1

2

Make setVisible(true) is the last statement of your sendUI method

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);               
}

Good Practice

  • Avoid using Absolute Positioning (null layout).
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42