gpt4 book ai didi

java - 在滚动 Pane 中将滚动条设置为 "always on"

转载 作者:行者123 更新时间:2023-12-02 09:08:40 26 4
gpt4 key购买 nike

我有两个滚动 Pane ,我正在尝试将滚动条设置为始终可见。由于某种原因,当我尝试使用下面的代码(滚动代码)时,出现错误“JTextPane 类型的方法 setHorizo​​ntalScrollBarPolicy(int) 未定义”。有什么建议吗?

滚动代码:

scrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

我创建滚动 Pane 的代码:

JPanel ScrollPanes = new JPanel();
frame.getContentPane().add(ScrollPanes, BorderLayout.CENTER);
ScrollPanes.setLayout(new GridLayout(0, 2, 0, 0));

JTextPane textPane_0 = new JTextPane();
ScrollPanes.add(textPane_0);
textPane_0.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
textPane_0.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

JTextPane textPane_1 = new JTextPane();
ScrollPanes.add(textPane_1);

完整代码:

package swing;

import java.awt.EventQueue;

public class Swing {

private JFrame frame;
private JTextField textField;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Swing window = new Swing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Swing() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel TopApps = new JPanel();
frame.getContentPane().add(TopApps, BorderLayout.NORTH);
TopApps.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));


JComboBox comboBox = new JComboBox();
comboBox.setPreferredSize(new Dimension(100, 20));
TopApps.add(comboBox);

textField = new JTextField();
TopApps.add(textField);
textField.setColumns(20);

JScrollPane scrollPanes = new JScrollPane();
frame.getContentPane().add(scrollPanes, BorderLayout.CENTER);
scrollPanes.setLayout(new GridLayout(0, 2, 0, 0));

JTextPane textPane_0 = new JTextPane();
scrollPanes.add(textPane_0);
textPane_0.setBorder(BorderFactory.createLoweredBevelBorder());
scrollPanes.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPanes.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

JTextPane textPane_1 = new JTextPane();
scrollPanes.add(textPane_1);
textPane_1.setBorder(BorderFactory.createLoweredBevelBorder());
scrollPanes.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPanes.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

JPanel bottomPanel = new JPanel();
frame.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
bottomPanel.setLayout(new GridLayout(2, 1, 5, 0));
bottomPanel.setBorder(BorderFactory.createLoweredBevelBorder());

JTextPane bText = new JTextPane();
bottomPanel.add(bText);
bText.setBorder(BorderFactory.createLoweredBevelBorder());

JLabel status = new JLabel("Status");
bottomPanel.add(status);
status.setHorizontalAlignment(JLabel.CENTER);

}

}

最佳答案

textPane_0JTextPane 的实例,而不是 JScrollPane,因此它没有 setHorizo​​ntalScrollBarPolicy 方法。

您可能希望将 textPane_0textPane_1 包装在 JScrollPane 的新实例中,设置其滚动条属性并将它们添加到框架中。

换句话说,使用

JTextPane textPane_0 = new JTextPane();
JScrollPane jScrollPane1 = new JScrollPane(textPane_0);//warp text pane
//and set properties of wrapper
jScrollPane1.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ScrollPanes.add(jScrollPane1);


JTextPane textPane_1 = new JTextPane();
JScrollPane jScrollPane2 = new JScrollPane(textPane_1);
jScrollPane2.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane2.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ScrollPanes.add(jScrollPane2);

演示:

class Demo {
public static void main(String[] args) {

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);

JPanel ScrollPanes = new JPanel();
frame.getContentPane().add(ScrollPanes, BorderLayout.CENTER);
ScrollPanes.setLayout(new GridLayout(0, 2, 0, 0));

JTextPane textPane_0 = new JTextPane();
JScrollPane jScrollPane1 = new JScrollPane(textPane_0);
jScrollPane1.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ScrollPanes.add(jScrollPane1);


JTextPane textPane_1 = new JTextPane();
JScrollPane jScrollPane2 = new JScrollPane(textPane_1);
jScrollPane2.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane2.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ScrollPanes.add(jScrollPane2);


frame.setVisible(true);


}
}

关于java - 在滚动 Pane 中将滚动条设置为 "always on",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24441423/

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