如何在Jpanel中获得不同的行高度 - Swing Java

时间:2016-04-04 05:04:52

标签: java swing jpanel

我正在使用Swing for GUI开发一个用于PC的Java应用程序。 当我需要一个Jpanel时,我使用自己的java类“DialogPanel”;这个类的代码在这篇文章的末尾......

现在我必须在我的jpanel里放几个组件;我的jpanel将作为一个包含单列的表,其中每个组件都填充一行。

代码是:

JScrollPane myJScrollPane2 = new JScrollPane(myJList1); //myJlist is a JList object containing strings
JScrollPane myJScrollPane3 = new JScrollPane(myJList2);
JScrollPane myJScrollPane4 = new JScrollPane(myJList3);

    dialogPanel = new DialogPanel(1, GridBagConstraints.BOTH, 1.0 , 1.0  ); //1=1 colonna

    dialogPanel.addRow( "Title 1"   ); //row containing a title
    dialogPanel.addRow( myJComboBox1 ); //row containing a Combo box with several items

    dialogPanel.addRow( "Title 2"   ); 
    dialogPanel.addRow( myJScrollPane2 ) ; //row containing a JScrollPane

    dialogPanel.addRow( "Title 3"   ); 
    dialogPanel.addRow( myJScrollPane3 );

    dialogPanel.addRow( "Title 4"   ); 
    dialogPanel.addRow( myJScrollPane4 );

    add(dialogPanel, BorderLayout.CENTER);

我的目标是获得不同的行高度,以便以合适的方式分配垂直空间: - 包含myJComboBox1和4个标题的行应仅占用包装内容所需的最小空间。 - 包含3 myJScrollPane的行应尽可能占用剩余空间(每个空间占33%的空间)

我无法得到它,我将所有垂直空间分为8个部分;因此,组合框和4个标题有太多的空间(在无用的情况下),而3 myJScrollPane,根据需要不那么大

DIALOGPANEL

package net.gui;

import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComponent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

/**
 * Helps constructing nice dialog panel and windows providing easy framework.
 * Extends <code>JPanel</code> so can be used anywhere where <code>JPanel</code>
 * can be. Makes use of <code>GridBagConstraints</code> divided to max two
 * columns surrounded with a thin border around all components to provide nice
 * look to end-user. Consists of several methods <code>addRow</code> to provide
 * easy framework to developers. Thanks to this class you have identical layout
 * for all app dialog windows as well as easy way to modify them.

 * Modificata da Fausto
 */
public class DialogPanel extends JPanel {

    private static final String myClassName="DialogPanel" ; 



  //__________________________________________________________________________________
  // ATTRIBUTI
  //__________________________________________________________________________________
  private final static int spazio_dal_bordo = 3 ;
  private final static int posizionamento= GridBagConstraints.CENTER; //CENTER: Put the component in the center of its display are;  NORTHWEST: Put the component at the top-left corner of its display area; ecc... 
  private final static Insets myInsets = new Insets(spazio_dal_bordo, spazio_dal_bordo, spazio_dal_bordo, spazio_dal_bordo); //An Insets object is a representation of the borders of a container. It specifies the space that a container must leave at each of its edges
  private final static int myEmptyBorder = 5 ;
  private final static int max_num_colonne = 5 ; //numero massimo di componenti in ogni riga

  private int numColumn; //numero colonne impostate dal costruttore
  private GridBagLayout myGridBagLayout;

  /**
   * Constraints for first column (if second one is used). Is assumed that all
   * first columns (if second ones are used) contains label for a component
   * located on its right and have all the same constraints.

   * FAU: Constraints for 2°,3°,4°,5° column used sometimes. It could be a component like
   * a text field or comboBox. It could be even another <code>JPanel</code> if
   * you want. Every last component in a row uses the same constraints.
  */

  private GridBagConstraints columnConstraints[] = new GridBagConstraints[max_num_colonne] ; 




  //__________________________________________________________________________________
  //   COSTRUTTORE x righe con n colonne, dove deve essere 1 <= numColumn <= 5
  //  - La 1° colonna generalmente si usa per la label; 
  //  - Se numColumn=1 si vuole una sola colonna fatta da una sola componente nella 1° colonna  
  //  USARE QUESTO COSTRUTTORE!!!
  /**
   * Construct dialog with the ability of control a behavior of added
   * components. For more information read the default constructor description
   * <code>DialogPanel</code>
   * 
   * @param fillTypeArg
   * Si applica a TUTTE le colonne
   * This field is used when the component's display area is larger than the component's requested size. It determines whether to resize the component, and if so, how.
        The following values are valid for fill:
        NONE: Do not resize the component.
        HORIZONTAL: Make the component wide enough to fill its display area horizontally, but do not change its height.
        VERTICAL: Make the component tall enough to fill its display area vertically, but do not change its width.
        BOTH: Make the component fill its display area entirely. 

   * @param weigthXArg
   * Si applica a TUTTE le colonne
   * Specifies how to distribute extra horizontal space
   * se weigthXArg<0 (ad esempio Strings.VALUE_INT_NEGATIVE) non lo modifica
    The grid bag layout manager calculates the weight of a column to be the maximum weightx of all the components in a column. If the resulting layout is smaller horizontally than the area it needs to fill, the extra space is distributed to each column in proportion to its weight. A column that has a weight of zero receives no extra space.
    If all the weights are zero, all the extra space appears between the grids of the cell and the left and right edges.
    The default value of this field is 0. weightx should be a non-negative value.
        - 0.0: for no extra space 
        - .....
        - 1.0 for 100% resizing.

   * @param weigthYArg
   * Si applica a TUTTE le colonne
   * Specifies how to distribute extra vertical space (Resizing del componente rispetto allo spazio disponibile): 
   * se weigthYArg<0 (ad esempio Strings.VALUE_INT_NEGATIVE) non lo modifica
   * @see #DialogPanel()
   */
  //__________________________________________________________________________________
  public DialogPanel(int numColumnArg, int fillTypeArg, double weigthXArg, double weigthYArg ) {
    super();
    this.numColumn=numColumnArg;
    myGridBagLayout = new GridBagLayout();
    setLayout(myGridBagLayout);

    int index_colonna;
    for (int i = 0; i < max_num_colonne; i++) {
        if (numColumnArg>i) {
            index_colonna=  (i+1);

            columnConstraints[index_colonna] = new GridBagConstraints();
            columnConstraints[index_colonna].fill = fillTypeArg;  
            if (weigthXArg>=0) columnConstraints[index_colonna].weightx = weigthXArg; // 0.0: No Resizing ; 1.0: 100% Resising
            if (weigthYArg>=0) columnConstraints[index_colonna].weighty = weigthYArg; // 0.0: No Resizing ; 1.0: 100% Resising
            columnConstraints[index_colonna].insets = myInsets;
            columnConstraints[index_colonna].anchor = posizionamento;
        }
    }


    if (numColumnArg==1) columnConstraints[1].gridwidth = GridBagConstraints.REMAINDER; //Specifies that this component is the last component in its column or row
    else if (numColumnArg==2) columnConstraints[2].gridwidth = GridBagConstraints.REMAINDER; //Specifies that this component is the last component in its column or row
    else if (numColumnArg==3) columnConstraints[3].gridwidth = GridBagConstraints.REMAINDER; //Specifies that this component is the last component in its column or row
    else if (numColumnArg==4) columnConstraints[4].gridwidth = GridBagConstraints.REMAINDER; //Specifies that this component is the last component in its column or row
    else if (numColumnArg==5) columnConstraints[5].gridwidth = GridBagConstraints.REMAINDER; //Specifies that this component is the last component in its column or row

    setBorder(new CompoundBorder(new EmptyBorder(myEmptyBorder, myEmptyBorder, myEmptyBorder, myEmptyBorder), new EtchedBorder( EtchedBorder.RAISED)));
  }



  //_____________________________________________________________________
  // addrow per righe con 1 colonna
  //_____________________________________________________________________


  //per aggiungere una riga fatta da: Componente  
  public void addRow(Component column1Component) {
    myGridBagLayout.setConstraints(column1Component, columnConstraints[1]);
    add(column1Component);
  }

  //per aggiungere una riga fatta da: Label  
  public void addRow(String column1Label) {
    JLabel column1Component = new JLabel(column1Label, JLabel.LEFT);
    addRow(column1Component);
  }




  //_____________________________________________________________________
  //_____________________________________________________________________



}

0 个答案:

没有答案