编程语言
首页 > 编程语言> > java – TextField大小和垂直对齐

java – TextField大小和垂直对齐

作者:互联网

我在DialogBox上使用MigLayout.

每个面板都使用MigLayout.

展示行有一个面板.
每列都有一个面板(交付类型,选择元素,Sortie).
输出文件行有一个面板.
按钮面板.

也许有更好的方法来做太多面板?

我的代码

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

在我移动窗户之前,我的文本字段没有大小,我不明白为什么.
另外,为什么每个色谱柱的组件都从面板的顶部开始?

解决方法:

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

// 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提取到具有single responsability的可重用顶级类中.如果没有简单的内部面板,则提取此类将更加困难.

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

标签:java,layout,swing,miglayout
来源: https://codeday.me/bug/20190630/1332019.html