[英]Java: How do I move a JLabel to the center of a JPanel?
So in my GUI, I have a JFrame that's a borderlayout. 所以在我的GUI中,我有一个JFrame,它是borderlayout。 There's a menubar, and some GUI stuff in NORTH and WEST. 有一个菜单栏,以及NORTH和WEST中的一些GUI内容。 In CENTER, there is one JLabel. 在CENTER中,有一个JLabel。 I want it to move to the center (both horizontally and vertically) of the JPanel. 我希望它移动到JPanel的中心(水平和垂直)。 How do I do that correctly? 我该怎么做呢? I tried box layout and grid layout. 我尝试了盒子布局和网格布局。 One requirement is that I cannot use gridbag layout. 一个要求是我不能使用gridbag布局。
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);
}
}
Check the API for methods that affect the alignment
of the component. 检查API是否有影响组件alignment
的方法。
There are methods that affect the alignment of the component within the layout manager and others that affect the alignment of the text within the label itself. 有一些方法会影响布局管理器中组件的对齐方式,而其他方法会影响标签本身中的文本对齐方式。
Start by taking a look at the JavaDocs for JLabel
首先看一下JLabel
的JavaDocs
Specifically, JLabel#setHorizontalAlignment
and JLabel#setVerticalAlignment
具体来说, JLabel#setHorizontalAlignment
和JLabel#setVerticalAlignment
Answered. 回答。 Thanks everyone. 感谢大家。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.