使用MigLayout设置JTable首选大小时移动的组件

时间:2017-10-09 06:08:05

标签: java swing layout-manager miglayout

我一直在使用MigLayout布局管理器设计Java应用程序,但在设置JTable的首选大小后遇到了问题。我想让JTable顶部的组件居中,这是我为JTable设置首选大小之前的情况。关于这部分计划的代码如下:

 public MRGSCareersManagement(){
    super("") ;
    setLookAndFeel() ;

    GraphicsConfiguration config = this.getGraphicsConfiguration();
    Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
    this.setMaximizedBounds(new Rectangle(0, 0, usableBounds.width, usableBounds.height));
    this.setExtendedState((this.getExtendedState() & this.MAXIMIZED_BOTH) == this.MAXIMIZED_BOTH ? JFrame.NORMAL : JFrame.MAXIMIZED_BOTH);

    this.setLayout(new MigLayout("align 50% 0%")) ;

    left = new JButton() ;  
    left.setIcon(new ImageIcon("src\\mrgscareersmanagement\\images\\leftImage.png"));
    left.setFocusPainted(false);
    left.setContentAreaFilled(false);
    left.setBackground(Color.white);
    left.addActionListener(this) ;

    right = new JButton() ;
    right.setIcon(new ImageIcon("src\\mrgscareersmanagement\\images\\rightImage.png"));
    right.setFocusPainted(false);
    right.setContentAreaFilled(false);
    right.setBackground(Color.white);
    right.addActionListener(this) ;


    header = new JLabel("Careers Management", SwingConstants.CENTER) ;
    header.setFont(new Font("Arial", Font.PLAIN, 35));
    header.setOpaque(true);
    header.setBackground(Color.pink) ;

    subHeader = new JLabel("", SwingConstants.CENTER) ;
    subHeader.setFont(new Font("Helvetica", Font.PLAIN, 20));
    subHeader.setOpaque(true);
    subHeader.setBackground(Color.pink) ;

    tableWidth = (int) (usableBounds.width * 0.7) ;
    tableHeight = (int) (usableBounds.height * 0.7) ;

    JTable table = new JTable(tableData, columnNames);
    JScrollPane tableScrollPane = new JScrollPane(table);
    table.setPreferredScrollableViewportSize(new Dimension(tableWidth, tableHeight));
    table.setFillsViewportHeight(true);
    table.setShowGrid(true);
    table.setGridColor(Color.BLACK);
    table.setBackground(Color.pink) ;

    DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
    centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
    for(int i = 0; i < 5; i++){
        table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
    }

    this.getContentPane().setBackground( Color.white );

    this.add(left, "span 1 2") ;
    this.add(header, "align center") ;
    this.add(right, "span 1 2, wrap") ;
    this.add(subHeader, "align center, cell 1 1, wrap") ;
    this.add(tableScrollPane, "align center, span") ;

    setBackground(new Color(1.0f,1.0f,1.0f));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    setVisible(true) ;
    open() ;
}

private void setLookAndFeel() {  //Set UI manager
    try {  //Handle errors when setting UI manager
        UIManager.setLookAndFeel(  //Set Nimbus as UI manager
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" 
        );
    } 
    catch(Exception exc) {   //Ignore Error
    }   
}

目前,该应用程序如下所示:https://imgur.com/mfadtrA

那么如何将内容集中在表格上方(即标题部分和箭头)?

由于

1 个答案:

答案 0 :(得分:0)

只需使用框架的默认BorderLayout

使用BorderLayout.CENTER将包含JTable的JScrollPane添加到框架中。

然后创建一个“包装”面板来保存包含您想要居中的组件的子面板,并使用BorderLayout.PAGE_START将此面板添加到框架中。此面板的默认布局是以集群为中心的FlowLayout。因此,现在您需要做的就是使用“标题”组件创建另一个面板,并将此面板添加到包装器面板。

所以基本代码是:

JPanel child = new JPanel( whatever layout manager you want );
child.add(...);
child.add(...);
JPanel wrapper = new JPanel();
wrapper.add( child );
frame.add(wrapper, BorderLayout.PAGE_START);