有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我应该使用什么layoutManager?

我正在创建一个应用程序来测试系统托盘,但我不知道应该使用什么layoutmanager使其看起来像这样:

enter image description here

我遇到的问题是,我无法垂直对齐文本字段中的文本,因此我想:“为什么不使用LayoutManager并使其与消息成比例?”你觉得这个怎么样


共 (3) 个答案

  1. # 1 楼答案

    我使用的最佳布局管理器之一First GridBagLayout可以更灵活地设置调色板的位置(Swing容器、控件)

    public class GridBagLayoutSample {
    JFrame frame = new JFrame("GridBagSample");
    JPanel panel = new JPanel();
    JButton btn1 = new JButton("One");
    JButton btn2 = new JButton("Two");
    JButton btn3 = new JButton("Three");
    JButton btn4 = new JButton("Four");
    JButton btn5 = new JButton("Five");
    public GridBagLayoutSample(){
    panel.setLayout(new GridBagLayout());
    GridBagConstraints a = new GridBagConstraints();
    a.fill = GridBagConstraints.HORIZONTAL;
    a.insets = new Insets(3,3,3,3);
    
    a.gridx = 0;
    a.gridy = 0;
    panel.add(btn1, a);
    
    a.gridx = 1;
    a.gridy = 0;
    panel.add(btn2, a);
    
    a.gridx = 0;
    a.gridy = 1;
    panel.add(btn3, a);
    
    
    a.gridx = 1;
    a.gridy = 1;
    panel.add(btn4, a);
    
    
    frame.add(panel);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setPreferredSize(new Dimension(500,500));
    
    panel.setSize(300,300);
    }
    public static void main(String[] args) {
    GridBagLayoutSample a = new GridBagLayoutSample();
    }
    

    查看此文档。这里解释了GridBagLayout是如何使用的。这是我使用的最好的布局,因为你可以找到你想要的任何位置。使用象限作为指导,这样就不会混淆组件的定位,希望这能有所帮助

    https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

  2. # 2 楼答案

    您可以使用BorderLayout

    为了满足您所需的定位要求,我能想出的最简单的方案是:

    public class PutText extends JFrame {
    
        public PutText() {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            initGUI();
            pack();
    
            setLocationRelativeTo(null);
        }
    
        private void initGUI() {
            Container cp = getContentPane();
            cp.setLayout(new BorderLayout(0, 10));
    
            JPanel upper = new JPanel(new BorderLayout());
            JPanel lower = new JPanel(new BorderLayout());
    
            cp.add(upper, BorderLayout.PAGE_START);
            cp.add(lower, BorderLayout.PAGE_END);
    
            JLabel lbl = new JLabel("Put the text you want the tray to show.");
            JTextArea ta = new JTextArea();
            ta.setLineWrap(true);
            JButton btn = new JButton("Send");
    
            upper.add(lbl, BorderLayout.LINE_START);
            cp.add(ta, BorderLayout.CENTER);
            lower.add(btn, BorderLayout.LINE_END);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                new PutText().setVisible(true);
            });
        }
    
    }
    

    请注意,您需要添加空边框,以便与要创建的程序相似

  3. # 3 楼答案

    简单:使用布局的组合。整体的边框布局,JLabel位于页面的起始位置,JScrollPane/JTextArea位于中间位置。和流动布局。右键使用JPanel在页面结束位置按住JButton

    例如

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class FooPanel extends JPanel {
        private static final String PROMPT = "This is prompt text:";
        private static final int TA_ROWS = 10;
        private static final int TA_COLS = 30;
        private static final int GAP = 5; 
    
        private JTextArea textArea = new JTextArea(TA_ROWS, TA_COLS);
    
        public FooPanel() {
            JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            bottomPanel.add(new JButton("Submit"));
    
            setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
            setLayout(new BorderLayout(GAP, GAP));
            add(new JLabel(PROMPT), BorderLayout.PAGE_START);
            add(new JScrollPane(textArea), BorderLayout.CENTER);
            add(bottomPanel, BorderLayout.PAGE_END);
        }
    
        private static void createAndShowGui() {
            FooPanel mainPanel = new FooPanel();
    
            JFrame frame = new JFrame("FooPanel");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.getContentPane().add(mainPanel);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                createAndShowGui();
            });
        }
    }
    

    或者,底部的JPanel可以使用BoxLayout,通过水平胶水将按钮按在上面:

            JPanel bottomPanel = new JPanel();
            bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
            bottomPanel.add(Box.createHorizontalGlue());
            bottomPanel.add(new JButton("Submit"));
    

    在模式对话框中使用上述内容并展示如何在JTextArea中换行的示例:

    import java.awt.BorderLayout;
    import java.awt.Dialog.ModalityType;
    import java.awt.FlowLayout;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class FooPanel extends JPanel {
        private static final int TA_ROWS = 10;
        private static final int TA_COLS = 30;
        private static final int GAP = 5;
    
        private JTextArea textArea = new JTextArea(TA_ROWS, TA_COLS);
    
        public FooPanel(String prompt) {
            textArea.setWrapStyleWord(true);
            textArea.setLineWrap(true);
    
            JPanel bottomPanel = new JPanel();
            bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
            bottomPanel.add(Box.createHorizontalGlue());
            bottomPanel.add(new JButton(new SendAction("Send", KeyEvent.VK_S)));
    
            setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
            setLayout(new BorderLayout(GAP, GAP));
            add(new JLabel(prompt), BorderLayout.PAGE_START);
            add(new JScrollPane(textArea), BorderLayout.CENTER);
            add(bottomPanel, BorderLayout.PAGE_END);
        }
    
        public String getText() {
            return textArea.getText();
        }
    
        private class SendAction extends AbstractAction {
            public SendAction(String name, int mnemonic) {
                super(name);
                putValue(MNEMONIC_KEY, mnemonic); // alt-key shortcut mnemonic
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                // simply dispose of this window
                Window win = SwingUtilities.getWindowAncestor(FooPanel.this);
                win.dispose();
            }
        }
    
        private static void createAndShowGui() {
            String prompt = "Enter the text that is to be displayed in the tray:";
            final FooPanel mainPanel = new FooPanel(prompt);
    
            final JFrame frame = new JFrame("FooPanel");
            final JTextArea displayArea = new JTextArea(TA_ROWS, TA_COLS);
            displayArea.setFocusable(false);
            displayArea.setEditable(false);
            displayArea.setWrapStyleWord(true);
            displayArea.setLineWrap(true);
    
            final JDialog dialog = new JDialog(frame, "Enter Text", ModalityType.APPLICATION_MODAL);
            dialog.add(mainPanel);
            dialog.pack();
    
            JPanel framePanel = new JPanel(new BorderLayout());
            framePanel.add(new JScrollPane(displayArea), BorderLayout.CENTER);
            framePanel.add(new JPanel() {
                {
                    add(new JButton(new AbstractAction("Show Dialog") {
                        {
                            putValue(MNEMONIC_KEY, KeyEvent.VK_S);
                        }
    
                        public void actionPerformed(ActionEvent e) {
                            dialog.setLocationRelativeTo(frame);
                            dialog.setVisible(true);
    
                            String text = mainPanel.getText();
                            displayArea.setText(text);
                        };
                    }));
                }
            }, BorderLayout.PAGE_END);
    
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.getContentPane().add(framePanel);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                createAndShowGui();
            });
        }
    }