【问题标题】:How to align JPanel to bottom of JFrame (java swing)如何将 JPanel 与 JFrame 底部对齐(java swing)
【发布时间】:2015-07-06 14:07:54
【问题描述】:

长话短说,我正在用 Java 制作一个简单的音频播放器并启动 GUI;没有事件,还没有任何类型的功能。 我在问如何使用按钮(控件)使 JPanel 与主窗口(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() 覆盖居中对齐;那么底部是什么?

【问题讨论】:

  • UI.setSize(400, 140); 最好是UI.pack();,它应该在UI.setResizable(false); 之后。进一步的UI.setVisible(true); 应该在UI.add(controls); 之后。

标签: java swing jpanel alignment layout-manager


【解决方案1】:

使用BorderLayout 并将您的面板放入其中的BorderLayout.SOUTH

【讨论】:

猜你喜欢
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
  • 2019-07-03
  • 1970-01-01
  • 2012-01-25
  • 2013-10-07
  • 1970-01-01
相关资源
最近更新 更多