2015-02-09 31 views
-1

我想在jButton上面設置我的jLabel。事實上,如果他們只是「漂浮」在彼此之上,那將是理想的。java JLabel不會重定位

Jet,我不能讓標籤顯示,因爲它停留在按鈕下方。我一直在google搜索和嘗試setLocation之類的東西,但無濟於事。我敢肯定,這是一件非常基本的,我很想念......

public class TranceExperiment extends JFrame { 

     public TranceExperiment() { 

      setTitle("Simple example"); 
      setSize(600, 400); 
      setLocationRelativeTo(null); 
      setDefaultCloseOperation(EXIT_ON_CLOSE); 
     } 


     public static void main(String[] args) { 

      EventQueue.invokeLater(new Runnable() { 
       @Override 
       public void run() { 
        TranceExperiment ex = new TranceExperiment(); 
        ex.setVisible(true); 

        //set startLabel 
        JLabel expText = new JLabel("Welcome to the experiment"); 
        ex.add(expText); 
        expText.setLocation(200,200); 
        //expText.setSize(100, 100); 
        expText.setVisible(true); 
//     expText.setLayout(null); 
        ex.add(expText); 


        //start Button 
        final JButton startButton = new JButton("Start"); 
        startButton.addActionListener(new ActionListener() { 
         @Override 
         public void actionPerformed(ActionEvent event) { 
          //do stuff 

          startButton.setText("Continue"); 

         } 

        }); 

        ex.add(startButton); 

       } 
      }); 
     } 
    } 
+0

嘗試使用佈局管理器嵌套其他組件內部的組件。 – Randy 2015-02-09 13:55:17

+0

我不知道這是什麼意思 – dorien 2015-02-09 13:55:54

+0

這不就是我在做什麼? HTTP://www.cs101。org/courses/fall05/resources/swinglayout/ – dorien 2015-02-09 13:56:34

回答

1

使用佈局,盧克! :)我喜歡GridBagLayout,因爲它非常靈活。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Main { 

public static void main(String[] args) { 
    System.out.println("Hello World!"); 
    final JFrame frame = new JFrame(); 
    frame.setTitle("Simple example"); 
    frame.setSize(600, 400); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
    frame.setLayout(new BorderLayout()); 

    JPanel contentPanel = new JPanel(new GridBagLayout()); 
    frame.add(contentPanel); 

    JLabel expText = new JLabel("Welcome to the experiment"); 
    expText.setPreferredSize(new Dimension(150, 40)); 
    expText.setVisible(true); 

    final JButton startButton = new JButton("Start"); 
    startButton.setPreferredSize(new Dimension(450, 40)); 
    startButton.setPreferredSize(new Dimension(450, 40)); 
    startButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      startButton.setText("Continue"); 
     } 

    }); 

    contentPanel.add(expText, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, 
      GridBagConstraints.HORIZONTAL, new Insets(0, 200, 0, 100), 0, 0)); 
    contentPanel.add(startButton, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, 
      GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); 
    contentPanel.add(Box.createVerticalGlue(), new GridBagConstraints(0, 1, 2, 2, 1, 1, 
      GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); 
    } 
} 
+0

太棒了!你介意在0,0,1,1,1,0和Insets上進行詳細說明嗎?是水平的,垂直的偏移量? – dorien 2015-02-09 16:13:45

+0

另外,即使我設置了startButton的首選大小,它也會延伸到窗口 – dorien 2015-02-09 16:15:14

+0

此描述GridBag佈局http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html第一對0 ,0 - x和y在網格中,第二對1,1 - 網格長度,最後一對1,0 - 填充水平並填充垂直!如果與GridBagConstraints不矛盾,則首選大小會起作用。第一優先級 - GridBagConstraints,第二 - 首選大小。 – rodgenk 2015-02-09 19:02:34

2

你必須使用佈局管理器,如果你想整齊間隔佈局。這些包括FlowLayout,BorderLayout和GridLayout。我建議使用GridLayout解決這個問題,例如:

ex.setLayout(new GridLayout(3,1)); 

3是用於行,1是用於列。在將按鈕和標籤添加到框架之前插入此行。

編輯:

您還可以添加一個JPanel到你的框架,然後設置面板GridLayout的佈局,像這樣:

ex.setLayout(new BorderLayout()); 
JPanel panel = new JPanel(); 
panel.setLayout(new GridLayout(3,1)); 
panel.add(startButton); 
ex.add(panel, BorderLayout.NORTH); 

通過使用BorderLayout您可以在任何位置的面板框架。請記住設置面板和/或框架的大小,並將其他標籤添加到面板。 BorderLayout有幾個設置可供玩家使用,包括SOUTH,CENTER,EAST和WEST。

+0

偉大的,除了按鈕現在變得非常大。我試過startButton.setSize(60,20);但這並沒有改變它。有任何想法嗎? – dorien 2015-02-09 14:42:23

+1

我編輯了我的回答,關於你的評論@dorien。 – instanceof 2015-02-10 08:28:34

1

,以實現自己的目標,最簡單的方法是使用的WindowBuilder,爲Eclipse插件(https://eclipse.org/windowbuilder/)。

首先從可用佈局列表中選擇GroupLayout

然後把你的jLabel放在你的jButton以上並選擇「錨定底部」。該插件將爲您生成代碼。

enter image description here

+0

我想我也可以用intelliJ來做這個。我似乎無法找到如何訪問他們的gui設計師 – dorien 2015-02-09 14:32:59

+0

我從來沒有嘗試* IntelliJ *,但我知道它也有一個GUI設計器。 – 2015-02-09 14:36:45

0

setLocationsetSize並不意味着被你叫,框架的用戶。它們意味着由與組件容器關聯的佈局管理器調用。

您可以找到有關佈局管理是如何工作的Java網站上的全面和詳細的信息:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

爲了得到你想要的東西,一個SpringLayout似乎更爲合適。

作爲一種替代方法,如果您想直接撥打setLocationsetSize,則應通過將其設置爲null來移除佈局管理器。