2012-05-28 60 views
2

爲什麼標籤不顯示?選擇列表中的項目後在面板上設置標籤

package javaapplication4; 


import java.awt.Color; 
import java.awt.Container; 
import java.awt.FlowLayout; 
import java.awt.GridBagLayout; 
import javax.swing.GroupLayout; 
import javax.swing.GroupLayout.Alignment; 
import javax.swing.JButton; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class settingPanel extends javax.swing.JPanel { 
    static JPanel con = new JPanel(); 
    static JFrame frame = new JFrame(); 
    JLabel jLabel1 = new JLabel(); 
    public JEditorPane htmlvi = new JEditorPane(); 

    /** Creates new form settingPanel */ 
    public settingPanel() { 
     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. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jTabbedPane1 = new javax.swing.JTabbedPane(); 
     jPanel1 = new javax.swing.JPanel(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jList1 = new javax.swing.JList(); 

     jList1.setModel(new javax.swing.AbstractListModel() { 
      String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", 
        "Item 5" }; 

      public int getSize() { 
       return strings.length; 
      } 

      public Object getElementAt(int i) { 
       return strings[i]; 
      } 
     }); 
     jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() { 
      public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 
       jList1ValueChanged(evt); 
      } 
     }); 
     jScrollPane1.setViewportView(jList1); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
       jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
       javax.swing.GroupLayout.Alignment.LEADING).addGroup(
       jPanel1Layout 
         .createSequentialGroup() 
         .addComponent(jScrollPane1, 
           javax.swing.GroupLayout.PREFERRED_SIZE, 70, 
           javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addContainerGap(325, Short.MAX_VALUE))); 
     jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
       javax.swing.GroupLayout.Alignment.LEADING).addComponent(
       jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, 
       Short.MAX_VALUE)); 

     jTabbedPane1.addTab("tab1", jPanel1); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(layout.createParallelGroup(
       javax.swing.GroupLayout.Alignment.LEADING).addComponent(
       jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, 
       Short.MAX_VALUE)); 
     layout.setVerticalGroup(layout.createParallelGroup(
       javax.swing.GroupLayout.Alignment.LEADING).addComponent(
       jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, 
       Short.MAX_VALUE)); 
    }// </editor-fold> 

    private void addpanel() { 
     JPanel pnl = new JPanel(); 
     pnl.setOpaque(false); 
     pnl.setLayout(new GridBagLayout()); 
     pnl.add(new JButton()); 
     pnl.setVisible(true); 
     jPanel1.setLocation(10, 30); 
     jPanel1.add(pnl); 
    } 

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) { 

     JLabel write = new JLabel(); 

     // if(evt.getValueIsAdjusting()){ 
     String read = "<html><h2>" + jList1.getSelectedValue().toString() 
       + "<UL>USB"; 
     addpanel(); 

     jPanel1.add(write); 

    } 

    // Variables declaration - do not modify 
    private javax.swing.JList jList1; 
    public static javax.swing.JPanel jPanel1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTabbedPane jTabbedPane1; 

    // End of variables declaration 
    public static void main(String args[]) { 

     frame.add(new settingPanel()); 
     // jPanel2.setLayout(new GridBagLayout()); 
     // jPanel2.add(new JButton()); 
     // jPanel2.setVisible(false); 
     frame.pack(); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(3); 
    } 
} 

回答

4

您將JLabel添加到jPanel1,該jPanel1使用GroupLayout來顯示其組件。 GroupLayout需要知道每個組件必須放置在垂直和水平組中,並且它不知道將新標籤放在哪裏,因爲您沒有告訴。

我非常懷疑您的最終目標是每次選擇更改時在GUI中添加一個新標籤。在初始化GUI時,您應該添加一個具有默認文本的唯一標籤,並在選擇更改時更改此標籤的文本。

+0

+1一個不錯的和完整的答案!真的沒什麼好補充的! – GingerHead

相關問題