JPanels:Java世界的<div>?

时间:2017-04-16 05:52:39

标签: java swing jpanel

可以在Java中使用JPanels,如在<div>中使用HTML吗?在JFrame中为CardLayout以外的其他东西设置多个JPanel是否与良好做法一致? (例如,如果我的JPanel包含一个位于JFrame内容面板北边界的帮助/约按钮,我使用包含该按钮的按钮放置一些间距以强制按钮为一定的规模...)

具体案例:

假设我正在尝试这样做:enter image description here,在开始时,我有一个这样的模拟(感谢我在其他地方找到的按钮渲染的一些代码):enter image description here。在这种情况下,使用多个JFrame会是一个好例子吗(就像那些HTML <div>那样)?如果不是,什么时候会?

1 个答案:

答案 0 :(得分:1)

根据您的要求,您不必“使用”多个面板,您可以从单个面板和GridBagLayout完成您想要的任务,例如......

Single Panel

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JButton helpButton = new JButton("?");
            JTextField projectDirectory = new JTextField(20);
            JTextField documentDirectory = new JTextField(20);
            JButton projectButton = new JButton("Project");
            JButton documentButton = new JButton("Document");
            JButton continueButton = new JButton("Continue to files");

            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 1;

            add(new JLabel("Project Directory"), gbc);
            gbc.gridy++;
            add(new JLabel("Documentation Directory"), gbc);

            gbc.gridx = 1;
            gbc.gridy = 1;
            add(projectDirectory, gbc);
            gbc.gridy++;
            add(documentDirectory, gbc);

            gbc.gridx = 2;
            gbc.gridy = 0;
            add(helpButton, gbc);
            gbc.gridy++;
            add(projectButton, gbc);
            gbc.gridy++;
            add(documentButton, gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(continueButton, gbc);
        }

    }

}

我将使用多个面板的主要原因是:

  1. 布局过于复杂,它可以让我将布局细分为个别需求,并更多地关注群体之间的关系
  2. 如果我专注于将管理分成单个元素(例如目录选择),那么面板可以成为一个独立的工作单元,包括管理和功能,允许自我包含和重新-usable组件
  3. 复合面板......

    Multiple Components

    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                JButton helpButton = new JButton("?");
                JButton continueButton = new JButton("Continue to files");
    
                setLayout(new GridBagLayout());
    
                GridBagConstraints gbc = new GridBagConstraints();
    
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.anchor = GridBagConstraints.EAST;
                add(helpButton, gbc);
    
                gbc.gridy++;
                add(new FolderSelectionPane("Project Folder"), gbc);
                gbc.gridy++;
                add(new FolderSelectionPane("Documentation Folder"), gbc);
                gbc.anchor = GridBagConstraints.CENTER;
                gbc.gridy++;
                add(continueButton, gbc);
            }
    
        }
    
        public class FolderSelectionPane extends JPanel {
    
            public FolderSelectionPane(String label) {
                JTextField projectDirectory = new JTextField(20);
                JButton projectButton = new JButton("Folder");
    
                setLayout(new GridBagLayout());
    
                GridBagConstraints gbc = new GridBagConstraints();
    
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.insets = new Insets(0, 2, 0, 2);
                add(new JLabel(label), gbc);
    
                gbc.gridx++;
                add(projectDirectory, gbc);
    
                gbc.gridx++;
                add(projectButton, gbc);
            }
    
        }
    
    }