我已经有了一些Java GUI代码,但后来我决定将整个内容包装在JTabbedPane中,因为我还有其他GUI内容可以放在其他标签中。
我已经关注了tutorials here,但是当我运行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);
}
答案 0 :(得分:2)
您永远不会将您的contentTabs组件添加到任何内容。解决方案:将其添加到contentPane。
下次遇到类似问题时,请认真考虑创建和发布sscce,因为它极大地简化了我们分析和解决问题的能力。你发布了一个很多的代码,其中大部分都与手头的问题完全无关,并且很少让我们更难理解这个问题。
答案 1 :(得分:1)
你需要实际将JTabbedPane添加到帧中,以便它显示...据我所知,你从未真正将它添加到帧中。尝试:
contentPane.add(contentTabs);
此外,最后可能不需要contentTabs.setVisible(true);
。