2014-01-17 79 views
0

我的JPanel沒有調整窗口大小,我至少喜歡它調整大小時移動到中心,JTextArea做得很好,但我無法使用其中的按鈕調整JPanel大小以及。這裏是我的代碼:JFrame沒有重新驗證

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package privatelessontracknetbeans; 


import java.awt.BorderLayout; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 


/** 
* 
* @author Brent C 
*/ 
public class PrivateLessonsTrackerGUI extends javax.swing.JFrame { 

/** 
* Creates new form PrivateLessonsTrackerGUI 
*/ 
public PrivateLessonsTrackerGUI() { 
    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() { 

    jPanel1 = new javax.swing.JPanel(); 
    sun = new javax.swing.JButton(); 
    mon = new javax.swing.JButton(); 
    tue = new javax.swing.JButton(); 
    wed = new javax.swing.JButton(); 
    thu = new javax.swing.JButton(); 
    fri = new javax.swing.JButton(); 
    sat = new javax.swing.JButton(); 
    jScrollPane1 = new javax.swing.JScrollPane(); 
    jTextArea1 = new javax.swing.JTextArea(); 
    jMenuBar1 = new javax.swing.JMenuBar(); 
    jMenu1 = new javax.swing.JMenu(); 
    jMenuItem3 = new javax.swing.JMenuItem(); 
    jMenuItem2 = new javax.swing.JMenuItem(); 
    jMenu2 = new javax.swing.JMenu(); 
    addInstructors = new javax.swing.JMenuItem(); 
    jMenu3 = new javax.swing.JMenu(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setTitle("Private Lessons Tracker"); 

    jPanel1.addComponentListener(new java.awt.event.ComponentAdapter() { 
     public void componentResized(java.awt.event.ComponentEvent evt) { 
      jPanel1ComponentResized(evt); 
     } 
    }); 

    sun.setText("Sun"); 
    sun.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      sunActionPerformed(evt); 
     } 
    }); 

    mon.setText("Mon"); 
    mon.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      monActionPerformed(evt); 
     } 
    }); 

    tue.setText("Tue"); 
    tue.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      tueActionPerformed(evt); 
     } 
    }); 

    wed.setText("Wed"); 
    wed.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      wedActionPerformed(evt); 
     } 
    }); 

    thu.setText("Thu"); 
    thu.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      thuActionPerformed(evt); 
     } 
    }); 

    fri.setText("Fri"); 
    fri.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      friActionPerformed(evt); 
     } 
    }); 

    sat.setText("Sat"); 
    sat.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      satActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(sun, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(mon, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(tue) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(wed) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(thu) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(fri) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(sat) 
      .addContainerGap(29, Short.MAX_VALUE)) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addContainerGap() 
      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(sun) 
       .addComponent(mon) 
       .addComponent(tue) 
       .addComponent(wed) 
       .addComponent(thu) 
       .addComponent(fri) 
       .addComponent(sat)) 
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
    ); 

    jTextArea1.setEditable(false); 
    jTextArea1.setColumns(20); 
    jTextArea1.setLineWrap(true); 
    jTextArea1.setRows(5); 
    jTextArea1.setWrapStyleWord(true); 
    jScrollPane1.setViewportView(jTextArea1); 

    jMenu1.setText("File"); 

    jMenuItem3.setText("Home"); 
    jMenu1.add(jMenuItem3); 

    jMenuItem2.setText("Exit"); 
    jMenuItem2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jMenuItem2ActionPerformed(evt); 
     } 
    }); 
    jMenu1.add(jMenuItem2); 

    jMenuBar1.add(jMenu1); 

    jMenu2.setText("Instructor"); 

    addInstructors.setText("Add Instructor(s)"); 
    addInstructors.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      addInstructorsActionPerformed(evt); 
     } 
    }); 
    jMenu2.add(addInstructors); 

    jMenuBar1.add(jMenu2); 

    jMenu3.setText("Student"); 
    jMenuBar1.add(jMenu3); 

    setJMenuBar(jMenuBar1); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addContainerGap() 
        .addComponent(jScrollPane1)) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(21, 21, 21) 
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) 
      .addContainerGap()) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE) 
      .addContainerGap()) 
    ); 

    pack(); 
    setLocationRelativeTo(null); 
}// </editor-fold>       

private void addInstructorsActionPerformed(java.awt.event.ActionEvent evt) {            
    AddingInstructorsGUI aiGUI = new AddingInstructorsGUI(); 
    aiGUI.setVisible(true); 
}            

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {           
    System.exit(0); 
}           

private void sunActionPerformed(java.awt.event.ActionEvent evt) {          
    jTextArea1 = new JTextArea("Sundays List"); 
    jTextArea1.setEditable(false); 
    jTextArea1.setColumns(20); 
    jTextArea1.setLineWrap(true); 
    jTextArea1.setRows(5); 
    jTextArea1.setWrapStyleWord(true); 
    jScrollPane1.setViewportView(jTextArea1); 
}         

private void monActionPerformed(java.awt.event.ActionEvent evt) {          
    jTextArea1 = new JTextArea("Mondays List"); 
    jTextArea1.setEditable(false); 
    jTextArea1.setColumns(20); 
    jTextArea1.setLineWrap(true); 
    jTextArea1.setRows(5); 
    jTextArea1.setWrapStyleWord(true); 
    jScrollPane1.setViewportView(jTextArea1); 
}         

private void tueActionPerformed(java.awt.event.ActionEvent evt) {          
    jTextArea1 = new JTextArea("Tuesdays List"); 
    jTextArea1.setEditable(false); 
    jTextArea1.setColumns(20); 
    jTextArea1.setLineWrap(true); 
    jTextArea1.setRows(5); 
    jTextArea1.setWrapStyleWord(true); 
    jScrollPane1.setViewportView(jTextArea1); 
}         

private void wedActionPerformed(java.awt.event.ActionEvent evt) {          
    jTextArea1 = new JTextArea("Wednesdays List"); 
    jTextArea1.setEditable(false); 
    jTextArea1.setColumns(20); 
    jTextArea1.setLineWrap(true); 
    jTextArea1.setRows(5); 
    jTextArea1.setWrapStyleWord(true); 
    jScrollPane1.setViewportView(jTextArea1); 
}         

private void thuActionPerformed(java.awt.event.ActionEvent evt) {          
    jTextArea1 = new JTextArea("Thursdays List"); 
    jTextArea1.setEditable(false); 
    jTextArea1.setColumns(20); 
    jTextArea1.setLineWrap(true); 
    jTextArea1.setRows(5); 
    jTextArea1.setWrapStyleWord(true); 
    jScrollPane1.setViewportView(jTextArea1); 
}         

private void friActionPerformed(java.awt.event.ActionEvent evt) {          
    jTextArea1 = new JTextArea("Fridays List"); 
    jTextArea1.setEditable(false); 
    jTextArea1.setColumns(20); 
    jTextArea1.setLineWrap(true); 
    jTextArea1.setRows(5); 
    jTextArea1.setWrapStyleWord(true); 
    jScrollPane1.setViewportView(jTextArea1); 
}         

private void satActionPerformed(java.awt.event.ActionEvent evt) {          
    jTextArea1 = new JTextArea("Saturdays List"); 
    jTextArea1.setEditable(false); 
    jTextArea1.setColumns(20); 
    jTextArea1.setLineWrap(true); 
    jTextArea1.setRows(5); 
    jTextArea1.setWrapStyleWord(true); 
    jScrollPane1.setViewportView(jTextArea1); 
}         

private void jPanel1ComponentResized(java.awt.event.ComponentEvent evt) {           
    this.jPanel1.revalidate(); 
}           


/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      new PrivateLessonsTrackerGUI().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JMenuItem addInstructors; 
private javax.swing.JButton fri; 
private javax.swing.JMenu jMenu1; 
private javax.swing.JMenu jMenu2; 
private javax.swing.JMenu jMenu3; 
private javax.swing.JMenuBar jMenuBar1; 
private javax.swing.JMenuItem jMenuItem2; 
private javax.swing.JMenuItem jMenuItem3; 
private javax.swing.JPanel jPanel1; 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JTextArea jTextArea1; 
private javax.swing.JButton mon; 
private javax.swing.JButton sat; 
private javax.swing.JButton sun; 
private javax.swing.JButton thu; 
private javax.swing.JButton tue; 
private javax.swing.JButton wed; 
// End of variables declaration     

}

這只是我有麻煩類,如果你嘗試運行它,你將有一些錯誤。還有一件事,我不明白需要高信譽才能擁有某些標籤,這是有原因嗎?

回答

0

使用具有居中對齊的FlowLayout代替GroupLayout。

jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER)); 
+0

它的netbeans,不能改變netbeans生成的代碼,我希望有人告訴我如何在netbeans中做到這一點。 – Brent

+0

感謝您的回覆。 – Brent