jpanel背景不会变为白色

时间:2015-06-02 03:23:18

标签: java swing

我已经使用以下代码进行应用,但卡片背景并未显示白色,尽管我已经设置了它。任何人都能说出遗漏的东西吗?

package hospitalinfo;

import java.awt.CardLayout;
import javax.swing.ImageIcon;

public class PatientInfo extends javax.swing.JFrame {

/**
 * Creates new form PatientInfo
 */
public PatientInfo() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */

// <editor-fold defaultstate="collapsed" desc="Generated Code">//GENBEGIN:initComponents
private void initComponents() {

    defaultUiPanel = new javax.swing.JPanel();
    addPatient = new javax.swing.JButton();
    addDoctor = new javax.swing.JButton();
    cardsPanel = new javax.swing.JPanel();
    patientCard = new javax.swing.JDesktopPane();
    crlabel = new javax.swing.JLabel();
    crvalue = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();
    patientName = new javax.swing.JTextField();
    relationSpecifier = new javax.swing.JComboBox();
    relativeName = new javax.swing.JTextField();
    agelabel = new javax.swing.JLabel();
    doblabel = new javax.swing.JLabel();
    jDateChooser1 = new com.toedter.calendar.JDateChooser();
    doctorCard = new javax.swing.JDesktopPane();
    jLabel2 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Hospital Information System");
    setBackground(new java.awt.Color(255, 255, 255));
    setExtendedState(MAXIMIZED_BOTH);
    setIconImage(new ImageIcon(getClass().getResource("/resources/hospital-icon.png")).getImage());
    getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.X_AXIS));

    defaultUiPanel.setBackground(new java.awt.Color(87, 196, 243));
    defaultUiPanel.setPreferredSize(new java.awt.Dimension(400, 100));

    addPatient.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/user-icon.png"))); // NOI18N
    addPatient.setText("Add Patient");
    addPatient.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addPatientActionPerformed(evt);
        }
    });

    addDoctor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/People-Doctor-Male-icon.png"))); // NOI18N
    addDoctor.setText("Add Doctor");
    addDoctor.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addDoctorActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout defaultUiPanelLayout = new javax.swing.GroupLayout(defaultUiPanel);
    defaultUiPanel.setLayout(defaultUiPanelLayout);
    defaultUiPanelLayout.setHorizontalGroup(
        defaultUiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(defaultUiPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(defaultUiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(addPatient, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                .addComponent(addDoctor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap(214, Short.MAX_VALUE))
    );
    defaultUiPanelLayout.setVerticalGroup(
        defaultUiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(defaultUiPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(addPatient)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(addDoctor)
            .addContainerGap(182, Short.MAX_VALUE))
    );

    getContentPane().add(defaultUiPanel);

    cardsPanel.setBackground(new java.awt.Color(255, 255, 255));
    cardsPanel.setLayout(new java.awt.CardLayout());

    patientCard.setBackground(new java.awt.Color(255, 255, 255));
    patientCard.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Patient", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Times New Roman", 0, 24), java.awt.Color.black)); // NOI18N

    crlabel.setText("CR No");
    patientCard.add(crlabel);
    crlabel.setBounds(16, 41, 30, 14);

    crvalue.setText("fd");
    patientCard.add(crvalue);
    crvalue.setBounds(69, 41, 14, 14);

    jLabel1.setText("Name");
    patientCard.add(jLabel1);
    jLabel1.setBounds(16, 64, 27, 14);
    patientCard.add(patientName);
    patientName.setBounds(69, 61, 114, 20);

    relationSpecifier.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "S/o", "D/o", "W/o", "M/o", "F/o", "C/o" }));
    relationSpecifier.setSelectedIndex(1);
    patientCard.add(relationSpecifier);
    relationSpecifier.setBounds(16, 87, 35, 20);

    relativeName.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            relativeNameActionPerformed(evt);
        }
    });
    patientCard.add(relativeName);
    relativeName.setBounds(69, 87, 114, 20);

    agelabel.setText("Age");
    patientCard.add(agelabel);
    agelabel.setBounds(164, 119, 19, 14);

    doblabel.setText("D.O.B");
    patientCard.add(doblabel);
    doblabel.setBounds(16, 119, 29, 14);
    patientCard.add(jDateChooser1);
    jDateChooser1.setBounds(69, 113, 91, 20);

    cardsPanel.add(patientCard, "patientcard");

    doctorCard.setBackground(new java.awt.Color(255, 255, 255));
    doctorCard.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Doctor ", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Times New Roman", 0, 24), new java.awt.Color(0, 0, 0))); // NOI18N

    jLabel2.setText("hello doctor");

    javax.swing.GroupLayout doctorCardLayout = new javax.swing.GroupLayout(doctorCard);
    doctorCard.setLayout(doctorCardLayout);
    doctorCardLayout.setHorizontalGroup(
        doctorCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(doctorCardLayout.createSequentialGroup()
            .addGap(29, 29, 29)
            .addComponent(jLabel2)
            .addContainerGap(31, Short.MAX_VALUE))
    );
    doctorCardLayout.setVerticalGroup(
        doctorCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(doctorCardLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel2)
            .addContainerGap(224, Short.MAX_VALUE))
    );
    doctorCard.setLayer(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

    cardsPanel.add(doctorCard, "doctorcard");

    getContentPane().add(cardsPanel);

    pack();
}// </editor-fold>//GEN-END:initComponents

private void addPatientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPatientActionPerformed
    // TODO add your handling code here:
    ((CardLayout)(cardsPanel.getLayout())).show(cardsPanel, "patientcard");
}//GEN-LAST:event_addPatientActionPerformed

private void addDoctorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDoctorActionPerformed
    // TODO add your handling code here:
    ((CardLayout)(cardsPanel.getLayout())).show(cardsPanel, "doctorcard");

}//GEN-LAST:event_addDoctorActionPerformed

private void relativeNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_relativeNameActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_relativeNameActionPerformed

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(PatientInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(PatientInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(PatientInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(PatientInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new PatientInfo().setVisible(true);
        }
    });
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addDoctor;
private javax.swing.JButton addPatient;
private javax.swing.JLabel agelabel;
private javax.swing.JPanel cardsPanel;
private javax.swing.JLabel crlabel;
private javax.swing.JLabel crvalue;
private javax.swing.JPanel defaultUiPanel;
private javax.swing.JLabel doblabel;
private javax.swing.JDesktopPane doctorCard;
private com.toedter.calendar.JDateChooser jDateChooser1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JDesktopPane patientCard;
private javax.swing.JTextField patientName;
private javax.swing.JComboBox relationSpecifier;
private javax.swing.JTextField relativeName;
// End of variables declaration//GEN-END:variables
}

1 个答案:

答案 0 :(得分:0)

你在JDekstopPane上使用.setBackground是行不通的。在你的情况下,我认为patientCard和doctorCard默认面板颜色与cardsPanel重叠,因此你不会看到cardsPanel的白色背景。

如果您想知道如何更改JDesktopPane的背景颜色,可以看到答案here