我是java&的新手我正在尝试制作聊天GUI。 我在尝试将JScrollPane添加到我的JTextArea时遇到了问题,这里是代码:
public class Test extends JFrame实现了ActionListener,MouseListener,KeyListener,WindowListener {
private static JFrame frame;
private static JTextField typeField;
private static JTextArea textArea;
private static JScrollPane scroll;
private static JButton b1;
private static int width = 500, height = 800;
private static String appTitle = "test";
public Test(){
// frame
frame = new JFrame();
frame.setSize(height, width);
frame.setResizable(false);
frame.setTitle(appTitle);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// type field
typeField = new JTextField();
typeField.setFont(new Font("Arial", Font.PLAIN, 13));
typeField.addKeyListener(this);
typeField.setSize(440, 27);
typeField.setLocation(9, 435);
typeField.addActionListener(this);
// text area
textArea = new JTextArea();
textArea.setFont(new Font("Arial", Font.PLAIN, 13));
textArea.setEditable(false);
textArea.setSize(464, 400);
textArea.setLocation(10, 30);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
// button
b1 = new JButton("^");
b1.addMouseListener(this);
b1.setSize(41, 26);
b1.setLocation(453, 435);
// scrollpane
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setSize(18, 400);
scroll.setLocation(476, 31);
// add elements
frame.add(typeField);
frame.add(textArea);
frame.add(scroll);
frame.add(b1);
frame.setVisible(true);
}
public static void main (String args[]){
new Test();
}
public void textSent() {
String senttxt = typeField.getText();
if (senttxt.equals("")) {
} else {
textArea.append("{User}: " + senttxt + "\n\n");
typeField.setText("");
}
}
public void actionPerformed(ActionEvent e) {
textSent();
}
@Override
public void mouseClicked(MouseEvent e) {
textSent();
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
滚动条在那里但是当文本填满文本区域时,我无法向下滚动。 有人可以帮我这个吗?谢谢!