文本字段大小和垂直对齐

imzjd6km  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(360)

我在一个 DialogBox .
每个面板都使用migalayout。
有一个显示行的面板。每个列都有一个面板(交付类型、选择元素、出击)。输出文件行有一个面板。有一个按钮面板。
也许有一个更好的方法来做它没有太多的面板?

我的代码

//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;
}

我的文本字段没有大小,直到我移动我的窗口,我不明白为什么。还有,为什么不是每一列的组件都从面板的顶部开始呢?

xt0899hw

xt0899hw1#

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

// 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(对于更复杂的面板)。

相关问题