GridLayout java居中对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20473325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:12:03  来源:igfitidea点击:

GridLayout java center alignment

javaswingalignmentjlabelgrid-layout

提问by Sathish

I'm using GridLayout class in Java to layout some UI components. Image is here:

我在 Java 中使用 GridLayout 类来布局一些 UI 组件。图片在这里:

enter image description here

在此处输入图片说明

I would like to get the Create Shopping Cart picture and the associated text to be aligned center in their respective cells in the panel. For details - the shopping cart picture would have to be in the center of the gray cell of the panel. And the text in the JTextArea have to be center aligned as well. Can you please help. My code is attached.

我想让“创建购物车”图片和相关文本在面板中各自的单元格中居中对齐。有关详细信息 - 购物车图片必须位于面板灰色单元格的中心。JTextArea 中的文本也必须居中对齐。你能帮忙吗。附上我的代码。

import javax.swing.*;
import java.awt.*;

class ImageDemo extends JFrame
{
ImageDemo()
{
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(2,2));

    setSize(800,400);
    setLayout(new GridLayout(2,2));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel cartpane = new JPanel();
    cartpane.setLayout(new GridLayout(1,2));    

    /*
    GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = 0;    
    c.anchor = GridBagConstraints.SOUTH;        
    c.fill=GridBagConstraints.BOTH;
    cartpane.add(imglabelcart,c);
    c.gridx=1;
    c.gridy=0;

    c.fill=GridBagConstraints.BOTH ;
    c.anchor = GridBagConstraints.WEST;
    cartpane.add(cartta,c); 
    */

    ImageIcon iconcart = new ImageIcon("cart.jpg");     
    JLabel imglabelcart = new JLabel("Create Shopping Cart");
    imglabelcart.setIcon(iconcart);
    imglabelcart.setVerticalTextPosition(SwingConstants.BOTTOM);
    imglabelcart.setHorizontalTextPosition(SwingConstants.CENTER);

    JTextArea cartta = new JTextArea();
    cartta.setLineWrap(true);
    cartta.append("Use the Create Shopping Cart transaction to create a 
                new shopping cart for your purchases.\n");
    cartta.append("You can view the products available in the catalog and select 
                them to be part of your shopping cart.");

    cartpane.add(imglabelcart);
    cartpane.add(cartta);

    ImageIcon iconapprove = new ImageIcon("approve.jpg");
    ImageIcon iconviewpo = new ImageIcon("viewpo.jpg");
    ImageIcon iconlogout = new ImageIcon("viewpo.jpg"); 
    JLabel imglabelapprove = new JLabel("Approve Shopping Cart");
    JLabel imglabelviewpo = new JLabel("View Purchase Order");
    JLabel imglabellogout = new JLabel("Logout");       

    imglabelapprove.setIcon(iconapprove);
    imglabelapprove.setVerticalTextPosition(SwingConstants.BOTTOM);
    imglabelapprove.setHorizontalTextPosition(SwingConstants.CENTER);

    imglabelviewpo.setIcon(iconviewpo);
    imglabelviewpo.setVerticalTextPosition(SwingConstants.BOTTOM);
    imglabelviewpo.setHorizontalTextPosition(SwingConstants.CENTER);

    imglabellogout.setIcon(iconlogout);
    imglabellogout.setVerticalTextPosition(SwingConstants.BOTTOM);
    imglabellogout.setHorizontalTextPosition(SwingConstants.CENTER);

    pane.setBackground(new Color(156,195,252));
    pane.add(cartpane);
    pane.add(imglabelapprove);
    pane.add(imglabelviewpo);
    pane.add(imglabellogout);

    setVisible(true);

}

public static void main(String[] args)
{
    ImageDemo demoi = new ImageDemo();
}
}

采纳答案by alex2410

Use setHorizontalAlignment()method of JLabellike next:

使用setHorizontalAlignment()方法JLabel如下:

imglabelapprove.setHorizontalAlignment(JLabel.CENTER);

For centring text in JTextArearead that answer.

对于在JTextArea阅读该答案中居中文本。