2010-07-19 109 views
4

我在設置Jlabel位置時遇到問題。
我將內容窗格設置爲一些JPanel,我創建並嘗試添加我的JLabel。
Java Swing - JLabel位置

JLabel mainTitle = new JLabel("SomeApp"); 
    mainTitle.setFont(new Font("Arial",2 , 28)); 
    mainTitle.setBounds(0,0, 115, 130); 
    getContentPane().add(mainTitle); 

我想我的JPanel將是對我的應用程序和我所得到的左上角是「SomeApp」的頂級中鋒。(而不是左上)。

btw我試圖添加JButton和我不能改變JButton的寬度,高度,x,y。

回答

4

搖擺使用Layout Managers來放置組件。

你必須瞭解他們如何有效地使用它們。您可以將佈局管理器設置爲null,並對佈局進行佈局,但不推薦使用,因爲每次都必須跟蹤新組件,並在窗口移動時自行執行佈局計算等。

佈局管理員起初有點難以掌握。

您的Windows可能是這樣的:

as simple as this http://img827.imageshack.us/img827/7043/capturadepantalla201007z.png

使用此代碼:

import javax.swing.*; 
import java.awt.Font; 
import java.awt.FlowLayout; 

class JLabelLocation { 

    public static void main(String [] args) { 

     JLabel mainTitle = new JLabel("SomeApp"); 
     mainTitle.setFont(new Font("Arial",2 , 28)); 
     //mainTitle.setBounds(0,0, 115, 130); //let the layout do the work 

     JFrame frame = new JFrame(); 
     JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));// places at the left 
     panel.add(mainTitle); 

     frame.add(panel);// no need to call getContentPane 
     frame.pack(); 
     frame.setVisible(true); 

    } 
} 
1

特定小部件在其容器中的位置取決於其使用的佈局管理器。佈局管理器確定如何調整大小並安排小部件以使它們適當地適合。顯然,內容窗格的默認佈局決定頂層中心是放置JLabel的最佳位置。

如果你想不使用佈局管理器,只是把自己的一切(這通常是不BTW打下東西是最好的出路),然後加入:

getContentPane().setLayout(null); 
+0

非常感謝,我正在另一個親blem - 我想將按鈕外觀和感覺設置爲特定的平臺,例如窗口我將獲得窗口樣式按鈕和mac。 這是我試過的 - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); – Yosi 2010-07-19 18:05:05

0

使用佈局通常是一個好主意,因爲它們允許部件的動態調整。這裏是你如何與BorderLayout的做到這一點:

this.getContentPane().setLayout(new BorderLayout()); 
this.getContentPane().add (new JLabel ("Main title"), BorderLayout.NORTH); 

如果你想添加一些你可以用它自己的佈局創建其它附加面板標籤的右側:

// Create a panel at the top for the title and anything else you might need 
JPanel titlePanel = new JPanel (new BorderLayout()); 
titlePanel.add(new JLabel ("Main title"), BorderLayout.WEST); 

// Add the title panel to the frame 
this.getContentPane().setLayout(new BorderLayout()); 
this.getContentPane().add(titlePanel, BorderLayout.CENTER); 

這裏有一些有用的鏈接開始使用的佈局:

http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/visual.html http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/using.html

+0

當我嘗試你的選擇,使用此代碼 - \t \t final JLabel mainTitle = new JLabel(「IronApp - Choose and Play」); \t \t mainTitle。setFont(new Font(「Consolas」,1,28)); \t \t mainTitle.setBounds(100,100,windowWidth,50); \t \t mainTitle.setForeground(Color.WHITE); \t \t getContentPane()。add(mainTitle,BorderLayout.NORTH); 我無法控制我的標題位置向左,向下或向右移動。 – Yosi 2010-07-19 23:16:42

+0

@Yosy使用自定義邊框:http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/border.html – OscarRyz 2010-07-20 15:12:41

+0

通常,當我使用佈局時,我嘗試不指定硬編碼大小除了h和v的差距。相反,我使用BorderLayouts來推動屏幕周圍的事物,使它們最終到達我想要的位置。如果您在第一個鏈接中查看BorderLayouts的圖像,您會發現在NORTH位置放置的任何東西都會佔據整個空間的長度,並且只會在高度方面需要什麼。假設我想將標籤推向右側,我只需創建一個新面板並將標籤添加到EAST區域,將其有效地推向右側。要將它推向左側,請使用WEST。 – ZeBlob 2010-07-26 07:33:02