如何将组件置于JPanel中其他组件的中心 [英] how to put a component at the center of other component that sit in a JPanel

查看:126
本文介绍了如何将组件置于JPanel中其他组件的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将JLabel或JPanel放在JPanel中的JTable的中心. 我看到了一个带有BufferedImage的示例,但无法将其转换为某些重用, 这是示例链接: 在JPanel中的组件上放置JLabel

I wondered how do I put a JLabel or a JPanel at the center of a JTable that Sit in a JPanel. I saw one example with BufferedImage but I could not convert it for some resone, here is the example link: Put JLabel on Component in JPanel

我还放了一张图片以显示我的意思. 按一下以查看我需要的结果 顺便说一句,这就是我使用Windows 8时的样子 现在... 有什么主意吗?

I put also an image to show what I mean. Press to see the result I need by the way this is how it looks like when I uses windows 8 now... any idea ?

推荐答案

您可以将任何组件直接添加到表中(这是编辑器的工作方式).

You can add any component directly to the table (this is how an editor works).

您只需要设置组件的大小/位置:

You just need to set the size/location of the component:

JLabel label = new JLabel( "Please Wait" );
label.setSize( label.getPreferredSize() );
label.setLocation(20, 20);
table.add( label );
table.repaint();

或者您可以使用JLayer类来装饰JTable.在如何使用JLayer类装饰组件获取更多信息和工作示例.

Or you could use the JLayer class to decorate the JTable. Read the section from the Swing tutorial on How to Decorate Components With the JLayer Class for more information and working examples.

适当的MCVE的简单示例"

A simple example of a proper MCVE"

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

public class PleaseWait extends JPanel
{
    PleaseWait()
    {
        JTable table = new JTable(5, 5);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        add( new JScrollPane( table ) );

        JLabel label = new JLabel("Please Wait");
        label.setOpaque(true);
        label.setBackground(Color.LIGHT_GRAY);
        label.setBorder( new EmptyBorder(10, 10, 10, 10) );
        label.setSize( label.getPreferredSize() );
        label.setLocation(150, 20);
        table.add( label );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("PleaseWait");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new PleaseWait());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

这篇关于如何将组件置于JPanel中其他组件的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆