简体   繁体   English

如何将JPanel与JFrame的底部对齐(java swing)

[英]How to align JPanel to bottom of JFrame (java swing)

Long story short, I'm making a simple audio player in Java and am starting the GUI; 简而言之,我正在用Java开发一个简单的音频播放器,并正在启动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). 我在问如何通过按钮(控件)使JPanel对准主窗口(JFrame)的底部中心。

Here's the code. 这是代码。

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();

  }
}

The FlowLayout() in JFrame covers the center alignment; JFrame中的FlowLayout()覆盖了中心对齐; so what covers the bottom? 那么到底是什么呢?

使用BorderLayout并将面板放置在BorderLayout.SOUTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM