gpt4 book ai didi

java - 如何将 JScrollBar 添加到 Java 窗口生成器中的 JTextArea?

转载 作者:行者123 更新时间:2023-12-02 10:21:30 24 4
gpt4 key购买 nike

好的,所以,我正在尝试向我的文本区域添加滚动条。我注意到在窗口生成器中,有一个名为 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);
}
}

最佳答案

您可以使用JScrollPane代替JScrollBar

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

关于java - 如何将 JScrollBar 添加到 Java 窗口生成器中的 JTextArea?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54320514/

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