Tôi muốn có một JTextPane có thanh cuộn, làm thế nào tôi có thể làm như vậy? Cảm ơn.Làm thế nào để có một JTextPane Scrollable?
13
A
Trả lời
17
Để chèn một thanh cuộn trên JTextPane mới của bạn, chỉ cần sử dụng một JScrollPane:
JTextPane txt = new JTextPane();
JScrollPane jsp = new JScrollPane(txt);
JTextPane API: http://download.oracle.com/javase/6/docs/api/javax/swing/JTextPane.html
JScrollPane API: http://download.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html
Nếu bạn có một số vấn đề , hãy xem câu hỏi SO này: Java JTextPane JScrollPane Display Issue
Hoặc hãy xem tại địa chỉ: http://www.daniweb.com/software-development/java/threads/30283
5
Đưa vào JScrollPane. Đọc số swing tutorial about scroll panes.
2
Chỉ cần đặt JTextPane trong JScrollPane.
public class SomeFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane tp = new JTextPane();
JScrollPane sp = new JScrollPane(tp);
frame.getContentPane().add(sp);
frame.pack();
frame.setVisible(true);
}
}
0
Trước đó chỉ cần thêm một ScrollPane để ContentPane trong thiết kế và thêm EditopPane để ScrollPane như con
JScrollPane sp = (JScrollPane)contentPane.getComponent(23);//this is in my hierarchy 23
JViewport vp = sp.getViewport();
JEditorPane ep = (JEditorPane)vp.getView();
0
Đây là đoạn mã để thêm một thanh cuộn để TextBox
JEditorPane edtDTWinfo = new JEditorPane();
edtDTWinfo.setEditable(false);
edtDTWinfo.setBorder(new LineBorder(Color.ORANGE, 2));
edtDTWinfo.setForeground(Color.BLUE);
JScrollPane spEditor = new JScrollPane(edtDTWinfo,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
spEditor.setBounds(0, 0, 200, 300);
Thêm thành phần "spEditor" vào JPanel