![entrer la description de l'image ici][1]J'ai la structure suivante
JFrame
JPanel
JScrollPane
class that extends JTextPane
Après avoir ajouté un texte long, le JScrollPane cache le JTextPane.
J'utilise la mise en page Gridbag.
J'ai DefaultStyledDocument dans clss qui étend JTextPane.
J'ai défini la taille maximale du JTextPane avec la commande
text.setMaximumSize(new Dimension(100,50));
paquet com.example ;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
public class Test extends JFrame{
IPanel p;
public Test() {
p = new IPanel();
p.init();
setTitle("Test");
setBounds(50, 50, 200, 200);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(p);
setVisible(true);
}
public static void main(String[] args) {
Test t = new Test();
}
}
class IPanel extends JPanel{
public void init() {
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gridbag = new GridBagLayout();
JLabel subjectLabel = new JLabel("Text");
gbc.weighty = 0.0;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.anchor = GridBagConstraints.NORTH;
gridbag.setConstraints(subjectLabel, gbc);
add(subjectLabel);
TextPane Text = new TextPane(255);
Text.setMaximumSize(new Dimension(100,20));
Text.setPreferredSize(new Dimension(100,20));
gridbag.setConstraints(Text, gbc);
JScrollPane subScrollPane = new JScrollPane(Text);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 0.0;
gbc.insets = new Insets(0, 0, 5, 0);
gridbag.setConstraints(subScrollPane, gbc);
subScrollPane.setWheelScrollingEnabled(false);
add(subScrollPane);
}
}
class TextPane extends JTextPane{
private Doc doc = new Doc();
public TextPane(int lenght) {
doc.setmaxLength(lenght);
}
}
class Doc extends DefaultStyledDocument{
private int maxLength = 0;
public void setmaxLength(int lenght) {
maxLength = lenght;
}
}
Voici un exemple de code