The method showMessageDialog(Component, Object) in the type JOptionPane is not applicable for the arguments (new ActionListener(){}, String)
时间: 2024-03-20 15:42:02 浏览: 19
这个错误通常是因为参数类型不匹配导致的。根据错误提示,可以看到 `showMessageDialog` 方法的参数列表与你所提供的参数不匹配。具体来说,你传递了一个实现了 `ActionListener` 接口的对象和一个字符串,而 `showMessageDialog` 方法的第二个参数应该是一个 `Object` 类型的对象,用于显示在窗口中的消息。
为了解决这个问题,你需要将 `showMessageDialog` 方法的第二个参数改为一个字符串类型的对象,例如:
```java
JOptionPane.showMessageDialog(this, "恭喜过关!");
```
这样就可以将一个字符串显示在窗口中了。如果你想要在弹出窗口中显示更多的内容,可以考虑使用 HTML 标签来格式化文本,例如:
```java
JOptionPane.showMessageDialog(this, "<html><h1>恭喜过关!</h1><p>你已经达到了最高分!</p></html>");
```
这样就可以在弹出窗口中显示一个标题和一些文本了。
相关问题
怎么解决Cannot make a static reference to the non-static method studentUpdate(String) from the type studentDao
这个错误提示表明你正在尝试从 `studentDao` 类的静态方法中调用一个非静态方法 `studentUpdate(String)`。要解决这个问题,有以下两种方法:
1. 创建 `studentDao` 类的对象,然后调用 `studentUpdate` 方法。示例代码如下:
```
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String newpassword = pField.getText();
int c = 0;
if (newpassword.equals("")) {
JOptionPane.showMessageDialog(null, "新密码不能为空", "warning", JOptionPane.PLAIN_MESSAGE);
} else {
studentDao dao = new studentDao();
c = dao.studentUpdate(newpassword);
if (c == 1) {
JOptionPane.showMessageDialog(null, "更新记录成功", "成功", JOptionPane.PLAIN_MESSAGE);
}
}
}
});
```
2. 将 `studentUpdate` 方法改为静态方法。示例代码如下:
```
public class studentDao {
public static int studentUpdate(String newpassword) {
// ...
}
}
```
改为静态方法后,就可以在静态方法中直接调用了。
When the user then clicks on the button, the action listener of the button must read the name of the library user that was typed in the first text field (using the getText method of the text field) and the number of books that was typed in the second text field (using again the getText method) and must call the moreBook method of the controller with these two strings as arguments. The moreBook method of the controller then returns a string as result. If the string returned by the moreBook method of the controller is different from the empty string "" then this string must be displayed back to the user using a message dialog (using the showMessageDialog method of the JOptionPane class). If the string returned by the moreBook method of the controller is equal to the empty string "" then nothing happens in ViewMoreBook.
Here is an example code snippet that implements the functionality you described:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ViewMoreBook extends JFrame {
private JTextField nameTextField;
private JTextField numBooksTextField;
private JButton moreBookButton;
private Controller controller;
public ViewMoreBook(Controller controller) {
this.controller = controller;
initComponents();
}
private void initComponents() {
setTitle("View More Book");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
nameTextField = new JTextField(20);
numBooksTextField = new JTextField(20);
moreBookButton = new JButton("More Book");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.add(new JLabel("Name:"));
panel.add(nameTextField);
panel.add(new JLabel("Number of Books:"));
panel.add(numBooksTextField);
panel.add(moreBookButton);
moreBookButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameTextField.getText();
String numBooks = numBooksTextField.getText();
String result = controller.moreBook(name, numBooks);
if (!result.equals("")) {
JOptionPane.showMessageDialog(ViewMoreBook.this, result);
}
}
});
getContentPane().add(panel);
pack();
setVisible(true);
}
}
```
In this code, we create a `JFrame` with two `JTextField`s for the user to input the name and number of books, respectively, and a `JButton` to trigger the action. We also create an instance variable of `Controller` to call its `moreBook` method later.
In the `initComponents` method, we add the text fields and button to a `JPanel` with a `GridLayout`, and add an `ActionListener` to the button. In the `actionPerformed` method of the listener, we get the text from the text fields using their `getText` methods, call the `moreBook` method of the controller with the two strings as arguments, and check if the result is not an empty string. If it's not empty, we display the result using a `JOptionPane.showMessageDialog`.
Finally, we call `pack` and `setVisible` to display the frame.
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)