Java选择布局

发布于 2024-11-09 04:22:31 字数 528 浏览 0 评论 0原文

我需要 Java 中的以下布局:

Layout

但是,我应该发现没有布局管理器可以简单地为我处理这个问题。我需要在 JFrame 中使用此布局。

有什么半简单的方法可以做到这一点吗?

提前致谢!

编辑:谢谢大家,我终于成功了!

这就是我所做的(正如您所提议的)

  • 窗口
  • leftPanel 的 BorderLayout,GridLayout(1,1) /* 拉伸 */,添加组件 1,窗口
  • rightPanel 中的 WEST,BorderLayout,窗口
  • rightTop 中的中心(添加到 rightPanel)作为CENTER)面板,添加组件2
  • rightBottom(添加到rightPanel作为SOUTH)面板,GridLayout(1,1)(也用于拉伸),添加组件3

谢谢大家,我混合了谁的建议^^

I need following layout in Java:

Layout

However, I should find that no layout manager can simply handle this problem for me. I need this layout in a JFrame.

Is there any halfway easy way I could do this?

Thanks in advance!

EDIT: Thanks, all of you, I finally managed!

That's what I've done (as you have proposed)

  • BorderLayout for the window
  • leftPanel, GridLayout(1,1) /* to stretch */, add Component 1, WEST in window
  • rightPanel, BorderLayout, CENTER in window
  • rightTop (added to rightPanel as CENTER) Panel, add Component 2
  • rightBottom (added to rightPanel as SOUTH) Panel, GridLayout(1,1) (also for stretching), add Component 3

Thanks you all, whose advice I have mixed ^^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

箜明 2024-11-16 04:22:31

我知道您通过将 JPanels 放置在 BorderLayout 布局管理器中解决了您的问题。如果这有效的话,那就太好了。

对于那些对使用 GridBagLayout 作为解决方案感到好奇的人,我将此类作为示例。只需复制粘贴、编译并运行。拖动这个小应用程序窗口的一角即可查看调整窗口大小的效果。

示例布局代码的屏幕截图
在此处输入图像描述

// Example code showing the flexibility of GridBagLayout.

// Source code generated by WindowBuilder 1.0.0r37 in Eclipse (Indigo Release) on Mac OS X 10.6.7.

// © 2011 Basil Bourque.   http://www.GridsGoneWild.com/
// This source code may be used freely forever by anyone taking full responsibility for doing so.

// Each layout manager bundled in Java is quite different from the others. They all have strengths and weaknesses,
// each designed for different purposes and effects.

// GridBagLayout is the most powerful and flexible, but takes some patient practice to understand.
// Funny animated video commentary by a GridBagLayout programmer: http://madbean.com/anim/totallygridbag/
// Hand-coding GridBagLayout is certainly tedious, and nearly impossible for complex forms. So I recommend the use of
// a visual GUI builder such as WindowBuilder in Eclipse, JFormDesigner from FormDev.com, or Matisse in NetBeans.

// Key ideas, "grow" & "fill":
// • In WindowBuilder's Design View, select a widget, then use the "Horizontal grow" and "Vertical grow" icons found in the upper right tool bar.
// • Use the widget's (label's, button's) "Constraints" > "fill" property in the property sheet of WindowBuilder.

// Further ideas:
// • To contain multiple widgets in each area, use JPanel objects where I have used single JLabel and JButton objects. 
//   Nesting JPanels inside JPanels (or in the JFrame's contentPane) is considered normal in Swing. Each JPanel has its own layout manager.
// • If need be, you can set the Minimum, Maximum, and/or Preferred size of a widget or JPanel.
// • If you want certain widgets or JPanels to get disproportionately more or less of the space gained or lost when a window is resized, 
//   use "weightx" and "weighty" properties.

// WindowBuilder Tips:
// • If it's your first time: Open the .java file, then click the "Design" button at bottom to see visual editor.
// • Click the "Show Advanced properties" icon at top of a widget's property sheet to see many hidden properties.

// package com.your.package.goes.here;  // Uncomment this line, and modify to suit your own package.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Insets;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GridBagLayout_Example extends JFrame {
    private JPanel contentPane;
    private JLabel lblVariableXVariableY;
    private JButton btnVariableXFixedY;
    private JLabel lblFixedXVariableY;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
        try {
            GridBagLayout_Example frame = new GridBagLayout_Example();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    });
    }

    /**
     * Constructor
     */
    public GridBagLayout_Example() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    this.contentPane = new JPanel();
    this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(this.contentPane);
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[]{0, 0, 0};
    gbl_contentPane.rowHeights = new int[]{0, 0, 0};
    gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
    this.contentPane.setLayout(gbl_contentPane);

    this.lblFixedXVariableY = new JLabel("Fixed x, Variable y");
    this.lblFixedXVariableY.setOpaque(true);
    this.lblFixedXVariableY.setBackground(new Color(233, 150, 122));
    GridBagConstraints gbc_lblFixedXVariableY = new GridBagConstraints();
    gbc_lblFixedXVariableY.fill = GridBagConstraints.VERTICAL;
    gbc_lblFixedXVariableY.gridheight = 2;
    gbc_lblFixedXVariableY.insets = new Insets(0, 0, 5, 5);
    gbc_lblFixedXVariableY.gridx = 0;
    gbc_lblFixedXVariableY.gridy = 0;
    this.contentPane.add(this.lblFixedXVariableY, gbc_lblFixedXVariableY);

    this.lblVariableXVariableY = new JLabel("Variable x & y");
    this.lblVariableXVariableY.setBackground(new Color(147, 112, 219));
    this.lblVariableXVariableY.setOpaque(true);
    GridBagConstraints gbc_lblVariableXVariableY = new GridBagConstraints();
    gbc_lblVariableXVariableY.fill = GridBagConstraints.BOTH;
    gbc_lblVariableXVariableY.insets = new Insets(0, 0, 5, 0);
    gbc_lblVariableXVariableY.gridx = 1;
    gbc_lblVariableXVariableY.gridy = 0;
    this.contentPane.add(this.lblVariableXVariableY, gbc_lblVariableXVariableY);

    this.btnVariableXFixedY = new JButton("Variable x, Fixed y");
    this.btnVariableXFixedY.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) { 
        }
    });
    this.btnVariableXFixedY.setOpaque(true);
    GridBagConstraints gbc_btnVariableXFixedY = new GridBagConstraints();
    gbc_btnVariableXFixedY.fill = GridBagConstraints.HORIZONTAL;
    gbc_btnVariableXFixedY.gridx = 1;
    gbc_btnVariableXFixedY.gridy = 1;
    this.contentPane.add(this.btnVariableXFixedY, gbc_btnVariableXFixedY);
    }

}

I know you solved your problem with a mix of JPanels placed within a BorderLayout layout manager. If that works, that's fine.

For those who are curious about using a GridBagLayout as a solution, I present this class as an example. Simply copy-paste, compile, and run. Drag the corner of this little app's window to see the effects of resizing the window.

Screenshot of example layout code
enter image description here

// Example code showing the flexibility of GridBagLayout.

// Source code generated by WindowBuilder 1.0.0r37 in Eclipse (Indigo Release) on Mac OS X 10.6.7.

// © 2011 Basil Bourque.   http://www.GridsGoneWild.com/
// This source code may be used freely forever by anyone taking full responsibility for doing so.

// Each layout manager bundled in Java is quite different from the others. They all have strengths and weaknesses,
// each designed for different purposes and effects.

// GridBagLayout is the most powerful and flexible, but takes some patient practice to understand.
// Funny animated video commentary by a GridBagLayout programmer: http://madbean.com/anim/totallygridbag/
// Hand-coding GridBagLayout is certainly tedious, and nearly impossible for complex forms. So I recommend the use of
// a visual GUI builder such as WindowBuilder in Eclipse, JFormDesigner from FormDev.com, or Matisse in NetBeans.

// Key ideas, "grow" & "fill":
// • In WindowBuilder's Design View, select a widget, then use the "Horizontal grow" and "Vertical grow" icons found in the upper right tool bar.
// • Use the widget's (label's, button's) "Constraints" > "fill" property in the property sheet of WindowBuilder.

// Further ideas:
// • To contain multiple widgets in each area, use JPanel objects where I have used single JLabel and JButton objects. 
//   Nesting JPanels inside JPanels (or in the JFrame's contentPane) is considered normal in Swing. Each JPanel has its own layout manager.
// • If need be, you can set the Minimum, Maximum, and/or Preferred size of a widget or JPanel.
// • If you want certain widgets or JPanels to get disproportionately more or less of the space gained or lost when a window is resized, 
//   use "weightx" and "weighty" properties.

// WindowBuilder Tips:
// • If it's your first time: Open the .java file, then click the "Design" button at bottom to see visual editor.
// • Click the "Show Advanced properties" icon at top of a widget's property sheet to see many hidden properties.

// package com.your.package.goes.here;  // Uncomment this line, and modify to suit your own package.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Insets;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GridBagLayout_Example extends JFrame {
    private JPanel contentPane;
    private JLabel lblVariableXVariableY;
    private JButton btnVariableXFixedY;
    private JLabel lblFixedXVariableY;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
        try {
            GridBagLayout_Example frame = new GridBagLayout_Example();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    });
    }

    /**
     * Constructor
     */
    public GridBagLayout_Example() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    this.contentPane = new JPanel();
    this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(this.contentPane);
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[]{0, 0, 0};
    gbl_contentPane.rowHeights = new int[]{0, 0, 0};
    gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
    this.contentPane.setLayout(gbl_contentPane);

    this.lblFixedXVariableY = new JLabel("Fixed x, Variable y");
    this.lblFixedXVariableY.setOpaque(true);
    this.lblFixedXVariableY.setBackground(new Color(233, 150, 122));
    GridBagConstraints gbc_lblFixedXVariableY = new GridBagConstraints();
    gbc_lblFixedXVariableY.fill = GridBagConstraints.VERTICAL;
    gbc_lblFixedXVariableY.gridheight = 2;
    gbc_lblFixedXVariableY.insets = new Insets(0, 0, 5, 5);
    gbc_lblFixedXVariableY.gridx = 0;
    gbc_lblFixedXVariableY.gridy = 0;
    this.contentPane.add(this.lblFixedXVariableY, gbc_lblFixedXVariableY);

    this.lblVariableXVariableY = new JLabel("Variable x & y");
    this.lblVariableXVariableY.setBackground(new Color(147, 112, 219));
    this.lblVariableXVariableY.setOpaque(true);
    GridBagConstraints gbc_lblVariableXVariableY = new GridBagConstraints();
    gbc_lblVariableXVariableY.fill = GridBagConstraints.BOTH;
    gbc_lblVariableXVariableY.insets = new Insets(0, 0, 5, 0);
    gbc_lblVariableXVariableY.gridx = 1;
    gbc_lblVariableXVariableY.gridy = 0;
    this.contentPane.add(this.lblVariableXVariableY, gbc_lblVariableXVariableY);

    this.btnVariableXFixedY = new JButton("Variable x, Fixed y");
    this.btnVariableXFixedY.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) { 
        }
    });
    this.btnVariableXFixedY.setOpaque(true);
    GridBagConstraints gbc_btnVariableXFixedY = new GridBagConstraints();
    gbc_btnVariableXFixedY.fill = GridBagConstraints.HORIZONTAL;
    gbc_btnVariableXFixedY.gridx = 1;
    gbc_btnVariableXFixedY.gridy = 1;
    this.contentPane.add(this.btnVariableXFixedY, gbc_btnVariableXFixedY);
    }

}
暗喜 2024-11-16 04:22:31

具有 2 个面板的边框布局,一个是 WEST,一个是 CENTER。在 CENTER 面板上,具有 Center 和 Nrth 组件的 borderLayout 可能会起作用。

A borderlayout with 2 panels, one WEST and one CENTER. On the CENTER panel a borderLayout with both Center and Nrth components might work.

上课铃就是安魂曲 2024-11-16 04:22:31

这不是特别简单,但我认为你必须使用 GridBagLayout。 http://download.oracle.com/javase/tutorial/uiswing/layout /gridbag.html

It isn't particularly simple but I think you have to use GridBagLayout. http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

耳根太软 2024-11-16 04:22:31

我之前的答案的替代方案是 MigLayout。

它是一个外部依赖项(不在 JDK 中),但它是可用的最强大的布局之一。借助 Google 的 WindowBuilder 这样的 GUI 构建器,它也非常易于使用(或使用)。

即使您不想使用 WindowBuilder 创建 GUI,它也肯定可以帮助您入门。

An alternative to my previous answer is MigLayout.

It is an external dependency (it's not in the JDK) but it's one of the most powerfull layouts available. And with a GUI builder like Google's WindowBuilder it's also very easy to use (or play with).

Even if you don't want to create your GUI with WindowBuilder it can sure help you get started.

破晓 2024-11-16 04:22:31

我使用一个带有 JPanel WEST 和一个 EAST 的简单 BorderLayout 来完成此操作。

我的代码

...
inGameFrame.getContentPane ().setLayout (new BorderLayout ());
inGameFrame.getContentPane ().add ("East", pnlRight);
inGameFrame.getContentPane ().add ("South", pnlBottom);
inGameFrame.getContentPane ().add ("Center", pnlGameBoard);
...

如果这不起作用,请尝试设置面板或组件的大小。
(即使我调整 JFrame 的大小,也只有居中面板改变大小)

编辑
您可以尝试的是:向西添加面板,并添加新的面板中心(也是BorderLayout)。然后在新面板上添加另外两个面板,一个中心面板和一个底部面板。这样就能达到想要的效果了。

...
JPanel pnlNew = new JPanel();
frame.getContentPane ().setLayout (new BorderLayout ());
pnlNew.setLayout (new BorderLayout ());
pnlNew.add ("Center", pnlMiddle);
pnlNew.add ("South", pnlBottom);
frame.getContentPane ().add ("West", pnlLeft);
frame.getContentPane ().add ("Center", pnlNew);
...

I got this working with a simple BorderLayout with a JPanel WEST and one EAST.

My code

...
inGameFrame.getContentPane ().setLayout (new BorderLayout ());
inGameFrame.getContentPane ().add ("East", pnlRight);
inGameFrame.getContentPane ().add ("South", pnlBottom);
inGameFrame.getContentPane ().add ("Center", pnlGameBoard);
...

If that does not work, try setting the panels' or components' size.
(Even if I resize the JFrame, only the centered panel changes size)

EDIT
What you can try is: add the panel west, and add a new panel center (also BorderLayout). Then on the new panel add your other two panels, one center and one bottom. That will get the desired effect.

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