0
我是新來的使用Java的GUI,我有一個問題移動我的文本和按鈕。不管我給我的按鈕或任何其他JLabel的座標,它不會移動,我想知道如何修復它,以便我可以將我的組件放在任何我想要的地方JPanel
無法定位按鈕或JLabels
public class IntroPage extends JFrame {
public static void main(String[] args) {
IntroPage main = new IntroPage();
main.setVisible(true);
}
private JPanel contentPane;
public IntroPage(){
//make sure the program exits when the frame closes
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Welcome");
contentPane = new JPanel();
setSize(400,700);
//This will center the JFrame in the middle of the screen
setLocationRelativeTo(null);
//Welcome Page stuff :D
JLabel ApplauseLabel = new JLabel("Welcome to U.X.Dot.X");
ApplauseLabel.setFont(new Font("Gill Sans MT", Font.PLAIN, 30));
ApplauseLabel.setLocation(100, 50);
contentPane.add(ApplauseLabel);
JLabel slogan = new JLabel("Register below");
slogan.setFont(new Font("Gill Sans MT", Font.PLAIN, 15));
slogan.setLocation(100, 400);
contentPane.add(slogan);
//FacebookSignUp.
JButton FBbutton = new JButton("Login With FaceBook");
FBbutton.setBackground(Color.BLUE);
FBbutton.setSize(50,50);
FBbutton.setLocation(20, 40);
FBbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Add JPanel to go to FB API. Much later
}
});
contentPane.add(FBbutton);
add(contentPane);
//make sure the JFrame is visible
setVisible(true);
}
}
1)提供ASCII藝術或一個簡單的繪圖*意*在最小尺寸的GUI的佈局,如果可以調整大小,更寬和更高。 2)請學習常用的Java命名規則(命名約定 - 例如'EachWordUpperCaseClass','firstWordLowerCaseMethod()','firstWordLowerCaseAttribute',除非它是'UPPER_CASE_CONSTANT')並且一致地使用它。 3)Java GUI必須在不同的語言環境中使用不同的PLAF來處理不同的操作系統,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。相反.. –
..使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及佈局填充和邊框爲[白色空間](http://stackoverflow.com /一個/418556分之17874718)。 4)'新的字體(「Gill Sans MT」,Font.PLAIN,15)'這是設置'Font'的一種可行的方法。大多數系統不會安裝它,所以最好首先檢查它的存在,然後根據需要默認使用'Font.SERIF'或'Font.SANS_SERIF'這樣的'通用'字體。通用字體將始終可用(儘管可能會從操作系統更改爲操作系統 - 另一個不使用精確定位的原因)。 –
那我應該用什麼來代替我的東西呢? @AndrewThompson。我正在使用'GridBag',但將它們放置爲1,0或4,0會使它不能移動。 – cuber