2015-09-04 22 views
-1

我已經嘗試setLocation(x,y)和setLocationRelativeTo(null)通過將JFrame的佈局設置爲null,但沒有解決。在搜索時,我發現這個問題已經被問到兩個或三個人,但他們通過setLocation()和setLocationRelativeTo(null)完成。如何手動設置JComponents的位置等

import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import java.awt.FlowLayout; 

public class StartMenu{ 
    JPanel startPanel; 
    JLabel title; 
    JButton startTest; 
    JButton exit; 
    JFrame menuFrame; 

    public StartMenu(){ 
     menuFrame = new JFrame("Start Menu"); 
     menuFrame.setLayout(null); 

     startPanel = new JPanel(); 

     title = new JLabel("Adaptive Test",JLabel.CENTER); 
     title.setLocation(20,20); 
     startPanel.add(title); 

     startTest = new JButton("Start"); 
     startTest.setLocation(40,40); 
     startPanel.add(startTest); 

     exit = new JButton("Exit"); 
     exit.setLocation(100,100); 
     startPanel.add(exit); 
     menuFrame.setContentPane(startPanel); 

     menuFrame.setVisible(true); 
     menuFrame.setSize(500, 500); 
     menuFrame.setResizable(false); 
     menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 
+2

Java GUI必須使用不同語言環境中使用不同PLAF的不同OS,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 –

+2

我投票結束這個問題作爲題外話,因爲它要求如何實施黑客,當更好的策略是使用佈局,邊界和填充。 –

+1

您的'startPanel'仍然具有默認的'FlowLayout'作爲佈局。只改變startPanel的佈局爲null(不是框架的),並使用'setBounds()'...這隻適用於你真的想要一個空佈局,當然(爲了學習理由或其他)。 - 如果沒有必要,請使用[佈局管理器](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)。 –

回答

0

JFrame的佈局設置爲空,但startPanel的一個不是。所以首先,使用:

startPanel.setLayout(null); 

現在,而不是component.setLocation(x, y),使用component.setBounds(x, y, width, height),所以你也設置大小爲他們。

但正如評論中所說,最好使用layout managers而不是空白布局。

-1

首先你設置爲null JPanel的JFrame的istead,所以你必須使用

startPanel.setLayout(null); 

那麼你應該使用setLocation的的setBounds istead becouse如果你剛纔設置的位置與空佈局管理器你」我們可能會在你的面板上看不到任何東西,因爲默認情況下,所有維度都被初始化爲0。

所以,你可以重寫你的軟件是這樣的:

import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class StartMenu{ 
    JPanel startPanel; 
    JLabel title; 
    JButton startTest; 
    JButton exit; 
    JFrame menuFrame; 

    public StartMenu(){ 
     menuFrame = new JFrame("Start Menu"); 
     menuFrame.setLayout(null); 

     startPanel = new JPanel(); 
     startPanel.setLayout(null); 

     title = new JLabel("Adaptive Test",JLabel.CENTER); 
     title.setBounds(20,20, 100, 30); 
     startPanel.add(title); 

     startTest = new JButton("Start"); 
     startTest.setBounds(50,50, 100, 30); 
     startPanel.add(startTest); 

     exit = new JButton("Exit"); 
     exit.setBounds(100,100, 100 ,30); 
     startPanel.add(exit); 
     menuFrame.setContentPane(startPanel); 

     menuFrame.setVisible(true); 
     menuFrame.setSize(500, 500); 
     menuFrame.setResizable(false); 
     menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

} 

這樣你得到的東西的工作,但它不是一個很好的編程習慣用手來設置所有的位置和大小在軟件becouse它在不同的系統上工作不好(因此它不會攜帶),而且當你的軟件開始增長數十甚至數百個圖形元素時,你也會發現自己處於調試噩夢。

我的建議是使用gridBagLayout,起初我似乎很模糊,但相信我,事實並非如此!