有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java空白JFrame,没有出现JPanel,但已经添加

有人能帮我吗?每当我运行下面的代码时,它总是返回一个空白帧,我不知道我哪里做错了。你们能帮我调试一下吗?我已经将组件添加到面板,将面板添加到框架,但它仍然返回空白输出

这是我得到的输出:enter image description here

而这正是我们所需要的

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

        }
    }

共 (2) 个答案

  1. # 1 楼答案

    start()的末尾添加frm_main.validate()

    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 ******
        }
    
  2. # 2 楼答案

    我建议使用嵌套或复合布局来完成此任务。请参阅源代码注释中的更多提示

    SRTool

    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组织组件,请使用布局管理器或combinations of them1,以及布局填充&;{a2}2的边框