2012-03-25 10 views
1

Ich versuche, meine Tasten in meiner GUI die gleiche Größe zu haben. Wenn ich PreferredSize() setze, scheint Nimbus jedoch automatisch die Höhe einiger Schaltflächen festzulegen. Was ist der beste Weg, um Nimbus dazu zu bringen, dass alle Knöpfe die gleiche Größe (d. H. Breite und Höhe) haben? Im folgenden Beispielcode ist die Höhe für die Schaltflächen in der ersten Zeile korrekt, die Breite jedoch nicht. In der zweiten Zeile ist die Breite korrekt, die Höhe jedoch nicht. Wie kann ich erreichen, dass alle Tasten in Nimbus die gleiche Größe haben?Java Swing Wie setze ich JButton-Größe in Nimbus?

public class SampleNimbusProblem extends javax.swing.JDialog { 

    /** Creates new form SampleNimbusProblem */ 
    public SampleNimbusProblem(java.awt.Frame parent, boolean modal) { 
     super(parent, modal); 
     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() { 
     java.awt.GridBagConstraints gridBagConstraints; 

     jPanel1 = new javax.swing.JPanel(); 
     okButton = new javax.swing.JButton(); 
     cancelButton = new javax.swing.JButton(); 
     jPanel2 = new javax.swing.JPanel(); 
     okButton1 = new javax.swing.JButton(); 
     cacelButton2 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 
     getContentPane().setLayout(new java.awt.GridBagLayout()); 

     jPanel1.setLayout(new java.awt.GridBagLayout()); 

     okButton.setText("OK"); 
     okButton.setMaximumSize(new java.awt.Dimension(65, 23)); 
     okButton.setMinimumSize(new java.awt.Dimension(65, 23)); 
     okButton.setPreferredSize(new java.awt.Dimension(65, 23)); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); 
     jPanel1.add(okButton, gridBagConstraints); 

     cancelButton.setText("Cancel"); 
     cancelButton.setMaximumSize(new java.awt.Dimension(65, 23)); 
     cancelButton.setMinimumSize(new java.awt.Dimension(65, 23)); 
     cancelButton.setPreferredSize(new java.awt.Dimension(65, 23)); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 1; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); 
     jPanel1.add(cancelButton, gridBagConstraints); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 1; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); 
     getContentPane().add(jPanel1, gridBagConstraints); 

     jPanel2.setLayout(new java.awt.GridBagLayout()); 

     okButton1.setText("OK"); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 
     gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); 
     jPanel2.add(okButton1, gridBagConstraints); 

     cacelButton2.setText("Cancel"); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 1; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); 
     jPanel2.add(cacelButton2, gridBagConstraints); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); 
     getContentPane().add(jPanel2, gridBagConstraints); 

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

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       SampleNimbusProblem dialog = new SampleNimbusProblem(new javax.swing.JFrame(), true); 
       dialog.addWindowListener(new java.awt.event.WindowAdapter() { 
        public void windowClosing(java.awt.event.WindowEvent e) { 
         System.exit(0); 
        } 
       }); 
       dialog.setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JButton cacelButton2; 
    private javax.swing.JButton cancelButton; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JButton okButton; 
    private javax.swing.JButton okButton1; 
    // End of variables declaration 

} 

Antwort

2

Eine Möglichkeit, um die Größe zu standardisieren ist Komponenten in ein GridLayout zu setzen. Weitere Informationen finden Sie unter Laying Out Components Within a Container.


..whenever I(),

BTW setPreferredSize - Es ist selten, wenn überhaupt, erforderlich, eine bevorzugte Größe einzustellen. Lassen Sie den Layout-Manager (mit entsprechenden Padding & Grenzen) das schwere Heben der Berechnung der richtigen Größe der Komponenten über verschiedene Bildschirmauflösungen, Größen, PLAFs und Versionen.

+0

Dank. Obwohl GridLayout mir nicht viel Kontrolle über das Layout gibt, scheint die einzige Lösung die Verwendung eines anderen LayoutManagers zu sein. GridBagConstraints scheint mit JButtons in Nimbus nicht gut zu funktionieren. Ich bin in der Lage, das richtige Layout mit GroupLayout zu bekommen. – meyerjp3

+0

Das 'GridLayout' macht die Tasten gleichmäßig groß und ist eine einfache Möglichkeit, dies zu tun. Wenn Sie in verschiedenen Teilen der Benutzeroberfläche (wie üblich) unterschiedliche Effekte benötigen, suchen Sie nach einem [verschachtelten Layout] (http://stackoverflow.com/a/5630271/418556). Aber sagen Sie, Sie haben bereits ein funktionierendes Layout mit 'GroupLayout'? –

+0

Ja, ich verwende jetzt ein verschachteltes Layout. Es gibt mehrere Panels, die jeweils ihr eigenes Layout haben. Ich habe GridBagConstraint für jedes Panel verwendet, aber ich konnte das Layout nicht direkt in Nimbus verwenden. Der Hauptlayout-Manager für alle Panels in meinem JDialog ist GridbagConstraints. Mein Button Panel verwendet jetzt GroupLayout und es scheint gut zu funktionieren. Danke für alle Vorschläge! – meyerjp3

2

Sie haben recht Nimbus ziemlich buggy (Entwicklung irgendwo in der ersten Quater beendet) und in einigen Fällen für PreferredSize ignored gesetzt, aus diesem Grund, warum es Resizing a Component

1

Hier ist die Art, wie ich das Beispiel wollte erscheinen. Die Verwendung von GroupLayout hat den Trick gemacht. Es erscheint korrekt im Nimbus LAF. Danke nochmal für die Hilfe. Ich denke, ich muss meine Präferenz für GridBagLayout noch einmal überdenken.

public class SampleNimbusProblem extends javax.swing.JDialog { 

/** Creates new form SampleNimbusProblem */ 
public SampleNimbusProblem(java.awt.Frame parent, boolean modal) { 
    super(parent, modal); 
    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() { 
    java.awt.GridBagConstraints gridBagConstraints; 

    jPanel1 = new javax.swing.JPanel(); 
    okButton = new javax.swing.JButton(); 
    cancelButton = new javax.swing.JButton(); 
    okButton1 = new javax.swing.JButton(); 
    cacelButton2 = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 
    getContentPane().setLayout(new java.awt.GridBagLayout()); 

    okButton.setText("OK"); 
    okButton.setMaximumSize(new java.awt.Dimension(65, 23)); 
    okButton.setMinimumSize(new java.awt.Dimension(65, 23)); 
    okButton.setPreferredSize(new java.awt.Dimension(65, 23)); 

    cancelButton.setText("Cancel"); 
    cancelButton.setMaximumSize(new java.awt.Dimension(65, 23)); 
    cancelButton.setMinimumSize(new java.awt.Dimension(65, 23)); 
    cancelButton.setPreferredSize(new java.awt.Dimension(65, 23)); 

    okButton1.setText("OK"); 

    cacelButton2.setText("Cancel"); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addGap(5, 5, 5) 
      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(10, 10, 10) 
        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
        .addComponent(okButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
        .addComponent(cacelButton2)))) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addGap(5, 5, 5) 
      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(cacelButton2) 
       .addComponent(okButton1)) 
      .addContainerGap(102, Short.MAX_VALUE)) 
    ); 

    gridBagConstraints = new java.awt.GridBagConstraints(); 
    gridBagConstraints.gridx = 0; 
    gridBagConstraints.gridy = 1; 
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 
    gridBagConstraints.insets = new java.awt.Insets(10, 125, 112, 125); 
    getContentPane().add(jPanel1, gridBagConstraints); 

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

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      SampleNimbusProblem dialog = new SampleNimbusProblem(new javax.swing.JFrame(), true); 
      dialog.addWindowListener(new java.awt.event.WindowAdapter() { 
       public void windowClosing(java.awt.event.WindowEvent e) { 
        System.exit(0); 
       } 
      }); 
      dialog.setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JButton cacelButton2; 
private javax.swing.JButton cancelButton; 
private javax.swing.JPanel jPanel1; 
private javax.swing.JButton okButton; 
private javax.swing.JButton okButton1; 
// End of variables declaration     

}

Verwandte Themen