[英]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.