gpt4 book ai didi

Java JScrollPane 滚动到底部......几乎

转载 作者:行者123 更新时间:2023-12-01 11:52:48 26 4
gpt4 key购买 nike

我的 Java 程序中有一个带有 JPanel 的 ScrollPane,它附加了大量文本。我需要 ScrollPane 在每次添加后滚动到最底部。我对以下代码的问题是它“几乎”滚动到底部但不是一直滚动。关于我做错了什么有什么想法吗?

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class Sample extends JFrame{
JTextField input = new JTextField();
JPanel log = new JPanel();
JScrollPane scrollPane = new JScrollPane(log);
JScrollBar scrollBar = scrollPane.getVerticalScrollBar();

public Sample(){

input.setBorder(new TitledBorder("Message"));
input.setToolTipText("Type a message here.");

log.setBorder(new TitledBorder("Log"));
log.setLayout(new BoxLayout(log, BoxLayout.Y_AXIS));
log.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

// Add the panels to the frame
setLayout(new BorderLayout(10, 10));
add(input, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);

setTitle("Chat Window");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

// Register Key listener
input.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
log.add(new ServerBubble(input.getText()));
log.add(Box.createRigidArea(new Dimension(0, 10)));
input.setText(null);
scrollPane.revalidate();
scrollBar.setValue(scrollBar.getMaximum());
}
}
});
}

public static void main(String[] args) {
JFrame frame = new Exercise33_9Server();

}

private class ServerBubble extends JLabel{

private final Color LIGHT_GREEN = new Color(204, 255, 229);
private final Color DARK_GREEN = new Color(0, 102, 51);
String message;

public ServerBubble(String label){
message = processText(label);
setOpaque(true);
setForeground(DARK_GREEN);
setBackground(LIGHT_GREEN);
setHorizontalAlignment(LEFT);
setBorder(new LineBorder(DARK_GREEN, 3, true));
setText(label);
setBorder(BorderFactory.createCompoundBorder(getBorder(), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
}

private String processText(String message){
message = "<HTML>" + message;
return message;
}
}
}

最佳答案

基本上,您可能会在组件层次结构有时间更新之前尝试更新视口(viewport)。有时候,你只需要等待。

此示例基本上使用 SwingUtilities.invokeLater 向事件队列发出请求,要求“在不久的将来的某个时间”执行某些工作。这允许处理当前挂起的更新时间...

if (e.getKeyCode() == KeyEvent.VK_ENTER) {

log.add(new ServerBubble(input.getText()));
log.add(Box.createRigidArea(new Dimension(0, 10)));
input.setText(null);
log.revalidate();

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Dimension vpSize = scrollPane.getViewport().getExtentSize();
Dimension logSize = log.getSize();

int height = logSize.height - vpSize.height;
scrollPane.getViewport().setViewPosition(new Point(0, height));
}
});
}

此时,我获取 View 的当前大小并将其与日志面板的当前大小进行比较,然后使用不同的值来计算距面板底部的 y 偏移量,这将允许最后一个元素可见...

关于Java JScrollPane 滚动到底部......几乎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28666648/

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