我第一次使用JFrame,使用JScrollPane时遇到问题。 我需要编写21个DNA序列,我创建了一个JSplitPane,左边是ID,右边是序列。我想把每个序列(大约1500个字符)放在一行上。我确定一张照片会比我的解释更好:以下的pciture正是我需要的,如果我放入15个第一个DNA序列,它就能完美运作!
但是如果我再添加一个......这是一场灾难:每个序列都写在几行上,我几乎完全丢失了水平卷轴: picture 2 : result if I add another sequence
以下是代码:
public class FenetreAlignement extends JFrame {
private JSplitPane split;
private MyTextPane textPaneSeq = new MyTextPane();
private MyTextPane textPaneID = new MyTextPane();
private JScrollPane scroll;
private JPanel panId = new JPanel(new BorderLayout());
private JPanel panSeq = new JPanel(new BorderLayout());
private ArbreAlignement arbre;
}
public FenetreAlignement(ArbreAlignement arbre) {
this("ALignement multiple de sequences",300000,300000,false,arbre);
}
public FenetreAlignement(String titre, int largeur, int hauteur, boolean boolExit, ArbreAlignement arbre) {
this.arbre = arbre;
this.setTitle(titre);
this.setSize(largeur,hauteur);
/* centrage */
this.setLocationRelativeTo(null);
if(boolExit == true)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
else
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
int taille;
char c;
HashMap<Integer,SequenceAlignement> sequencesInitiales = arbre.getSequences();
ArrayList<String> seq = arbre.getResultatFinal();
StyledDocument doc = textPaneSeq.getStyledDocument();
StyledDocument docID = textPaneID.getStyledDocument();
Color cA = new Color(64,128,255);
Attribut styleA = new Attribut("Courier new",18,cA);
Attribut styleT = new Attribut("Courier new",18,Color.PINK);
Attribut styleG = new Attribut("Courier new",18,Color.CYAN);
Attribut styleC = new Attribut("Courier new",18,Color.MAGENTA);
Attribut styleDef = new Attribut("Courier new",18,Color.WHITE);
try {
for(String s : seq) {
taille = s.length();
for(int i = 0; i < taille; i++) {
c = s.charAt(i);
if(c == 'A')
doc.insertString(doc.getLength(),"A",styleA);
else if(c == 'G')
doc.insertString(doc.getLength(),"G",styleG);
else if(c == 'C')
doc.insertString(doc.getLength(),"C",styleC);
else if(c == 'T')
doc.insertString(doc.getLength(),"T",styleT);
else
doc.insertString(doc.getLength(),Character.toString(c),styleDef);
}
doc.insertString(doc.getLength(),"\n\n\n",styleDef);
}
catch(Exception e){}
try {
for (HashMap.Entry<Integer, SequenceAlignement> entry :sequencesInitiales.entrySet()) {
docID.insertString(docID.getLength(),entry.getValue().getId(),styleDefaut);
docID.insertString(docID.getLength(),"\n\n\n",styleDefaut);
}
}
catch (BadLocationException e){}
scroll = new JScrollPane(textPaneSeq,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panId, panSeq);
split.setDividerLocation(60);
split.setRightComponent(scroll);
split.setLeftComponent(textPaneID);
this.getContentPane().add(split);
this.setVisible(true);
}
我已经阅读了很多主题,但没有什么能解决这个问题。我也试过这个:
public boolean getScrollableTracksViewportWidth() {
return getUI().getPreferredSize(this).width <= getParent().getSize().width;
}
非常感谢您的帮助或任何想法。