【发布时间】:2015-05-14 11:57:58
【问题描述】:
第一次用 Java 编写 GUI
到目前为止,当我单击“坐标异常”时,JTextArea 中会显示一个字符串数组列表,但查看文本的唯一方法是突出显示它。
我还尝试向 JTextArea 添加滚动条,但没有成功。
JTextArea textArea = new JTextArea();
textArea.setBounds(10, 79, 172, 339);
frame.getContentPane().add(textArea);
JButton btnNewButton_1 = new JButton("Coordinate Anomalies");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ArrayList<String> anomalies = vessels.coordinateAnomaly();
JScrollPane jp = new JScrollPane();
jp.setViewportView(textArea);
for (String a : anomalies) {
textArea.append(a + "\n");
}
textArea.setBounds(10, 79, 172, 339);
frame.getContentPane().add(textArea);
}
});
btnNewButton_1.setBounds(10, 45, 172, 23);
frame.getContentPane().add(btnNewButton_1);
这是我的代码(很抱歉出现问题),我对 GUI 不熟悉,因此我的压力再怎么强调也不为过,任何建议都非常感谢。
【问题讨论】:
-
Layout managers 是你的朋友
-
Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。