[英]Make JTextArea scrollable in Swing
我怎樣才能使我的JTextArea
可滾動。 我要使它的keysDisplay
有一個滾動條,可用於滾動文本區域。 我已經跳過了jFramePrint()
一些未實現的代碼。
public class ApplicationViewer extends JFrame {
private JTabbedPane tabs = new JTabbedPane();
private JTextArea keyGenDisplay = new JTextArea();
private JTextArea keysDisplay = new JTextArea();
private JPanel controlPanel = new JPanel();
private JButton addNumber = new JButton("Add Number");
private JButton addLetter = new JButton("Add Letter");
private JButton addHyphen = new JButton("Add Hyphen");
private JButton calculateButton = new JButton("Calculate Key");
private JTextField amountField = new JTextField("", 6);
private JLabel amountLabel = new JLabel(" Amount of Keys : ");
private JScrollPane scroll = new JScrollPane(keysDisplay);
public void jFramePrint() {
this.setLayout(new BorderLayout());
this.add(controlPanel, BorderLayout.SOUTH);
controlPanel.add(addNumber);
controlPanel.add(addLetter);
controlPanel.add(addHyphen);
controlPanel.add(amountLabel);
controlPanel.add(amountField);
controlPanel.add(calculateButton);
this.add(scroll);
this.setSize(1400, 900);
this.setTitle("Key Generator");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
keyGenDisplay.append("Key Format: ");
keysDisplay.append("Keys Here: ");
tabs.add("Key Generator", keyGenDisplay);
tabs.add("Keys", keysDisplay);
this.add(tabs);
this.setVisible(true);
}
}
private JTextArea keysDisplay = new JTextArea();
首先,您應該使用類似:
private JTextArea keysDisplay = new JTextArea(5, 20);
這將允許文本區域計算自己的首選大小。 將滾動條添加到滾動窗格后,它將正常工作,並且在文本區域添加了5行以上的文本。
this.add(scroll);
...
this.add(tabs);
您的框架正在使用BorderLayout
。 當您不使用約束時,默認情況下會使用“ CENTER。
您不能將多個組件添加到BorderLayout
的相同區域。 因此,僅顯示最后添加的組件。
為選項卡組件指定其他約束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.