2015-04-26 76 views
0
import java.awt.FlowLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 


     public class Gui_Window extends JFrame { 
      private JLabel Main_L; 

      public Gui_Window() { 
       setLayout(new FlowLayout()); 

       Main_L = new JLabel("Did you know it is possible to bind keys?"); 
       add(Main_L); 
      } 

       public static void main (String args[]) { 
        Gui_Window gui = new Gui_Window(); 
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        gui.setSize(300,300); 
        gui.setVisible(true); 
        gui.setTitle("Gamers AudioMute"); 
        gui.setResizable(true); 


      } 
     } 

我想知道如何移动我的“你知道”标签周围。你能否说明如何对齐左,右,中间以及如何通过坐标移动它?如何移动JLabes周围

IMG of the Program

+2

您应该阅读[教程:放置容器内的组件](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html)。 – Radiodef

+0

您还应该阅读[使用顶层Swing容器](http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html)来学习避免扩展'JFrame'类的事情。 – asgs

回答

0

您可以使用包含在Swing类的方法。尝试

mainL.setHorizontalAlignment(SwingConstants.LEFT); 
mainL.setVerticalAlignment(SwingConstants.BOTTOM); 

两种方法setHorizontalAligmentsetVeritcalAlignment都采取,将设置任一标签到摆动窗口内的像素整数的水平或垂直分量的开始的整数。

您还应该阅读SwingConstants,它允许您通过Swing插入预定义的整数,以便将文本与面板中的所需位置对齐。这里有一个链接

http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingConstants.html

操纵不同的布局,如BorderLayoutGridLayout时,它们也非常有用。

+0

这是一个不错的主意,但只有当JLabel大于需要包含的文本 – MadProgrammer

相关问题