0

I have looked for similar questions for hours, but I got no idea about the problem. Hoping somebody could help me.

Here is the class SimulatedWindow apart from the imports:

public class SimulatedWindow extends JFrame {

    private JFrame defFrame = new JFrame();

    SimulatedWindow() {
        windowsInit();
    }

    private void windowsInit() {
        defFrame.setSize(new Dimension(600, 480));
        defFrame.setTitle("Radar Simulate System");
        defFrame.setLayout(null);
        defFrame.setLocation(100, 100);

        DefPanel defPanel = new DefPanel();
        // this.getContentPane().add(defPanel, BorderLayout.CENTER);
        this.add(new DefPanel());
        defFrame.add(defPanel);
        defFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        defFrame.setVisible(true);
    }

    class DefPanel extends JPanel {
        private LeftPanel myLeftPanel;
        private RightPanel myRightPanel;

        public DefPanel() {
            setLayout(null);
            myLeftPanel = new LeftPanel();
            myRightPanel = new RightPanel();

            add(new JButton("Hello World!"));
            add(myLeftPanel);
            add(myRightPanel);
            System.out.println("DefPAnel");
        }
    }


    class LeftPanel extends JPanel {
        private JButton avgSpeedButton = new JButton();
        private JButton trafficColumeButton = new JButton();
        private JLabel avgSpeedLabel = new JLabel();
        private JLabel trifficColumeLabel = new JLabel();
        private JLabel curTimeLabel = new JLabel();

        LeftPanel() {
            setLayout(null);
            this.setBounds(0, 0, this.getSize().width, this.getSize().height);

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            curTimeLabel.setText(df.format(new Date()));
            add(curTimeLabel);

            add(avgSpeedButton);

            System.out.println("LeftPanel");
        }
    }
}

The main function follows:

public class SimulatedWindowTest {

    public static void main(String[] args) {
        SimulatedWindow simulatedWindow = new SimulatedWindow();
    }
}

None of the elements (JButton, JLabel) in the JPanel are displaying.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Wentao Wan
  • 111
  • 1
  • 2
  • 8
  • `this.setBounds(0, 0, this.getSize().width, this.getSize().height);` will leave the panel with a size of 0 (getSize returning its own size, which is 0) .Also, `curTimeLabel` has no size set. Finally, you should avoid `null` layout if you don't really need aboslute positioning. – Arnaud May 10 '16 at 07:53
  • 2
    Use a [LayoutManager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) Luke. `setLayout(null)` is a path to the dark side . – Sergiy Medvynskyy May 10 '16 at 07:54
  • Simply try `setLayout(new FlowLayout())` for starters – Roel Strolenberg May 10 '16 at 07:55
  • 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) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson May 10 '16 at 08:27
  • Thanks very much to all the upper comments!The problem exists in Layout Managers.I forgot I have set JPanel null layout.Finally,I use the FlowLayout to all the components and it works well .Thank you for advice and help me!come on ,starter! – Wentao Wan May 10 '16 at 08:42

1 Answers1

0

I believe this might be your problem: check it

The main answer is:

This is the problem with absolute positioning (or null layout). It requires you to set the sizes of all your components, otherwise they will stay are their default zero-size and won't appear. That's why it's always better to use a layout manager.

Community
  • 1
  • 1
dquijada
  • 1,697
  • 3
  • 14
  • 19
  • *"I believe this might be your problem: check it"* - So, instead of voting to close the question, you've instead decided that rep some reward for someone else's answer? – MadProgrammer May 10 '16 at 09:35