gpt4 book ai didi

java - JScrollBar 的样式文档?

转载 作者:太空宇宙 更新时间:2023-11-04 06:33:26 25 4
gpt4 key购买 nike

我想自定义滚动条对象的输出。下面的代码适用于 JTextPane,那么 JScrollBar 的等效代码是什么?

    private JTextPane textPane;

StyledDocument doc = textPane.getStyledDocument();

SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);

最佳答案

如文档 How to Use Scroll Panes 中所述,

A JScrollPane provides a scrollable view of a component. When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically...

.. Here's the code that creates the text area, makes it the scroll pane's client, and adds the scroll pane to a container.

因此,您可以使 JTextPane 成为 JScrollBar 的客户端。

示例代码:

public class Sample extends JFrame {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample frame = new Sample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 300);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(0, 0));
setContentPane(panel);

JTextPane textPane = new JTextPane();
textPane.setEditable(false);

JScrollPane scrollPane = new JScrollPane();
scrollPane
.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.add(textPane);
scrollPane.setViewportView(textPane);
panel.add(scrollPane, BorderLayout.CENTER);

final StyledDocument doc = textPane.getStyledDocument();
insertStringToDoc(getString() + "\n", doc, doc.getLength());

}

private void insertStringToDoc(String str, StyledDocument doc, int offset) {
try {

SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
doc.insertString(doc.getLength(), str, keyWord);
} catch (Exception e) {
e.printStackTrace();
}
}

private String getString() {
return "hello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello "
+ "\nhello hello hello hello hello hello hello hello hello hello hello ";
}
}

关于java - JScrollBar 的样式文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25780348/

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