java - 框架的最佳布局

标签 java swing layout awt layout-manager

我应该使用什么样的布局来创建这样的页面: 它应该可以调整大小 它有两个主面板 Right and Left

Example of GUI

最佳答案

“主文本”文本区域将获得额外空间,按钮面板居中时将获得额外高度。

End-Of-Line Button Layout

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class EndOfLineButtonLayout {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));

                JPanel textPanel = new JPanel(new BorderLayout(5,5));
                textPanel.add(new JScrollPane(new JTextArea("Top Text",3,20)), 
                        BorderLayout.PAGE_START);
                textPanel.add(new JScrollPane(new JTextArea("Main Text",10,10)));
                gui.add(textPanel, BorderLayout.CENTER);

                JPanel buttonCenter = new JPanel(new GridBagLayout());
                buttonCenter.setBorder(new EmptyBorder(5,5,5,5));
                JPanel buttonPanel = new JPanel(new GridLayout(0,1,5,5));
                for (int ii=1; ii<6; ii++) {
                    buttonPanel.add(new JButton("Button " + ii));
                }
                // a component added to a GBL with no constraint will be centered
                buttonCenter.add(buttonPanel);

                gui.add(buttonCenter, BorderLayout.LINE_END);

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

关于java - 框架的最佳布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17883338/

相关文章:

java - 动态创建 FormLayout 行和列(在运行时)

html - 使用 html、div 和 css 的带有登录框的 3 行布局

java - JVM设计决策

java - 如何在 Android 中以编程方式检查蓝牙网络共享状态

java - 如何用 txt 文件中的某些词填充 JList

java - 如何在可运行的jar文件中运行音乐

java - 使用 if、else if、else 重写 java 中的 switch 语句

java - 如何在 Eclipse-Helios JDT 中专门抑制 "Comparing identical expressions"

java - JOptionPane 操作监听器在退出时发布

html - 我的响应式导航栏在移动设备上没有响应(溢出问题?)