简体   繁体   English

JTabbedPane图像对齐

[英]JTabbedPane image alignment

I made a quick research to solve this problem but until now I found nothing regarding to this. 我做了一个快速研究来解决这个问题,但直到现在我才发现这个问题。 I have one image into one TabbedPane object but when I try to align this image on the center of the label inside the TabbedPane it "Doesn't" work. 我在一个TabbedPane对象中有一个图像,但是当我尝试将此图像对齐TabbedPane内部标签的中心时,它“无法”工作。 The center alignment in this case works only for horizontal view but I want to be in the center of both vertical and horizontal. 在这种情况下,中心对齐仅适用于水平视图,但我希望位于垂直和水平的中心。 Check out the sample below: 看看下面的示例:

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.sh.st.gui.MainScreen;

public class test {

    JTabbedPane tabbedPane = new JTabbedPane();
    JFrame mainFrame = new JFrame();

public static void main (String[] args){

        test t = new test();

    }

    public test(){

        JPanel entrance = new JPanel();
        JLabel lbImage1;
        JMenuBar bar;
        JMenu file, registerQuery;
        ImageIcon Logo= new ImageIcon("rsc/img/imagem.jpg");        


        lbImage1= new JLabel(Logo, JLabel.CENTER);
            entrance.add(lbImage1);
                tabbedPane.addTab("Entrance", null, entrance);
                    mainFrame.getContentPane().add( tabbedPane, BorderLayout.CENTER);
                        bar= new JMenuBar();
                            file= new JMenu("File");
                                registerQuery= new JMenu("Request");
                                    mainFrame.setVisible(true);
    }   

} }

I guess its not so hard to do what I want but until now as I said, I found nothing, anyone could help please? 我想要做我想要的并不是那么难,但直到现在,正如我所说的,我什么都没找到,有人可以帮忙吗? thanks in advance 提前致谢

The JLabel alignment will only center horizontally due to the potitioning characteristics of its parent container. 由于其父容器的灌溉特性, JLabel对齐将仅水平居中。 In this case it is the default layout for JPanel which is FlowLayout . 在这种情况下,它是JPanel的默认布局,即FlowLayout This layout manager does not facilitate easy vertical alignment. 此布局管理器不便于轻松垂直对齐。

Provided that the JLabel lbImage1 will be the only component on the JPanel entrance , then GridLayout can be used to center the JLabel both horizontally and vertically: 如果JLabel lbImage1将是JPanel entrance上唯一的组件,那么GridLayout可用于水平和垂直居中JLabel

entrance.setLayout(new GridLayout());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM