当前位置: 首页 > 知识库问答 >
问题:

Java:如何将JLabel移动到JPanel的中心?

慕冠宇
2023-03-14

所以在我的GUI中,我有一个边框布局的JFrame。在北部和西部有一个菜单栏和一些图形用户界面。在CENTER中,有一个JLabel。我希望它移动到JPanel的中心(水平和垂直)。我如何正确地做到这一点?我尝试了盒子布局和网格布局。一个要求是我不能使用gridpack布局。

public class NewClass extends JFrame{
    public NewClass () {
        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation (EXIT_ON_CLOSE);

//menubar
        JMenuBar bar = new JMenuBar();
        JMenu editMenu = new JMenu("Edit");
        JMenuItem mItem = new JMenuItem("Cut"); // edit->cut
        editMenu.add(mItem);
        mItem = new JMenuItem("Copy"); // edit->copy
        editMenu.add(mItem);
        mItem = new JMenuItem("Paste"); // edit->paste
        editMenu.add(mItem);
        bar.add(editMenu);
        this.setJMenuBar(bar);

//north panel
        JPanel topPanel = new JPanel();
        this.add(topPanel,BorderLayout.NORTH);
        JLabel myLabel = new JLabel ("Label:") ;
        topPanel.add(myLabel);
        JButton mytopButton = new JButton ("Push Me");
        topPanel.add(mytopButton);

//left panel
        JPanel leftPanel = new JPanel();
        leftPanel.setBorder (new TitledBorder("Commands:"));
        leftPanel.setLayout (new GridLayout (10,1));
        this.add(leftPanel,BorderLayout.WEST);
        JButton myLeftButton1 = new JButton ("Button 1");
        leftPanel.add(myLeftButton1);
        JButton myLeftButton2 = new JButton ("Button 2");
        leftPanel.add(myLeftButton2);
        JButton myLeftButton3 = new JButton ("Button3");
        leftPanel.add(myLeftButton3);

//center panel
        JPanel centerPanel = new JPanel();
        this.add(centerPanel,BorderLayout.CENTER);
        JLabel mapLabel = new JLabel("Test_String"); //move this to center of JPanel
        centerPanel.add(mapLabel);
        centerPanel.setBorder (new EtchedBorder(Color.black,Color.black));
        centerPanel.setBackground (Color.white);
    }
}

共有3个答案

姚永年
2023-03-14

回答。谢谢大家。

    JPanel centerPanel = new JPanel();
    centerPanel.setLayout (new GridLayout ()); //added
    this.add(centerPanel,BorderLayout.CENTER);
    JLabel mapLabel = new JLabel("Test_String");
    mapLabel.setHorizontalAlignment(SwingConstants.CENTER); //added
    mapLabel.setVerticalAlignment(SwingConstants.CENTER); //added
    centerPanel.add(mapLabel);
    centerPanel.setBorder (new EtchedBorder(Color.black,Color.black));
    centerPanel.setBackground (Color.white);
后树
2023-03-14

首先看看JLabel的JavaDocs

具体而言,JLabel#设置水平对齐JLabel#设置垂直对齐

阮炯
2023-03-14

检查API中影响组件对齐的方法。

有些方法会影响布局管理器中构件的对齐方式,有些方法会影响标签本身中文本的对齐方式。

 类似资料:
  • 我试着用Java写一个包含图像的标签每秒钟都在移动的程序,但不幸的是代码没有运行,尽管它包含零错误。有没有人知道发生了什么。代码如下: 拜托,有谁能帮我。

  • 问题内容: 我尝试每秒钟将其移到右侧(x ++) 我尝试用螺纹移动它。 怎么做?(并且可以看到它每秒移动一次) 还有另一种不用线程的方法吗? 我应该使用什么布局管理器? 我在这里尝试 非常感谢您的任何帮助 问题答案: 类名以大写字母开头,即 Swing组件应在以下位置创建和修改 这样创建一个新的: }); t.start();//start thread 但是我建议在它上运行一个Swing : 如

  • 我试图让一些jLabel在按下GridLayout键的JPanel上彼此交换位置。 到目前为止: 1。我只能让他们对鼠标点击做出反应 2。他们只能在其上方和左侧的位置进行切换。 注: 1。如果我设置movePlayer(1),它会工作。并从网格(8,7)切换一个带有玩家图像的JLabel,其中一个JLabel上面有一个地板图像。然而,我的问题是,当我设置movePlayer(2)时,它会给我和索引

  • 问题内容: 我正在使用NetBeans GUI构建器来处理我的布局(我对LayoutManagers很糟糕),并试图放置一个简单的JLabel,以使其始终(水平)居于其父JPanel内部。理想情况下,即使调整了JPanel的大小,这也将保持正确,但是,如果这是一个疯狂的编码,比第一次创建JPanel时居中就足够了。 我自己尝试处理布局已经很糟糕了,但是由于NetBeans GUI Builder自

  • 问题内容: 如何在Java的JLabel中使用html标签? 问题答案: 要将html放在中,您可以使它看起来像这样

  • 问题内容: 我希望简单的动画每0.5秒设置一次位置,但它不会仅在循环结束时设置动画。 int x = 1; int y = 1; 我已经尝试过使用thread.sleep()绘制动画,并且可以正常工作,但动画确实正确,但不幸的是,这对我来说不是一个选择,因为我需要在框架内移动jlabel并在其中放置图形图片。有人可以帮助我解决这个问题。 我已经尝试过这两个相同的结果 问题答案: 与其使用Java