3
자바에서 GridLayout 클래스를 사용하여 일부 UI 구성 요소를 레이아웃하고 있습니다. 이미지는 여기에 있습니다 :GridLayout 자바 센터 정렬
내가 작성 장바구니 사진 및 관련 텍스트가 패널에서 각각의 세포에 중심을 정렬 할 수 싶습니다. 자세한 내용은 장바구니 사진을 패널의 회색 셀 중심에 위치해야합니다. 그리고 JTextArea의 텍스트도 중앙 정렬되어야합니다. 도와 주실 수 있니? 내 코드가 첨부되어 있습니다. 다음과 같은 JLabel
의
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();
}
}
이것이 작동하지 않는다면 그 이유가 무엇일까요? –
@ JochenReinschlüssel이 접근 방식이 효과가 없다면 JLabel이 너무 짧아 콘텐츠 중앙에 위치 하더라도 셀 왼쪽에 나타납니다. 그것의 길이를 늘리십시오. – FaithReaper