本文介绍了如何将JPanel与JFrame的底部对齐(java swing)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
长话短说,我正在用Java开发一个简单的音频播放器,并正在启动GUI.到目前为止,还没有事件,也没有任何功能.我在问如何通过按钮(控件)使JPanel对准主窗口(JFrame)的底部中心.
Long story short, I'm making a simple audio player in Java and am starting the GUI; no events, no functionality of any kind as of yet.I'm asking how I can the JPanel with the buttons (controls), to align to the bottom center of the main window (JFrame).
这是代码.
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()覆盖了中心对齐;那么到底是什么呢?
The FlowLayout() in JFrame covers the center alignment; so what covers the bottom?
推荐答案
使用BorderLayout
并将您的面板放在它的BorderLayout.SOUTH
中
Use BorderLayout
and put your panel in the BorderLayout.SOUTH
of it
这篇关于如何将JPanel与JFrame的底部对齐(java swing)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!