2015-04-27 60 views
0

長話短說,我用Java製作了一個簡單的音頻播放器,並啓動了GUI;沒有任何事件,也沒有任何功能。 我在問如何使用按鈕(控件)將JPanel對齊到主窗口(JFrame)的底部中心。如何將JPanel與JFrame的底部對齊(java swing)

這是代碼。

import javax.swing.*; 
import java.awt.*; 
public class tryingtowindow extends JFrame { 

    //Buttons 
public JButton rewind; 
public JButton play; 
    public JButton fastForward; 

    //the window 
public JFrame UI; 
public JPanel controls; 

//main gui function 
public tryingtowindow(){ 

//rewind button 
rewind = new JButton(new ImageIcon ("rewind.png")); 
rewind.setBackground(Color.WHITE); 
rewind.setFocusPainted(false); 

//playbutton 
play = new JButton(new ImageIcon ("play.png")); 
play.setBackground(Color.WHITE); 
play.setFocusPainted(false); 

//fastforward button 
fastForward = new JButton(new ImageIcon ("fastforward.png")); 
fastForward.setBackground(Color.WHITE); 
fastForward.setFocusPainted(false); 

//panel w/buttons 
controls = new JPanel(); 
controls.add(rewind); 
controls.add(play); 
controls.add(fastForward); 
controls.setBackground(Color.BLACK); 

//window 
UI = new JFrame(); 
UI.setLayout(new FlowLayout(FlowLayout.CENTER)); 
UI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
UI.setSize(400, 140); 
UI.setVisible(true); 
UI.setResizable(false); 
UI.setTitle("title"); 
UI.add(controls); 

} 

public static void main(String args[]) { 

new tryingtowindow(); 

    } 
} 

JFrame中的FlowLayout()包含中心對齊;那麼底部是什麼?

+0

'UI.setSize(400,140)的BorderLayout.SOUTH;'應該最好是'UI.pack();'whcihc要來後'UI.setResizable(假);'。進一步的'UI.setVisible(true);'應該在UI.add(controls);'之後出現。 –

回答

2

使用BorderLayout並把你的面板在它

+0

有個例子嗎?我試過了,按鈕(和他們的JPanel)根本沒有出現。 – LeZayta

+0

@LeZayta [如何使用BorderLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html)和[敷設容器內的組件](http://docs.oracle.com/docs.oracle。 com/javase/tutorial/uiswing/layout/index.html)以獲取更多選項 – MadProgrammer

相關問題