My interface objects change every time I run it on NetBeans. What for?

I encoded a very simple user interface on NetBeans that has a Frame, a label as a title in the frame, and a panel. The panel will contain labels and text fields. I encoded the first two labels ( Employee IDand ID Number) and text fields ( txtempidand txtidno) in the panel, but now my problem is that every time I run the application to check it, sometimes both shortcuts and text fields, sometimes Employee IDnothing more is displayed , otherwise only tags Employee IDand txtempid, or all buttxtidno, or everything is displayed as usual. Each time I launch the application, the objects in this panel are displayed in one of the ways described above. I checked my coding and there are no errors or warnings and it all seems 100%. What could be the cause of this phenomenon?

The following is the encoding for this interface:

import java.awt.*;
import javax.swing.*;

public class empManUI  
{
    public empManUI()
    { }

    public void layout ()
    {
        JFrame empManipulate = new javax.swing.JFrame();
        empManipulate.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        empManipulate.setSize(720,520);
        empManipulate.setVisible(true);
        JPanel panel = new javax.swing.JPanel();
        panel.setLocation(230, 80);
        panel.setSize(450,310);
        panel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        panel.setLayout(null);
        panel.setVisible(true);
        empManipulate.add(panel);
        empManipulate.setLayout(null);

        JLabel lbltitle = new javax.swing.JLabel();
        lbltitle.setText("Employees");
        lbltitle.setFont(new java.awt.Font("Excelerate", 1, 42));
        lbltitle.setBounds(60, 0, 620, 70);
        lbltitle.setForeground(Color.RED);
        empManipulate.add(lbltitle);
        JLabel lblemp = new javax.swing.JLabel();
        lblemp.setText("Employee ID: ");
        lblemp.setFont(new java.awt.Font("Alexis", 1, 18));
        lblemp.setBounds(20, 30, 170, 13);
        lblemp.setVisible(true);
        panel.add(lblemp);
        JTextField txtempid = new javax.swing.JTextField();
        txtempid.setBounds(200, 20, 230, 30);
        txtempid.setVisible(true);
        panel.add(txtempid);        
        JLabel lblidno = new javax.swing.JLabel();
        lblidno.setText("ID Number: ");
        lblidno.setFont(new java.awt.Font("Alexis", 1, 18));
        lblidno.setBounds(20, 60, 170, 13);
        lblidno.setVisible(true);
        panel.add(lblidno);
        JTextField txtidno = new javax.swing.JTextField();
        txtidno.setBounds(200, 50, 230, 30);
        txtidno.setVisible(true);
        panel.add(txtidno);
    }

    public static void main (String[]args)
    {
        empManUI empUI = new empManUI();
        empUI.layout();
    }
}
+4
source share
1 answer

Try replacing mainas follows:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            empManUI empUI = new empManUI();
            empUI.layout();
        }
    });
}

See if this works better.

Swing . main , . , AWT . javax.swing.SwingUtilities.invokeLater(...) java.awt.EventQueue.invokeLater(...).

+2

All Articles