JPanel fills all JFrame space(JPanel 填充所有 JFrame 空间)
问题描述
我已经编写了这段代码来显示面板中的一些颜色:
I have write this code for displaying some set of colors from panel:
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Palette {
public static void main(String[] args) {
JFrame frame = new JFrame("Panel");
palette.add(new Color(255, 0, 0));
palette.add(new Color(0, 255, 0));
palette.add(new Color(0, 0, 255));
int width = 100;
int height = 250;
int x = 0;
for (Color color : palette) {
JPanel panel = new JPanel();
panel.setSize(width, height);
panel.setLocation(x, 750);
panel.setBackground(new java.awt.Color(color.getColor()));
frame.add(panel);
x+=width;
}
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 1000);
frame.setVisible(true);
}
}
两个前面板在正确的位置和正确的尺寸.但最后一个用蓝色填充所有帧.怎么了?
Two first panels in the right place and with right dimensions. But the last one fills all frame in blue color. What's wrong?
推荐答案
你必须使用合适的布局管理器.默认情况下,JFrame 具有 BorderLayout.
You have to use the appropriate Layout Manager. A JFrame by default has a BorderLayout.
查看 LayoutManagers 教程:
Check out this tutorial for LayoutManagers:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
这篇关于JPanel 填充所有 JFrame 空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JPanel 填充所有 JFrame 空间
基础教程推荐
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
