2

Can anyone help me? Whenever I ran the codes below, it always returns a blank frame, I don't know where I did wrong. Can you guys help me debug this? I already added the components to the panel, and the panel to the frame, but still it returns a blank output.

Here is the output I'm getting: enter image description here

While this is what 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();

        }
    }
4

2 回答 2

3

我建议为此任务使用嵌套或复合布局。请参阅源代码中的评论中的更多提示。

SR工具

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

Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或它们的组合1,以及空白区域的布局填充和边框2



于 2013-09-07T13:45:10.600 回答
1

Add frm_main.validate() in the end of start()

public void start(){

   /*
   ...
   Same As Above
   ...
   */

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

        frm_main.validate(); // Add this line ******
    }
于 2013-09-07T12:30:25.587 回答