Java 我应该使用什么layoutManager?

Java 我应该使用什么layoutManager?,java,swing,layout-manager,Java,Swing,Layout Manager,我正在创建一个应用程序来测试系统托盘,但我不知道应该使用什么layoutmanager使其看起来像这样: 我遇到的问题是,我无法垂直对齐文本字段中的文本,因此我想:“为什么不使用LayoutManager并使其与消息一起缩放。”您对此有何看法?简单:使用布局的组合。整体的边框布局,JLabel位于页面的起始位置,JScrollPane/JTextArea位于中间位置。和一个FlowLayout.RIGHT,使用JPanel在页面结束位置按住JButton e、 g 或者,底部JPanel可以使

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


我遇到的问题是,我无法垂直对齐文本字段中的文本,因此我想:“为什么不使用LayoutManager并使其与消息一起缩放。”您对此有何看法?

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

e、 g

或者,底部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();
        });
    }
}

您可以使用
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);
        });
    }

}

请注意,您需要添加空边框以类似于要创建的程序。

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

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


你看过了吗?是的,我看过了,但是我没有找到我想要的经理,或者我无法让它看起来像图片。请使用多种布局组合。整体的边框布局,JLabel位于页面的起始位置,JScrollPane/JTextArea位于中间位置。和一个FlowLayout.RIGHT,使用JPanel在页面末端位置按住JButton。我可能会使用SpringLayout/BorderLayout,但这是因为我用它们绘制了简单的Android布局的等价物。要在JTextArea中包装单词,请在JTextArea上调用方法,
setWrapStyleWord(true)
setLineWrap(true)
。请看编辑我的答案,这是一个很好的例子,但我喜欢我的框架尺寸。谢谢您的帮助。@xX4m4zingXx:“框架尺寸”应该由文本组件本身的大小决定。如果使用JTextArea,那么这是通过设置JTextArea的行和列属性来实现的。我只是想演示如何简单地定位元素。@hoverCraftFullOfels您的意思是:当JTextArea中的文本变得比JTextArea的尺寸大时,它应该变得更大?Dinomarion10我还以为你已经展示了,谢谢。@xX4m4zingXx:你还需要调用
ta.setWrapStyleWord(true)
,这样“行将被包装在单词边界上”(根据)。我会看一看的,谢谢。我认为版面经理是最好的,还是不是?我更喜欢这个版面经理。它比自由设计更灵活。我使用的最常见的布局是边界、流和网格布局。并尝试查看面板过渡的卡片布局。如果这有助于你投票支持我。
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);
        });
    }

}
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();
}