Blank JFrame and JPanel not showing, but added


ZaCk Dela Cruz

who can help me? Whenever I run the code below it always returns blank frames, I don't know where I am doing wrong. Can you guys help me debug it? I've added the component to the panel, and the panel to the frame, but it still returns blank output.

This is the output I get:enter image description here

Although this is required.

desired output

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.ButtonGroup;
    import javax.swing.BorderFactory;
    import javax.swing.UIManager;
    import javax.swing.BoxLayout;
    import java.awt.GridLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JRadioButton;
    /**
     *
     * @author Chareux
     */

    //Declaring Variables



    public class TestUI {

        private JFrame frm_main;
        private JPanel sr_pnl;
        private JLabel sr_lbl;
        private JLabel sr_lbl2;
        private JLabel ret_optn_lbl;
        private JLabel ret_rsn_lbl;
        private ButtonGroup ret_ops;
        private JTextField sr_txtnum;
        private JTextField sr_ret_txtrsn;
        private JButton sr_start;
        private JRadioButton ret_optn_rdbn_y;
        private JRadioButton ret_optn_rdbn_n;


        public TestUI(){
            start();
        }

        public void start(){

//Creating the JFrame
            frm_main = new JFrame("Service Desk SR Tool");
            frm_main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm_main.setSize(500,450);
            frm_main.setLocationRelativeTo(null);
            frm_main.setResizable(false);
            frm_main.setVisible(true);

            // the Panel
            sr_pnl = new JPanel();

            //Components
            sr_lbl = new JLabel("SERVICE DESK SR TIMER!");
            sr_lbl2 = new JLabel("SR number: ");
            sr_txtnum = new JTextField("Enter SR number here..",20);
            ret_optn_lbl = new JLabel("Returning Ticket?");
            ret_optn_rdbn_y = new JRadioButton("Yes");
            ret_optn_rdbn_n = new JRadioButton("No");
            ret_rsn_lbl = new JLabel("Reason: ");
            sr_ret_txtrsn = new JTextField("Enter Reason number here..",20);
            sr_start = new JButton("START!");

            //adding the Components to the panel
            sr_pnl.add(sr_lbl);
            sr_pnl.add(sr_lbl2);
            sr_pnl.add(sr_txtnum);
            sr_pnl.add(ret_optn_lbl); 
            sr_pnl.add(ret_optn_rdbn_y);
            sr_pnl.add(ret_optn_rdbn_n);
            sr_pnl.add(ret_rsn_lbl);
            sr_pnl.add(sr_ret_txtrsn);
            sr_pnl.add(sr_start);



            frm_main.add(sr_pnl,BorderLayout.CENTER);

            //ButtonGroup for the radio button
            ret_ops = new ButtonGroup();
            ret_ops.add(ret_optn_rdbn_y);
            ret_ops.add(ret_optn_rdbn_n);
        }

        public static void main(String[] args) {

         new TestUI();

        }
    }
Andrew Thompson

I recommend using nested or compound layouts for this task. See other tips in the comments in the source code.

SR tool

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

public class SRTool {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new GridLayout(0,1,6,6));
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));

                // show the BG
                gui.setBackground(Color.CYAN);
                // center the label text
                gui.add(new JLabel(
                        "Service Desk SR Tool", SwingConstants.CENTER));
                // create a lyout that can center multiple components
                FlowLayout layout = new FlowLayout(FlowLayout.CENTER,5,5);
                JPanel srPanel = new JPanel(layout);
                gui.add(srPanel);
                srPanel.add(new JLabel("SR:"));
                srPanel.add(new JTextField(8));

                JPanel returnTicketPanel = new JPanel(layout);
                gui.add(returnTicketPanel);
                returnTicketPanel.add(new JLabel("Returning Ticket?"));
                returnTicketPanel.add(new JCheckBox());

                JPanel reasonPanel = new JPanel(layout);
                gui.add(reasonPanel);
                reasonPanel.add(new JLabel("Reason:"));
                reasonPanel.add(new JTextField(14));

                JPanel buttonPanel = new JPanel(layout);
                gui.add(buttonPanel);
                buttonPanel.add(new JButton("Start!"));

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

A Java GUI may have to run on multiple platforms, with different screen resolutions and using different PLAFs. Therefore, they are not conducive to precise placement of components. To organize components for a robust GUI, use a layout manager or a combination of them instead 1 , and blank 2 for layout padding and borders .



Related