Swing 和 JPanels、布局管理器
几周前我(第一次)尝试使用 swing,并且我自己尝试了一下以更好地感受它。我似乎遇到了垂直对齐的问题。
情况是这样的:我在边框布局的西部有一个 boxlayout(Y_AXiS) JPanel。在带有 boxlayout 的 JPanel 内部,我还有另外两个 JPanel,我希望它们垂直对齐(无论框架的尺寸如何,都推向屏幕顶部)。然而,布局管理器似乎在两个 JPanel 之间放置了一个很大的垂直空间。我在想也许网格布局更适合这个,但我不确定。我曾多次尝试通过谷歌搜索答案并纠正问题,但没有成功。请帮忙!
package com.granet;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class CustomerOrganizer extends JFrame
{
public CustomerOrganizer()
{
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.darkGray);
JLabel customerSlogan = new JLabel("Customer Organizer");
topPanel.add(customerSlogan);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.darkGray);
leftPanel.setBorder(new EmptyBorder(10,100,00,0));
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
DrawPanel firstTab = new DrawPanel(Color.white, 350, 50);
DrawPanel secondTab = new DrawPanel(Color.white, 350, 50);
JLabel firstTabText = new JLabel("First Tab");
JLabel secondTabText = new JLabel("Second Tab");
firstTab.setBorder(new EmptyBorder(0,60,60,0));
secondTab.setBorder(new EmptyBorder(0,60,60,0));
firstTab.add(firstTabText);
secondTab.add(secondTabText);
leftPanel.add(firstTab);
leftPanel.add(secondTab);
firstTab.setAlignmentX(Component.LEFT_ALIGNMENT);
secondTab.setAlignmentX(Component.LEFT_ALIGNMENT);
/* DOESN'T WORK, I'm pretty sure this changes the point on the jpanel which used for alignment (top, bottom, left or right)
firstTab.setAlignmentY(Component.TOP_ALIGNMENT);
secondTab.setAlignmentY(Component.TOP_ALIGNMENT);
*/
add(topPanel, BorderLayout.NORTH);
add(leftPanel, BorderLayout.WEST);
pack();
}
public static void main(String[] args)
{
CustomerOrganizer frame = new CustomerOrganizer();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,400);
frame.setVisible(true);
}
}
DrawPanel.java
package com.granet;
import javax.swing.*;
import java.awt.*;
public class DrawPanel extends JPanel
{
Color color;
int width;
int height;
public DrawPanel(Color color, int width, int height)
{
this.color=color;
this.width=width;
this.height=height;
}
public DrawPanel()
{
this.color = Color.darkGray;
this.width=this.getWidth();
this.height=this.getHeight();
}
public void paintComponent(Graphics g)
{
g.setColor(color);
g.fillRect(0, 0, width, height);
}
}
这是我运行它时看到的: http://concertforhope.net/citeforme/javabug.png
请注意,底部选项卡未推送靠在顶部标签上。
NVM,我明白为什么了。选项卡不是那么小,我以错误的方式填充背景(白色框并不代表实际的选项卡)。感谢您的帮助。
I've tried working with swing (for the first time) a few weeks ago and I ventured on my own for a bit to get a better feel for it. I seem to be running into a problem with vertical alignment.
Here is the situation: i have a boxlayout(Y_AXiS) JPanel in the West part of a borderlayout. Inside the JPanel w/ boxlayout, I have two other JPanels which I want to be vertically aligned (pushed towards the top of the screen no matter the dimensions of the frame). However, the layout manager seems to put a big vertical space between the two JPanels. I was thinking maybe a gridlayout would be better suited for this, but am unsure. I've tried numerous times to google for an answer and to correct the problem with no luck. Help Please!
package com.granet;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class CustomerOrganizer extends JFrame
{
public CustomerOrganizer()
{
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.darkGray);
JLabel customerSlogan = new JLabel("Customer Organizer");
topPanel.add(customerSlogan);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.darkGray);
leftPanel.setBorder(new EmptyBorder(10,100,00,0));
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
DrawPanel firstTab = new DrawPanel(Color.white, 350, 50);
DrawPanel secondTab = new DrawPanel(Color.white, 350, 50);
JLabel firstTabText = new JLabel("First Tab");
JLabel secondTabText = new JLabel("Second Tab");
firstTab.setBorder(new EmptyBorder(0,60,60,0));
secondTab.setBorder(new EmptyBorder(0,60,60,0));
firstTab.add(firstTabText);
secondTab.add(secondTabText);
leftPanel.add(firstTab);
leftPanel.add(secondTab);
firstTab.setAlignmentX(Component.LEFT_ALIGNMENT);
secondTab.setAlignmentX(Component.LEFT_ALIGNMENT);
/* DOESN'T WORK, I'm pretty sure this changes the point on the jpanel which used for alignment (top, bottom, left or right)
firstTab.setAlignmentY(Component.TOP_ALIGNMENT);
secondTab.setAlignmentY(Component.TOP_ALIGNMENT);
*/
add(topPanel, BorderLayout.NORTH);
add(leftPanel, BorderLayout.WEST);
pack();
}
public static void main(String[] args)
{
CustomerOrganizer frame = new CustomerOrganizer();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,400);
frame.setVisible(true);
}
}
DrawPanel.java
package com.granet;
import javax.swing.*;
import java.awt.*;
public class DrawPanel extends JPanel
{
Color color;
int width;
int height;
public DrawPanel(Color color, int width, int height)
{
this.color=color;
this.width=width;
this.height=height;
}
public DrawPanel()
{
this.color = Color.darkGray;
this.width=this.getWidth();
this.height=this.getHeight();
}
public void paintComponent(Graphics g)
{
g.setColor(color);
g.fillRect(0, 0, width, height);
}
}
and this is what I see when I run it:
http://concertforhope.net/citeforme/javabug.png
Notice that the bottom tab is not pushed up against the top tab.
NVM, I see why. The tabs weren't that small, I was filling in the background the wrong way (the white boxes didn't represent the actual tabs). Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是一个错误。盒子布局应该在组件之间平均划分整个空间。
您可能想要一个垂直的流程布局或类似的东西。
我真的建议您获取 Google WindowBuilder(免费)并尝试一下GUI 中的布局。
在您的特殊情况下,您还可以考虑 JTabbedPane,选项卡位置位于右侧。
This is not a bug. A boxlayout is supposed to divide the entire space equally between the components.
You'd probably want a flowlayout vertically or something like that.
I'd really suggest you get Google WindowBuilder (free) and play a bit with layouts in the GUI.
In your special case you could also consider JTabbedPane with a tab placement on the right.
您还可以考虑使用不可见组件作为填充 控制垂直间距。例如,
org.gcs.kinetic.ControlPanel
< /a> 使用Box.createVerticalStrut()
,而这个 示例使用Box .createVerticalGlue()
。You might also consider Using Invisible Components as Filler to control the vertical spacing. For example,
org.gcs.kinetic.ControlPanel
usesBox.createVerticalStrut()
, while this example usesBox.createVerticalGlue()
.