我正在使用窗口构建器来构建一个简单的测试GUI
但是,我遇到了一个问题。
我试图通过附加到按钮的ActionListener将文本附加到文本区域组件。但是,当我在线上写字时,它说
textArea无法解析
我已使用评论标记了相关区域
public class MainWindow {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 457, 435);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 149, 0};
gridBagLayout.rowHeights = new int[]{0, 14, 0, 307, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
JSeparator separator = new JSeparator();
GridBagConstraints gbc_separator = new GridBagConstraints();
gbc_separator.insets = new Insets(0, 0, 5, 0);
gbc_separator.gridx = 1;
gbc_separator.gridy = 0;
frame.getContentPane().add(separator, gbc_separator);
JSeparator separator_1 = new JSeparator();
GridBagConstraints gbc_separator_1 = new GridBagConstraints();
gbc_separator_1.insets = new Insets(0, 0, 5, 5);
gbc_separator_1.gridx = 0;
gbc_separator_1.gridy = 1;
frame.getContentPane().add(separator_1, gbc_separator_1);
//Here is the button.
JButton btnBeginScan = new JButton("Begin Scan");
btnBeginScan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//This is where I get the error.
textArea.append("hello");
}
});
GridBagConstraints gbc_btnBeginScan = new GridBagConstraints();
gbc_btnBeginScan.insets = new Insets(0, 0, 5, 0);
gbc_btnBeginScan.gridx = 1;
gbc_btnBeginScan.gridy = 1;
frame.getContentPane().add(btnBeginScan, gbc_btnBeginScan);
Choice choice = new Choice();
choice.setBackground(SystemColor.scrollbar);
GridBagConstraints gbc_choice = new GridBagConstraints();
gbc_choice.insets = new Insets(0, 0, 5, 0);
gbc_choice.fill = GridBagConstraints.HORIZONTAL;
gbc_choice.gridx = 1;
gbc_choice.gridy = 2;
frame.getContentPane().add(choice, gbc_choice);
//This is the text area.
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Source Sans Pro Light", Font.PLAIN, 12));
textArea.setEditable(false);
textArea.setBackground(SystemColor.inactiveCaption);
GridBagConstraints gbc_textArea = new GridBagConstraints();
gbc_textArea.insets = new Insets(0, 0, 5, 0);
gbc_textArea.fill = GridBagConstraints.BOTH;
gbc_textArea.gridx = 1;
gbc_textArea.gridy = 3;
frame.getContentPane().add(textArea, gbc_textArea);
JButton btnCancelScan = new JButton("Cancel Scan");
GridBagConstraints gbc_btnCancelScan = new GridBagConstraints();
gbc_btnCancelScan.gridx = 1;
gbc_btnCancelScan.gridy = 4;
frame.getContentPane().add(btnCancelScan, gbc_btnCancelScan);
}
}
我非常擅长在java中构建guis,我所知道的只是通过在线教程。我试着搜索我的问题,但我不太了解答案,或者我也没有提出正确的问题。这就是为什么我来这里问这个问题。
答案 0 :(得分:1)
在您拥有操作它的动作侦听器的代码部分之前移动文本区域的定义,否则编译器在处理侦听器代码时将不知道变量textArea
存在,您将获得无法解决符号错误。
final JTextArea textArea = new JTextArea();
// ...
btnBeginScan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.append("hello");
}
});
此外,如果您使用Java 7或更低版本,则必须将textArea
变量声明为final
,以便可以在匿名ActionListener
中访问它。在Java 8中,final
在这种情况下不是必需的,因为textArea
变量是effectively final:
一个变量或参数,其值在初始化后永远不会改变,实际上是最终的。