可以使用equals()方法来比较标签和文本框中的字符串。然后根据比较结果在标签中显示相应的信息。
示例代码:
JLabel lblResult = new JLabel(); JTextField txtInput = new JTextField(); String labelText = "Hello World"; lblResult.setText(labelText);
txtInput.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Get text from input text field String inputText = txtInput.getText();
// Check if input text matches label text
if(labelText.equals(inputText)){
lblResult.setText("Matched!");
} else {
lblResult.setText("Not matched!");
}
}
});
数组的加法操作可以在循环中使用加法运算符来实现。首先创建一个包含要相加的数组的数组。然后,在循环中遍历此数组并累加每个元素的值。最后输出总和。
示例代码:
int[] arr1 = {1, 2, 3, 4, 5}; int[] arr2 = {6, 7, 8, 9, 10}; int[] sum = new int[5];
for (int i = 0; i < arr1.length; i++) { sum[i] = arr1[i] + arr2[i]; }
for (int i = 0; i < sum.length; i++) { System.out.println(sum[i]); }
输出:7 9 11 13 15