長話短說,我用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()包含中心對齊;那麼底部是什麼?
'UI.setSize(400,140)的
BorderLayout.SOUTH
;'應該最好是'UI.pack();'whcihc要來後'UI.setResizable(假);'。進一步的'UI.setVisible(true);'應該在UI.add(controls);'之後出現。 –