如何使 JPanel 中的组件尽可能伸展?
Posted
技术标签:
【中文标题】如何使 JPanel 中的组件尽可能伸展?【英文标题】:How to make components in a JPanel stretch as far as possible? 【发布时间】:2015-07-08 16:15:57 【问题描述】:我正在尝试创建一个简单的挥杆程序,让用户玩一个圆圈。
这是我的代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
public class SixthProgram
public static void main(String[] args)
GUI prog=new GUI("SixthProgram");
prog.setBounds(350,250,500,250);
prog.setVisible(true);
class GUI extends JFrame implements MouseListener, MouseMotionListener
JPanel colorPan, color1, color2, color3 ,color4 ,color5;
int x=3,y=30; // Position of circle
public GUI(String header)
super(header);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
maker();
addMouseListener(this);
addMouseMotionListener(this);
add(colorPan, BorderLayout.SOUTH);
public void maker()
colorPan = new JPanel();
Border raisedbevel = BorderFactory.createRaisedBevelBorder();
Border loweredbevel = BorderFactory.createLoweredBevelBorder();
Border compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
colorPan.setBorder(compound);
colorPan.setLayout(new FlowLayout(FlowLayout.CENTER));
color1 = new JPanel();
color2 = new JPanel();
color3 = new JPanel();
color4 = new JPanel();
color5 = new JPanel();
color1.setBackground(Color.WHITE);
color2.setBackground(Color.GREEN);
color3.setBackground(Color.RED);
color4.setBackground(Color.BLUE);
color5.setBackground(Color.BLACK);
colorPan.add(color1);
colorPan.add(color2);
colorPan.add(color3);
colorPan.add(color4);
colorPan.add(color5);
@Override
public void paint(Graphics g)
//g.setColor(Color.WHITE);
//g.fillRect(0,0,getWidth(),getHeight());
super.paint(g); //Do the same thing as above(Clear jframe)
g.setColor(Color.RED);
g.fillOval(x,y,50,50);
public void mouseExited(MouseEvent e) //MouseListener overrided methods
System.out.println("Exit");
public void mouseEntered(MouseEvent e)
System.out.println("Enter");
public void mouseReleased(MouseEvent e)
System.out.println("Release");
public void mousePressed(MouseEvent e)
System.out.println("Press");
x=e.getX();
y=e.getY();
if(x+50<getWidth() && y+50<getHeight()) // Preventing out of bounds
repaint();
public void mouseClicked(MouseEvent e) //Press+Release=Click
System.out.println("Click");
System.err.println(((JPanel)e.getSource()).getName());
public void mouseDragged(MouseEvent e) //MouseMotionListener overrided methods
System.out.println("Dragged to ("+ e.getX() +","+ e.getY() +")");
x=e.getX();
y=e.getY();
if((x>=3 && y>=30) && (x+50<getWidth() && y+50<getHeight())) // Preventing out of bounds
repaint();
public void mouseMoved(MouseEvent e)
System.out.println("Moved to ("+ e.getX() +","+ e.getY() +")");
我正在尝试创建具有不同颜色的 JPanel。单击颜色时,圆圈的颜色会发生变化。我还没有实现这部分。
我的问题是,当我运行上面的程序时,我得到的输出是:
但我希望输出是
我试过删除
colorPan.setLayout(new FlowLayout(FlowLayout.CENTER));
这导致了相同的输出。
如何拉伸colorPan
中的组件,以获得预期的输出?
【问题讨论】:
【参考方案1】:您可以使用GridLayout
而不是FlowLayout
,如下所示:
colorPan.setLayout(new GridLayout(1, 5));
这将元素的宽度分为 1 行和 5 列,每种颜色一个。
【讨论】:
谢谢。我可能会添加更多颜色。所以,我将使用colorPan.setLayout(new GridLayout(1, 0));
【参考方案2】:
更改以下内容:
colorPan.setLayout(new FlowLayout(FlowLayout.CENTER));
到将内容拉伸以适应的布局,例如GridLayout
:E.G.
colorPan.setLayout(new GridLayout(1,0));
【讨论】:
【参考方案3】:试试:
colorPan.setLayout(new BoxLayout(colorPan,BoxLayout.LINE_AXIS));
【讨论】:
以上是关于如何使 JPanel 中的组件尽可能伸展?的主要内容,如果未能解决你的问题,请参考以下文章