帮我改写的能运行起来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(); } }
时间: 2024-03-06 16:49:40 浏览: 120
相关问题资料\java\AWT-组件.ppt
改写后的代码如下:
```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("张三");
}
}
```
改写后的代码主要是对变量名和方法名进行规范化、重构,同时对布局管理器进行了修改,更加灵活。程序的功能并没有变化,仅仅是代码更加规范、易读。
阅读全文