繁体   English   中英

JTabbedPane在GUI中显示空白

[英]JTabbedPane showing Blank in GUI

我已经有了一些Java GUI代码,但后来我决定将整个内容包装在JTabbedPane中,因为我还有其他GUI内容可以放在其他标签中。

我已经按照这里教程 ,但是当我运行我的GUI时,我看到的只是一个空白的JFrame!

在此输入图像描述

如何才能显示我的JTabbedPane? 这是GUI初始化代码:

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

您永远不会将您的contentTabs组件添加到任何内容。 解决方案:将其添加到contentPane。

下次遇到类似的问题时,请认真考虑创建和发布sscce,因为它极大地简化了我们分析和解决问题的能力。 你已经发布了大量的代码,其中大部分与手头的问题完全无关,但却很难让我们更难理解这个问题。

你需要实际添加JTabbedPane到框架,以便它显示...据我所知,你从来没有实际将它添加到框架。 尝试:

contentPane.add(contentTabs);

此外,可能没有必要contentTabs.setVisible(true); 最后那里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM