saveButton.addActionListener(new ActionListener() {中的 ActionListener没有被调用
时间: 2024-06-02 09:10:33 浏览: 7
可能是因为您没有正确添加 ActionListener 到 saveButton 上。请确保您已经使用 `saveButton.addActionListener()` 方法将 ActionListener 添加到按钮上,并且方法中包含了正确的实现代码。
如果您已经这样做了,但是 ActionListener 仍然没有被调用,可能是因为您的代码存在其他问题。您可以尝试在 ActionListener 实现代码中添加一些日志或调试语句,以便更好地了解程序的执行流程和任何可能的问题。
相关问题
用JFrame和ActionListener类来实现
以下是使用`JFrame`和`ActionListener`类实现的Java代码:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JLabel resultLabel;
public Main() {
super("计算器");
setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("请输入一个正整数n:"));
textField = new JTextField(10);
inputPanel.add(textField);
JButton calculateButton = new JButton("计算");
calculateButton.addActionListener(this);
inputPanel.add(calculateButton);
add(inputPanel, BorderLayout.NORTH);
JPanel resultPanel = new JPanel();
resultLabel = new JLabel();
resultPanel.add(resultLabel);
JButton saveButton = new JButton("保存");
saveButton.addActionListener(this);
resultPanel.add(saveButton);
add(resultPanel, BorderLayout.CENTER);
JButton closeButton = new JButton("关闭");
closeButton.addActionListener(this);
add(closeButton, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("计算")) {
try {
int n = Integer.parseInt(textField.getText());
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
resultLabel.setText("1到" + n + "的和为:" + sum);
} catch (NumberFormatException ex) {
resultLabel.setText("请输入一个正整数!");
}
} else if (e.getActionCommand().equals("保存")) {
if (resultLabel.getText().equals("")) {
resultLabel.setText("请先计算!");
} else {
try {
String fileName = textField.getText() + ".txt";
PrintWriter printWriter = new PrintWriter(new FileWriter(fileName));
printWriter.println(resultLabel.getText());
printWriter.close();
resultLabel.setText(resultLabel.getText() + " 已保存到 " + fileName);
} catch (IOException ex) {
resultLabel.setText("保存失败!");
ex.printStackTrace();
}
}
} else if (e.getActionCommand().equals("关闭")) {
dispose();
}
}
public static void main(String[] args) {
new Main();
}
}
```
该程序创建了一个`JFrame`窗口,并分别在窗口的北、中、南面板中添加了输入、输出和关闭按钮。在输入面板中,使用`JTextField`类实现了用户输入正整数的功能,并添加了一个计算按钮,点击该按钮会计算1到n的和并在输出面板中显示结果。在输出面板中,使用`JLabel`类显示计算结果,并添加了一个保存按钮,点击该按钮会将计算结果保存到用户指定的文件中。在关闭按钮中,添加了关闭窗口的功能。
在`Main`类中实现了`ActionListener`接口,并重写了`actionPerformed()`方法来响应用户点击按钮的事件。根据不同的事件源,执行不同的操作。在计算事件中,使用`try-catch`块来捕获用户输入的非整数情况,并在输出面板中显示相应的错误信息。在保存事件中,首先判断计算结果是否为空,如果为空则提示用户先进行计算;否则,将计算结果保存到用户指定的文件中,并在输出面板中显示保存信息。在关闭事件中,使用`dispose()`方法关闭窗口。
如何在jtable表中编辑数据并保存
要在JTable中编辑数据并保存,可以按照以下步骤进行操作:
1. 创建JTable对象并设置TableModel。
```java
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
```
2. 向TableModel中添加数据。
```java
model.addRow(new Object[]{"John", "Doe", 25});
model.addRow(new Object[]{"Jane", "Doe", 30});
```
3. 设置JTable的编辑模式为可编辑。
```java
table.setEditable(true);
```
4. 添加一个按钮,用于保存数据。
```java
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 在此处编写保存数据的代码
}
});
```
5. 在按钮的ActionListener中编写保存数据的代码。可以使用TableModel的getValueAt()方法来获取单元格中的值,并将其保存到文件或数据库中。
```java
public void actionPerformed(ActionEvent e) {
for (int row = 0; row < model.getRowCount(); row++) {
String firstName = model.getValueAt(row, 0).toString();
String lastName = model.getValueAt(row, 1).toString();
int age = Integer.parseInt(model.getValueAt(row, 2).toString());
// 在此处将数据保存到文件或数据库中
}
}
```
6. 将按钮添加到容器中,并显示窗口。
```java
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
frame.getContentPane().add(saveButton, BorderLayout.SOUTH);
frame.setSize(400, 300);
frame.setVisible(true);
```
以上就是在JTable中编辑数据并保存的基本步骤。您可以根据需要进行修改和扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](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)