4

DialogBox. _

每个面板都使用 MigLayout。

显示行有一个面板。每列都有一个面板(交付类型、选择元素、排序)。输出文件行有一个面板。有一个按钮面板。

也许有更好的方法可以在没有太多面板的情况下做到这一点?

miglayout 示例

我的代码

//set global layout
this.setLayout( new MigLayout( "wrap 3, debug" ) );

this.add( getSearchPanel(), "span 3, wrap" );

// middle section
this.add( getLivraison(), "width 33%" );
this.add( getChoixElement(), "width 33%" );
this.add( getProfile(), "width 33%" );

JLabel lblFichierSortie = new JLabel( "Output file" );
JTextField txtFichierSortie = new JTextField();
this.add( lblFichierSortie, "span 2, right" );
this.add( txtFichierSortie, "width 33%, wrap" );

this.add( getButton(), "span 3, right" );

private JPanel getSearchPanel() {
    JPanel searchPanel = new JPanel( new MigLayout() );

    JLabel lblEmission = new JLabel( "Show" );
    JTextField txtEmission = new JTextField( 10 );
    JTextField txtEM = new JTextField( 5 );

    searchPanel.add( lblEmission, "width 2%" );
    searchPanel.add( txtEmission, "split 2,right,width 60%, growx" );
    searchPanel.add( txtEM, "width 38%, growx" );

    return searchPanel;
}

private JPanel getLivraison() {
    JPanel livraisonPanel = new JPanel( new MigLayout() );
    JLabel lblComponent1 = new JLabel( "Delivery type" );
    JCheckBox chkFluxElementaire = new JCheckBox( "Flux" );
    JCheckBox chkTranscoding = new JCheckBox( "Transcoding" );

    livraisonPanel.add( lblComponent1, "wrap" );
    livraisonPanel.add( chkFluxElementaire, "wrap" );
    livraisonPanel.add( chkTranscoding, "wrap" );

    return livraisonPanel;
}

private JPanel getChoixElement() {
    JPanel component2 = new JPanel( new MigLayout() );
    JLabel lblChoix = new JLabel( "Choose element" );
    JLabel lblVideo = new JLabel( "Vidéo" );


    JLabel lblAudio = new JLabel( "Audio" );
    JLabel lblAudio2 = new JLabel( "Audio 2" );
    JLabel lblSubTitle = new JLabel( "ST" );
    JLabel lblMontage = new JLabel( "Montage" );

    //todo put combobox below every label
    component2.add( lblChoix, "wrap" );
    component2.add( lblVideo,"wrap" );
    component2.add( lblAudio,"wrap" );
    component2.add( lblAudio2, "wrap" );
    component2.add( lblSubTitle, "wrap" );
    component2.add( lblMontage, "wrap" );

    return component2;
}

private JPanel getProfile() {
    JPanel component3 = new JPanel( new MigLayout() );
    JLabel lblSortie = new JLabel( "Sortie" );
    component3.add( lblSortie, "wrap" );

    JLabel lblProfil = new JLabel( "Profil" );
    component3.add( lblProfil, "wrap" );

    JComboBox cbxProfil = new JComboBox();
    component3.add( cbxProfil, "wrap" );

    return component3;
}

private JPanel getButton() {
    JPanel buttonPanel = new JPanel( new MigLayout( "fillx,insets 0" ) );

    JButton okButton = new JButton( "Ok" );
    okButton.setMnemonic( 'O' );
    buttonPanel.add( okButton, "split,right,width 100!" );

    // Cancel button
    JButton cancelButton = new JButton( "Cancel" );
    cancelButton.setMnemonic( 'C' );

    buttonPanel.add( cancelButton, "width 100!" );
    return buttonPanel;
}

在我移动窗口之前,我的文本字段没有大小,我不明白为什么。另外,为什么不是每列的组件都从面板顶部开始?

4

1 回答 1

3

试试这个“顶部对齐问题”:

// set global layout
this.setLayout(new MigLayout("wrap 3, debug", null, "[top]"));

对于“texfield 大小问题”:

this.add(getSearchPanel(), "span 3, wrap, grow");

如果您想在没有内部 JPanel 的情况下获得相同的结果,您可以这样做:

final JLabel lblEmission = new JLabel("Show");
final JTextField txtEmission = new JTextField(10);
final JTextField txtEM = new JTextField(5);

this.add(lblEmission, "width 2%, span 3, split 3");
this.add(txtEmission, "right,width 60%, growx");
this.add(txtEM, "width 38%, growx");

但是,这不是我最喜欢的解决方案。使用内部面板,代码更加简单和可重用。我认为当您在组件上添加一些用于用户交互的侦听器和用于与模型交互的控制器时,此代码将过于复杂。当这种情况发生时,您可能希望将其提取到具有单一责任SearchPanel的可重用顶级类中。如果没有简单的内部面板,提取这个类会困难得多。

这就是为什么当我设计 swing gui 时,我更喜欢先使用BorderLayout,然后使用 MigLayout(对于更复杂的面板)。

于 2012-08-24T12:19:22.557 回答