1

I had some Java GUI code already, but then decided I wanted to wrap the whole thing in a JTabbedPane, as I have other GUI stuff that will go in other tabs.

I have followed the tutorials here, but when I run my GUI, all I see is a blank JFrame!

enter image description here

How can I get my JTabbedPane to show? Here is the GUI initialization code:

/**
 * Create the frame.
 * @param The RMC.
 */
public RMCGUI(final RMCServant r0) {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Regional Monitoring Centre " + "[" + r0.rmcid() + "]");
    lmsPanels = new HashMap<String, LMSPanel>();
    rmc = r0;
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(15, 15, 5, 15));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout(0, 0));
    JTabbedPane contentTabs = new JTabbedPane();
    ImageIcon monitorTab = createImageIcon("/images/panel.png", "An Icon of a generic panel.");

    final JPanel geoCoveragePanel = new JPanel();
    JScrollPane jspane = new JScrollPane(geoCoveragePanel);
    jspane.setBorder(BorderFactory.createEmptyBorder());
    contentPane.add(jspane);
    geoCoveragePanel.setLayout(new GridLayout(1, 1, 5, 5));
    geoCoveragePanel.setBorder(new TitledBorder("Geographical Coverage"));
    final JPanel paddedPanel = new JPanel(new GridLayout(0, 2, 5, 5));
    paddedPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    geoCoveragePanel.add(paddedPanel);

    JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
    contentPane.add(buttonsPanel, BorderLayout.SOUTH);

    JButton btnRegisterLms = new JButton("Register LMS");
    btnRegisterLms.setFocusPainted(false);
    final JButton btnRegisterSensor = new JButton("Register Sensor");
    btnRegisterSensor.setVisible(false);
    btnRegisterSensor.setFocusPainted(false);
    buttonsPanel.add(btnRegisterLms);
    buttonsPanel.add(btnRegisterSensor);

    btnRegisterLms.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            String lmsid = JOptionPane.showInputDialog("Please enter the LMS ID name below.");
            System.out.println(lmsid);
            if(lmsid != null)
            {
                lmsid = lmsid.toUpperCase();
                if(rmc.lmsExists(lmsid))
                {
                    rmc.registerLMS(lmsid);
                    paddedPanel.add(createNewLMSPanel(lmsid));
                    //SwingUtilities.getWindowAncestor(paddedPanel).pack();

                    if(lmsPanels.size() != 0)
                    {
                        btnRegisterSensor.setVisible(true);
                    }
                    paddedPanel.revalidate();
                } else {
                    JOptionPane.showMessageDialog(paddedPanel, "Unable to Find LMS Device with ID: " + lmsid);
                }
            }
        }
    });

    btnRegisterSensor.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField lmsField = new JTextField(5);
            JTextField zoneField = new JTextField(5);
            JTextField sensorField = new JTextField(5);
            JPanel inputPanel = new JPanel();
            inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
            inputPanel.add(new JLabel("LMS ID:"));
            inputPanel.add(lmsField);
            inputPanel.add(new JLabel("Zone No:"));
            inputPanel.add(zoneField);
            inputPanel.add(new JLabel("Sensor ID:"));
            inputPanel.add(sensorField);
            lmsField.requestFocusInWindow();

            int result = JOptionPane.showConfirmDialog(geoCoveragePanel, inputPanel, "Please enter the ID of the LMS you want the Sensor added to, along with the Sensor ID", JOptionPane.OK_CANCEL_OPTION);

            if (result == JOptionPane.OK_OPTION) {
                String lmsid = lmsField.getText().toUpperCase();
                String zoneid = zoneField.getText().toUpperCase();
                String sensorid = sensorField.getText().toUpperCase();
                System.out.println("lmsField value: " + lmsid);
                System.out.println("zoneField value: " + zoneid);
                System.out.println("sensorField value: " + sensorid);
                try {
                    if(rmc.registerSensorWithLMS(lmsid, zoneid, sensorid))
                    {
                        System.out.println("Successfully registered the Sensor.");
                        lmsPanels.get(lmsid).justAddedLbl(zoneid, sensorid);

                        if(rmc.hasZoneTwoSensors(lmsid, zoneid))
                        {
                            lmsPanels.get(lmsid).removeAll();
                            lmsPanels.get(lmsid).addZonePanel(zoneid);
                            revalidate();
                        }
                    }
                } catch (AlreadyHasTwoSensors e1) {
                    JOptionPane.showMessageDialog(geoCoveragePanel, "Sorry, Only 2 Sensor devices per zone are allowed.");
                } catch (ZoneAlreadyExists e2) {
                    JOptionPane.showMessageDialog(geoCoveragePanel, "Sorry, That zone already exists for this LMS.");
                }
            }
        }
    });

    contentTabs.addTab("Monitor", monitorTab, contentPane, "Monitor Zones");
    contentTabs.setVisible(true);
}
4

2 回答 2

2

You never add your contentTabs component to anything. Solution: add it to the contentPane.

And next time you have a similar problem, seriously consider creating and posting an sscce as it greatly simplifies our ability to analyze and solve your problem. You've posted a lot of code, and most of it is completely unrelated to the problem at hand and does little but make it harder for us to understand the problem.

于 2013-04-21T00:32:10.877 回答
1

You need to actually add the JTabbedPane to the frame, in order for it to show... As far as I can see, you never actually add it to the frame. Try:

contentPane.add(contentTabs);

Also, there is probably no need to contentTabs.setVisible(true); at the end there.

于 2013-04-21T00:32:41.147 回答