Я использую построитель окон для создания простого тестового графического интерфейсаActionListener не может разрешить компонент
Однако у меня проблема.
Я пытаюсь добавить текст в компонент текстовой области с помощью 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);
}
}
Я очень новый создание guis в java, все, что я знаю, через онлайн-учебники. Я попытался найти свою проблему, но я не совсем понял ответы, или, может быть, я не задал правильный вопрос. Вот почему я пришел сюда, чтобы спросить об этом.
Вы пытались переместить определение кнопки после 'textArea'? –