JFrame添加滚动条 [英] JFrame add scrollbar

查看:1280
本文介绍了JFrame添加滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一些教程并查看了其他答案,但它似乎仍然没有帮助,我想向该类似控制台的JTextArea添加滚动条,并保留该属性,以便每行的新行文字会增加其余部分.

I've tried a few tutorials and had a look at other answers but it still doesn't seem to help me, I want to add a scrollbar to this console-like JTextArea, and keep the property whereby each new line of text will push the rest up.

这是当前的样子:

和代码:

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    int width = 600;
    int height = 400;

    JFrame frame = new JFrame("ChanDown");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JTextField textField = new JTextField();
    textField.setHorizontalAlignment(JTextField.LEFT) ;



    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width, height);
    frame.setResizable(false);

    JTextArea console = new JTextArea();
    console.setBackground(Color.DARK_GRAY);
    console.setForeground(Color.white);
    console.setMargin(new Insets(0,10,10,10));
    console.setLineWrap(true);

    panel.add(console, BorderLayout.NORTH);

    panel.add(textField, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.SOUTH);

    frame.getContentPane().setBackground(Color.DARK_GRAY);

    frame.setVisible(true);

推荐答案

尝试类似的方法

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class ChanDown extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextArea textArea;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChanDown frame = new ChanDown();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ChanDown() {
        setForeground(Color.WHITE);
        setBackground(Color.DARK_GRAY);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 381);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JScrollPane scrollPane = new JScrollPane();

        textField = new JTextField();
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == 10) {
                    textArea.append(textField.getText() + "\n");
                    textField.setText("");
                    textField.requestFocus();
                }
            }
        });
        textField.setColumns(10);
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
                .addComponent(textField, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.TRAILING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(textField, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        );

        textArea = new JTextArea();
        textArea.setBackground(Color.DARK_GRAY);
        textArea.setForeground(Color.WHITE);
        textArea.setAlignmentY(Component.BOTTOM_ALIGNMENT);
        textArea.setEditable(false);
        scrollPane.setViewportView(textArea);
        contentPane.setLayout(gl_contentPane);
    }
}

这篇关于JFrame添加滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆