2015-04-27 56 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

相关问题