2017-04-09 41 views
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); 
    } 
} 
+0

1)提供ASCII藝術或一個簡單的繪圖*意*在最小尺寸的GUI的佈局,如果可以調整大小,更寬和更高。 2)請學習常用的Java命名規則(命名約定 - 例如'EachWordUpperCaseClass','firstWordLowerCaseMethod()','firstWordLowerCaseAttribute',除非它是'UPPER_CASE_CONSTANT')並且一致地使用它。 3)Java GUI必須在不同的語言環境中使用不同的PLAF來處理不同的操作系統,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。相反.. –

+0

..使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及佈局填充和邊框爲[白色空間](http://stackoverflow.com /一個/418556分之17874718)。 4)'新的字體(「Gill Sans MT」,Font.PLAIN,15)'這是設置'Font'的一種可行的方法。大多數系統不會安裝它,所以最好首先檢查它的存在,然後根據需要默認使用'Font.SERIF'或'Font.SANS_SERIF'這樣的'通用'字體。通用字體將始終可用(儘管可能會從操作系統更改爲操作系統 - 另一個不使用精確定位的原因)。 –

+0

那我應該用什麼來代替我的東西呢? @AndrewThompson。我正在使用'GridBag',但將它們放置爲1,0或4,0會使它不能移動。 – cuber

回答

3

你忽略了contentPane JPanel的佈局管理器。瞭解它默認使用FlowLayout,並會忽略你的setLocation和setBounds語句。如果要使JPanel接受絕對定位,則必須通過contentPane.setLayout(null)將其設置爲空佈局。

說了這麼多,我不建議你這樣做!雖然null佈局,setLocation(...)setBounds(...)對於Swing新手來說似乎是創建複雜GUI的最簡單和最好的方法,但更多Swing GUI的創建使用它們時會遇到更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。

例如以下GUI

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.GridBagLayout; 

import javax.swing.*; 

public class IntroPage2 extends JPanel { 
    public static final String TITLE = "Welcome to U.X.Dot.X"; 
    private JLabel welcomeLabel = new JLabel(TITLE, SwingConstants.CENTER); 
    private JButton fbButton = new JButton("Login With Facebook"); 

    public IntroPage2() { 
     fbButton.setBackground(Color.BLUE); 
     fbButton.setForeground(Color.CYAN); 
     welcomeLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 30)); 
     int wlGap = 20; 
     welcomeLabel.setBorder(BorderFactory.createEmptyBorder(wlGap, wlGap, wlGap, wlGap)); 

     JLabel registerBelowLabel = new JLabel("Register Below"); 
     registerBelowLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15)); 
     JPanel centralPanel = new JPanel(new GridBagLayout()); 
     centralPanel.setPreferredSize(new Dimension(300, 600)); 
     centralPanel.add(registerBelowLabel); 

     JPanel topPanel = new JPanel(new BorderLayout()); 
     topPanel.add(fbButton, BorderLayout.LINE_START); 
     topPanel.add(welcomeLabel, BorderLayout.CENTER); 

     setLayout(new BorderLayout()); 
     int ebGap = 8; 
     setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap)); 
     add(topPanel, BorderLayout.PAGE_START); 
     add(centralPanel, BorderLayout.CENTER); 
    } 

    private static void createAndShowGui() { 
     IntroPage2 mainPanel = new IntroPage2(); 

     JFrame frame = new JFrame("Welcome"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

會造成類似:

enter image description here