【问题标题】:GridLayout java center alignmentGridLayout java居中对齐
【发布时间】:2013-12-26 17:19:25
【问题描述】:

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

我想让创建购物车图片和相关文本在面板中各自的单元格中居中对齐。有关详细信息 - 购物车图片必须位于面板灰色单元格的中心。 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();
}
}

【问题讨论】:

    标签: java swing alignment jlabel grid-layout


    【解决方案1】:

    使用JLabelsetHorizontalAlignment()方法如下:

    imglabelapprove.setHorizontalAlignment(JLabel.CENTER);
    

    要使JTextArea 中的文本居中,请阅读answer

    【讨论】:

    • 如果这不起作用,可能是什么原因?
    • @JochenReinschlüssel 如果使用这种方法不起作用,可能是您的 JLabel 太短,所以即使您将内容居中,它也会出现在单元格的左侧。增加它的长度。
    猜你喜欢
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 2013-07-24
    • 2023-03-05
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多