我的 JPanel 没有按我想要的方式显示

发布于 2024-12-15 10:43:53 字数 1356 浏览 1 评论 0原文

我试图使我的应用程序的外观看起来像这样:

How i Want my application to Look

但是当我尝试时为了做到这一点,我得到了这样的东西: 我的应用程序的外观

这是我用来创建两个 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:

How i want my application to look

But when i try to make this i get something like this:
How my application looks

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人生戏 2024-12-22 10:43:53

GridLayout 将拉伸每个单元格中的组件以适合单元格的大小,如果您想避免这种拉伸,请将按钮添加到另一个面板并将该面板添加到 GridLayout。

根据我的经验,用 Java 布局用户界面就是混合和匹配布局管理器以实现总体目标。有一些简化的管理器,比如 Mig Layout

对于你的例子,我会做这样的事情:

+----------------------------------------+
| panel1                                 |
|+--------------------------------------+|
|| panel2                               ||
|+--------------------------------------+|
+----------------------------------------+
+----------------------------------------+
| panel3                                 |
|+-----------++-----------++------------+|
|| panel4    || panel5    || panel6     ||
|+-----------++-----------++------------+|
+----------------------------------------+
  • panel1 有一个 BorderLayout
  • panel2 是您的 ImagePanel,并通过 panel1.add(panel2, BorderLayout.CENTER); 添加到 panel1
  • >panel3 是您的GridLayout
  • panel4panel5panel6 都是默认的 (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                                 |
|+--------------------------------------+|
|| panel2                               ||
|+--------------------------------------+|
+----------------------------------------+
+----------------------------------------+
| panel3                                 |
|+-----------++-----------++------------+|
|| panel4    || panel5    || panel6     ||
|+-----------++-----------++------------+|
+----------------------------------------+
  • panel1 has a BorderLayout
  • panel2 is your ImagePanel and added to panel1 via panel1.add(panel2, BorderLayout.CENTER);
  • panel3 is your GridLayout.
  • panel4, panel5, and panel6 are all default (FlowLayout) and each of these JPanels will contain one of your three buttons.

You can then add this to the Content Pane by setting it's Layout to BorderLayout and adding panel1 via getContentPane().add(panel1, BorderLayout.NORTH); and panel3 via getContentPane().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.

乱世争霸 2024-12-22 10:43:53

您需要更好地了解布局管理器。浏览一下这个方便的指南并选择一个布局管理器适合您的需要。

在底部面板上,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.

猫卆 2024-12-22 10:43:53

尝试添加一个中间 JPanel
IE

JPanel appletRunningPanel2Wrapper = new JPanel();
appletRunningPanel2Wrapper.add(appletRunningPanel2);
add(appletRunningPanel2Wrapper);

Try to add an intermediate JPanel
i.e

JPanel appletRunningPanel2Wrapper = new JPanel();
appletRunningPanel2Wrapper.add(appletRunningPanel2);
add(appletRunningPanel2Wrapper);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文