好的,因此,我正在尝试向文本区域添加滚动条。我已经注意到,在窗口构建器中,可以使用一个名为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);
}
}
答案 0 :(得分:1)
您可以使用 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);
答案 1 :(得分:0)
您可以在代码中简单地更改此行:
frmHelloWorldProgram.getContentPane().add(textArea2);
到
frmHelloWorldProgram.getContentPane().add(new JScrollPane(textArea2));
(与其他文本区域相似)