我的 JPanel 没有按我想要的方式显示
我试图使我的应用程序的外观看起来像这样:
但是当我尝试时为了做到这一点,我得到了这样的东西:
这是我用来创建两个 JPanel 的代码以及如何添加按钮等。
//This is the panel that shows the image
appletRunningPanel = new ImagePanel();
appletRunningPanel.setSize(600, 300);
appletRunningPanel.validate();
//This is the panels that shows the 3 buttons
appletRunningPanel2 = new Panel(new GridLayout(1, 3));
appletRunningPanel2.setSize(600, 300);
appletRunningPanel2.add(test1);
appletRunningPanel2.add(test2);
appletRunningPanel2.add(test3);
appletRunningPanel2.validate();
//Then i add them to the applet with this:
add(appletRunningPanel);
add(appletRunningPanel2);
这是ImagePanel的代码
public class ImagePanel extends JPanel{
private BufferedImage image;
public ImagePanel() {
setSize(600, 300);
try {
image = ImageIO.read(getClass().getResourceAsStream("/res/TCHLogo.png"));
} catch (IOException ex) {
// handle exception...
System.out.println(ex);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
Im trying to make the look of my application to look something like this:
But when i try to make this i get something like this:
Here is the code i use to create my two JPanels and how i add the buttons and soo on..
//This is the panel that shows the image
appletRunningPanel = new ImagePanel();
appletRunningPanel.setSize(600, 300);
appletRunningPanel.validate();
//This is the panels that shows the 3 buttons
appletRunningPanel2 = new Panel(new GridLayout(1, 3));
appletRunningPanel2.setSize(600, 300);
appletRunningPanel2.add(test1);
appletRunningPanel2.add(test2);
appletRunningPanel2.add(test3);
appletRunningPanel2.validate();
//Then i add them to the applet with this:
add(appletRunningPanel);
add(appletRunningPanel2);
Here is the code for ImagePanel
public class ImagePanel extends JPanel{
private BufferedImage image;
public ImagePanel() {
setSize(600, 300);
try {
image = ImageIO.read(getClass().getResourceAsStream("/res/TCHLogo.png"));
} catch (IOException ex) {
// handle exception...
System.out.println(ex);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GridLayout 将拉伸每个单元格中的组件以适合单元格的大小,如果您想避免这种拉伸,请将按钮添加到另一个面板并将该面板添加到 GridLayout。
根据我的经验,用 Java 布局用户界面就是混合和匹配布局管理器以实现总体目标。有一些简化的管理器,比如 Mig Layout
对于你的例子,我会做这样的事情:
panel1
有一个BorderLayout
panel2
是您的ImagePanel
,并通过panel1.add(panel2, BorderLayout.CENTER);
添加到panel1
GridLayout
。panel4
、panel5
和panel6
都是默认的 (FlowLayout
),并且每个JPanel
code>s 将包含三个按钮之一。然后,您可以将其布局设置为
BorderLayout
并通过getContentPane().add(panel1, BorderLayout.NORTH); 添加
panel1
来将其添加到内容窗格中;< /code> 和panel3
通过getContentPane().add(panel3, BorderLayout.SOUTH);
这并不完美,但它会实现更干净的外观 为你。您可以添加更多内容以使事情看起来更好。我最喜欢的布局管理器之一是
BoxLayout
。GridLayout will stretch the component in each cell to fit the size of the cell, if you want to avoid this stretching then add the Buttons to another Panel and add that panel to the
GridLayout
.Laying out a user interface in Java, in my experience, is all about mixing and matching Layout managers to achieve your overall goal. There are some simplified managers out there like Mig Layout
For you example I'd do something like this:
panel1
has aBorderLayout
panel2
is yourImagePanel
and added topanel1
viapanel1.add(panel2, BorderLayout.CENTER);
panel3
is yourGridLayout
.panel4
,panel5
, andpanel6
are all default (FlowLayout
) and each of theseJPanel
s will contain one of your three buttons.You can then add this to the Content Pane by setting it's Layout to
BorderLayout
and addingpanel1
viagetContentPane().add(panel1, BorderLayout.NORTH);
andpanel3
viagetContentPane().add(panel3, BorderLayout.SOUTH);
It's not perfect, but it'll achieve a cleaner look for you. There is a lot more that you can add to make things appear nicer. One of my favorite layout managers is
BoxLayout
.您需要更好地了解布局管理器。浏览一下这个方便的指南并选择一个布局管理器适合您的需要。
在底部面板上,GridLayout 不考虑组件(JButton)的首选大小,它使用网格部分中的所有可用空间。
您可能需要为每个面板使用不同的布局管理器,并为您的小程序框架再次使用另一个布局管理器。
You need to better understand Layout Managers. Have a look through this handy guide and pick a layout manager that suits your need.
On the bottom panel, GridLayout doesn't respect the preferred size of the components (JButtons), it uses all the space available in the grid section.
You may need to use different layout managers for each panel, and another one again for your applet frame.
尝试添加一个中间 JPanel
IE
Try to add an intermediate JPanel
i.e