GridLayout java中心对齐

 nnamyp_826 发布于 2023-02-11 23:59

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

在此输入图像描述

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

alex2410.. 12

使用下一个setHorizontalAlignment()方法JLabel:

imglabelapprove.setHorizontalAlignment(JLabel.CENTER);

用于将JTextArea阅读中的文本居中回答.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有