Как я могу добавить JScrollBar в мой JTextArea в Java построителе окон? - PullRequest
0 голосов
/ 23 января 2019

Хорошо, я пытаюсь добавить полосу прокрутки в свою текстовую область.Я заметил, что в построителе окон есть объект с именем JScrollBar, который я могу использовать.Я уже добавил его в свою программу, однако у меня возникают трудности с привязкой их к конкретной области текста.(У меня есть два разных textAreas, и я создал два объекта ScrollBar.)

Может кто-нибудь показать мне, как связать JScrollBar с JtextArea?

Спасибо!

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JPanel;

public class HelloWorldGUI {

    private JFrame frmHelloWorldProgram;
    private JTextField chatBox1;
    private JTextField chatBox2;
    private String message;

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

    /**
     * Create the application.
     */
    public HelloWorldGUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmHelloWorldProgram = new JFrame();
        frmHelloWorldProgram.setTitle("Chat with yourself");
        frmHelloWorldProgram.getContentPane().setBackground(SystemColor.inactiveCaption);
        frmHelloWorldProgram.getContentPane().setForeground(Color.WHITE);
        frmHelloWorldProgram.setBounds(100, 100, 866, 491);
        frmHelloWorldProgram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmHelloWorldProgram.getContentPane().setLayout(null);

        JScrollBar scrollBar2 = new JScrollBar();
        scrollBar2.setBounds(778, 56, 21, 253);
        frmHelloWorldProgram.getContentPane().add(scrollBar2);

        JScrollBar scrollBar1 = new JScrollBar();
        scrollBar1.setBounds(366, 56, 21, 253);
        frmHelloWorldProgram.getContentPane().add(scrollBar1);

        JTextArea textArea2 = new JTextArea();
        textArea2.setEditable(false);
        textArea2.setBackground(SystemColor.window);
        textArea2.setBounds(459, 56, 340, 253);
        frmHelloWorldProgram.getContentPane().add(textArea2);

        JTextArea textArea1 = new JTextArea();
        textArea1.setEditable(false);
        textArea1.setBackground(SystemColor.window);
        textArea1.setBounds(47, 56, 340, 253);
        frmHelloWorldProgram.getContentPane().add(textArea1);

        chatBox1 = new JTextField();
        chatBox1.setText("Please enter your text here");
        chatBox1.setBounds(47, 343, 340, 22);
        frmHelloWorldProgram.getContentPane().add(chatBox1);
        chatBox1.setColumns(10);

        chatBox2 = new JTextField();
        chatBox2.setText("Please enter your text here");
        chatBox2.setColumns(10);
        chatBox2.setBounds(459, 343, 340, 22);
        frmHelloWorldProgram.getContentPane().add(chatBox2);

        JButton btnSend1 = new JButton("Send");
        btnSend1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                message = chatBox1.getText();
                String messageD = message + "\n";
                textArea2.append(messageD);
            }
        });
        btnSend1.setBounds(173, 389, 97, 25);
        frmHelloWorldProgram.getContentPane().add(btnSend1);

        JButton btnSend2 = new JButton("Send");
        btnSend2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                message = chatBox2.getText();
                String messageD = message + "\n";
                textArea1.append(messageD);
            }
        });
        btnSend2.setBounds(594, 389, 97, 25);
        frmHelloWorldProgram.getContentPane().add(btnSend2);

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setIcon(new ImageIcon(HelloWorldGUI.class.getResource("/images/Image.jpg")));
        lblNewLabel.setBounds(0, 0, 848, 444);
        frmHelloWorldProgram.getContentPane().add(lblNewLabel);
    }
}

Ответы [ 2 ]

0 голосов
/ 23 января 2019

Вы можете просто изменить эту строку в своем коде:

frmHelloWorldProgram.getContentPane().add(textArea2);

на

frmHelloWorldProgram.getContentPane().add(new JScrollPane(textArea2));

(Аналогично для других текстовых областей также)

0 голосов
/ 23 января 2019

Вместо JScrollBar вы можете использовать JScrollPane .

JTextArea textArea2 = new JTextArea();
textArea2.setEditable(false);
textArea2.setBackground(SystemColor.window);
textArea2.setBounds(459, 56, 340, 253);
//frmHelloWorldProgram.getContentPane().add(textArea2);
JScrollPane scrollBar2 = new JScrollPane(textArea2); // like this
scrollBar2.setBounds(778, 56, 330, 240);
frmHelloWorldProgram.getContentPane().add(scrollBar2);
...