gpt4 book ai didi

java - 我应该使用什么布局管理器?

转载 作者:行者123 更新时间:2023-12-01 07:21:28 25 4
gpt4 key购买 nike

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

enter image description here

我遇到的问题是我无法垂直对齐 textField 中的文本,所以我想:“为什么不使用布局管理器并使其随消息缩放。”对此您有何看法?

最佳答案

简单:使用布局的组合。整体的 BorderLayout,JLabel 位于 PAGE_START 位置,JScrollPane/JTextArea 位于 CENTER 位置。以及在 PAGE_END 位置使用 JPanel 并持有 JButton 的 FlowLayout.RIGHT。

例如,

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

关于java - 我应该使用什么布局管理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35972773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com