java - 在 JTextArea 下面添加按钮

标签 java swing user-interface jtextarea

我尝试使用 Eclipse WindowBuilder 在 JTextArea 下添加两个按钮,但我做不到。我尝试更改布局,但找不到一种方法来在我想要的位置添加按钮并以简单的方式重新调整 JTextArea 的大小。

public TestScrollPane03() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JTextArea textArea = new JTextArea(100, 50);
                JScrollPane scrollPane = new JScrollPane(textArea);
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(scrollPane);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    } 

我将如何在我原来的 textArea 下方添加按钮?

最佳答案

您需要有两个面板,一个用于您的 textArea,另一个用于您的输入(在本例中为按钮)。我认为您正在寻找这样的东西:

enter image description here

public class Test
{
    public static void createFrame()
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                JPanel panel = new JPanel();
                panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
                panel.setOpaque(true);
                JTextArea textArea = new JTextArea(15, 50);
                textArea.setWrapStyleWord(true);
                textArea.setEditable(false);
                textArea.setFont(Font.getFont(Font.SANS_SERIF));
                JScrollPane scroller = new JScrollPane(textArea);
                scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                JPanel inputpanel = new JPanel();
                inputpanel.setLayout(new FlowLayout());
                JTextField input = new JTextField(20);
                JButton button = new JButton("Enter");
                DefaultCaret caret = (DefaultCaret) textArea.getCaret();
                caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
                panel.add(scroller);
                inputpanel.add(input);
                inputpanel.add(button);
                panel.add(inputpanel);
                frame.getContentPane().add(BorderLayout.CENTER, panel);
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
                frame.setResizable(false);
                input.requestFocus();
            }
        });
    }

    public static void main(String... args)
    {
        createFrame();
    }
}

如果你想让你的框架看起来更像你正在运行的操作系统的框架,你可以在使 frame 可见之前添加 .setLookAndFeel():

try 
{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
    e.printStackTrace();
}

添加 UIManager 的样子(明显更小):

enter image description here

关于java - 在 JTextArea 下面添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15455655/

相关文章:

java - 取消 ThreadPoolExector 中的等待任务

java - 更改 GridBagLayout 中的网格大小

java - 为什么我不能将 EmptyBorder 添加到自定义 JComponent?

excel - 在 Excel 电子表格中捕获 Click 事件

java - Java Swing UI 属性列表?

c++ - 使用工厂模式来实例化所有小部件是否明智?

Java 等待线程完成

java - 如何使用 Java 存储过程显示表数据?

java - 透明面板上透明图像的旋转

java - 如何在其余代码之前执行一行代码?更改按钮上的文本