空白的JFrame和没有JPanel出现但已添加

时间:2013-09-07 12:23:19

标签: java swing layout-manager

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

以下是我得到的输出: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 个答案:

答案 0 :(得分:3)

我建议为此任务使用嵌套或复合布局。请参阅来源中的评论中的进一步提示。

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 them 1 ,以及布局填充和& white space 2 的边框。



答案 1 :(得分:1)

frm_main.validate()

的末尾添加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 ******
    }