btnOk.addActionListener(this);
时间: 2023-08-07 16:07:53 浏览: 124
这段代码是为按钮添加一个监听器,用于捕获按钮的单击事件。在这个例子中,`btnOk` 是一个按钮对象,通过调用 `addActionListener()` 方法,将当前对象(通常是实现了 `ActionListener` 接口的对象)注册为按钮的监听器。
当用户单击按钮时,就会触发 `actionPerformed()` 方法,该方法是 `ActionListener` 接口的唯一方法,用于处理按钮单击事件。在 `actionPerformed()` 方法中,可以编写相应的处理逻辑,例如弹出对话框、执行业务逻辑等。
通过为按钮添加监听器,可以使按钮实现交互功能,例如提交表单、保存数据等。在实际开发中,通常会为多个组件添加监听器,以实现复杂的交互功能。
相关问题
帮我改写的能运行起来package com.swingtest; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class StudentManger extends JFrame implements ActionListener { JLabel lstname = new JLabel("学生姓名: "); JTextField tf_name = new JTextField(12); JLabel lmajor = new JLabel("专业"); JTextField tf_major = new JTextField(); JButton btnOK = new JButton("添加"); JButton btnDelete = new JButton("删除"); JButton btnQuit = new JButton("退出"); JTable table; DefaultTableModel model; public void studentManger(String name) { // TODO Auto-generated constructor stub JLabel welcome = new JLabel(name +",欢迎登录!"); setTitle("学生管理"); setSize(400,400); welcome.setBounds(50,20,200,20); lstname.setBounds(50,20,200,20); tf_name.setBounds(150,50,100,20); lmajor.setBounds(50,80,100,20); tf_major.setBounds(150,80,100,20); btnOK.setBounds(80,110,60,20); btnDelete.setBounds(150,110,60,20); btnQuit.setBounds(220,110,60,20); Container c = getContentPane(); JPanel panel = new JPanel(); panel.setLayout(null); panel.add(welcome); panel.add(lstname); panel.add(tf_name); panel.add(lmajor); panel.add(tf_major); panel.add(btnOK); panel.add(btnDelete); panel.add(btnQuit); String[] colName = {"姓名","专业"}; model = new DefaultTableModel(colName,0); table = new JTable(model); JScrollPane scrollPane = new JScrollPane(table); c.setLayout(new SpringLayout()); c.add(panel); c.add(scrollPane); setLocationRelativeTo(null); setVisible(true); btnOK.addActionListener(this); btnDelete.addActionListener(this); btnQuit.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Object ob = e.getSource(); if(ob == btnQuit) { System.exit(0); }else if(ob ==btnOK) { String[] stuInfo = {tf_name.getText(),tf_major.getText()}; model.addRow(stuInfo); tf_name.setText(""); tf_major.setText(""); }else if(ob == btnDelete) { if(table.getSelectedRow()<0) { JOptionPane.showMessageDialog(null,"请在表格中选中要删除的行");} else { model.removeRow(table.getSelectedRow()); } }} public static void main(String[] args) { new StudentManger(); } }
改写后的代码如下:
```java
package com.swingtest;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class StudentManager extends JFrame implements ActionListener {
private JLabel welcomeLabel;
private JLabel nameLabel;
private JTextField nameTextField;
private JLabel majorLabel;
private JTextField majorTextField;
private JButton addButton;
private JButton deleteButton;
private JButton quitButton;
private JTable table;
private DefaultTableModel model;
public StudentManager(String name) {
welcomeLabel = new JLabel(name + ",欢迎登录!");
nameLabel = new JLabel("学生姓名: ");
nameTextField = new JTextField(12);
majorLabel = new JLabel("专业");
majorTextField = new JTextField();
addButton = new JButton("添加");
deleteButton = new JButton("删除");
quitButton = new JButton("退出");
String[] colName = {"姓名", "专业"};
model = new DefaultTableModel(colName, 0);
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
setTitle("学生管理");
setSize(400, 400);
setLocationRelativeTo(null);
setVisible(true);
welcomeLabel.setBounds(50, 20, 200, 20);
nameLabel.setBounds(50, 50, 100, 20);
nameTextField.setBounds(150, 50, 100, 20);
majorLabel.setBounds(50, 80, 100, 20);
majorTextField.setBounds(150, 80, 100, 20);
addButton.setBounds(80, 110, 60, 20);
deleteButton.setBounds(150, 110, 60, 20);
quitButton.setBounds(220, 110, 60, 20);
Container c = getContentPane();
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(welcomeLabel);
panel.add(nameLabel);
panel.add(nameTextField);
panel.add(majorLabel);
panel.add(majorTextField);
panel.add(addButton);
panel.add(deleteButton);
panel.add(quitButton);
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
c.add(panel);
c.add(scrollPane);
addButton.addActionListener(this);
deleteButton.addActionListener(this);
quitButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Object ob = e.getSource();
if (ob == quitButton) {
System.exit(0);
} else if (ob == addButton) {
String[] stuInfo = {nameTextField.getText(), majorTextField.getText()};
model.addRow(stuInfo);
nameTextField.setText("");
majorTextField.setText("");
} else if (ob == deleteButton) {
if (table.getSelectedRow() < 0) {
JOptionPane.showMessageDialog(null, "请在表格中选中要删除的行");
} else {
model.removeRow(table.getSelectedRow());
}
}
}
public static void main(String[] args) {
new StudentManager("张三");
}
}
```
改写后的代码主要是对变量名和方法名进行规范化、重构,同时对布局管理器进行了修改,更加灵活。程序的功能并没有变化,仅仅是代码更加规范、易读。
var btn = new Button("选择文件夹"); btn.onClick = function() { var folder = Folder.selectDialog("选择文件夹"); if (folder) { var fileList = folder.getFiles(); var list = []; for (var i = 0; i < fileList.length; i++) { if (fileList[i] instanceof File) { list.push(fileList[i]); } } var dlg = new Window("dialog", "文件列表"); dlg.orientation = "column"; var lst = dlg.add("listbox", undefined, list); var btnOK = dlg.add("button", undefined, "确定"); btnOK.onClick = function() { var selectedItem = lst.selection; if (selectedItem) { var file = selectedItem.text; // 在这里编写将文件导入ae的脚本 alert(file + "导入成功!"); } else { alert("请选择一个文件!"); } dlg.close(); } dlg.show(); } }
这是一段 JavaScript 代码,用于实现一个 Adobe After Effects 脚本界面,用于选择一个文件夹,并将该文件夹下的所有文件展示在一个列表框中。用户可以选择列表框中的任意一个文件,并执行将该文件导入 After Effects 的操作。如果用户没有选择任何文件,则会提示用户选择文件。你有什么关于这段代码的问题吗?
阅读全文