當用戶從 JList 中選擇序列 ID 時,我正在創建一個基本的 Swing GUI,它在 JTextArea 中顯示非常大的字串(序列)。當序列字串 <= 300,000 個字符長時,JTextArea 正確顯示序列
但是,當序列超過 400,000 個字符時,JTextArea 上顯示的序列被覆寫且難以辨認
如何在 JTextArea 中顯示非常大的字串而不破壞這些大(>=400,000 個字符)字串?
我的代碼:
public class GUI {
private String[] stringArr;
JList<String> idList;
private JTextArea seqArea;
Map<String, String> sequences;
public void init() {
JFrame frame = new JFrame();
JPanel seqPanel = new JPanel();
stringArr = new String[0];
idList = new JList<>();
idList.addListSelectionListener(new SeqListSelectionListener());
idList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroller = new JScrollPane(idList);
idList.setVisibleRowCount(12);
seqPanel.add(scroller);
idList.setListData(stringArr);
seqArea = new JTextArea(15, 50);
seqArea.setLineWrap(true);
seqArea.setCaretPosition(0);
seqArea.setEditable(false);
JScrollPane seqScroller = new JScrollPane(seqArea);
seqScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
seqScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
seqPanel.add(seqScroller, BorderLayout.WEST);
parseFile();
frame.add(seqPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
private void parseFile() {
// function to simulate string that would be parsed from file.
sequences = new LinkedHashMap<>();
StringBuilder seq1 = new StringBuilder();
StringBuilder seq2 = new StringBuilder();
String[] bases = {"A", "C", "T", "G"};
for (int i=0; i < 400000; i ) {
int index1 = (int) Math.floor(Math.random() * 3);
int index2 = (int) Math.floor(Math.random() * 3);
seq1.append(bases[index1]);
seq2.append(bases[index2]);
}
sequences.put("seq1", seq1.toString());
sequences.put("seq2", seq2.toString());
setList(sequences);
}
private void setList(Map<String, String> sequences) {
stringArr = sequences.keySet().toArray(new String[0]);
idList.setListData(stringArr);
}
public class SeqListSelectionListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent le) {
if (!le.getValueIsAdjusting()) {
String chosenSeq = idList.getSelectedValue();
String sequence = sequences.get(chosenSeq);
seqArea.setText("");
seqArea.setText(sequence);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GUI().init());
}
}
uj5u.com熱心網友回復:
據報道,此問題是 Linux 環境中的常見錯誤 ( https://bugs.openjdk.java.net/browse/JDK-8262010 )。
我通過使用解決了這個問題
-Dsun.java2d.xrender=false
運行程式時的 Java VM 選項,它關閉基于 XRender 的 Java 2D 渲染管道。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/404802.html
標籤:
