java类属性改变_java中关于其他类方法改变主类属性的一些存在过的疑虑和自己的理解...

package a;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.util.List;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class ModifyStudentView {

JTextField nameText;

JTextField sexText;

JTextField ageText;

List list;

JFrame frame;

int a;

File file = new File("C:/Users/cxjaq/Desktop/stu.txt");

StudentTableModel model;

public ModifyStudentView(List list, StudentTableModel model,int a) {

this.list = list;

this.model = model;

this.a=a;

}

public void init() {

frame = new JFrame();

frame.setSize(300, 400);

frame.setLocationRelativeTo(null);

frame.setTitle("修改学生");

JPanel mainPanel = (JPanel) frame.getContentPane();

BoxLayout boxLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);

mainPanel.setLayout(boxLayout);

JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

JPanel panel4 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

mainPanel.add(panel1);

mainPanel.add(panel2);

mainPanel.add(panel3);

mainPanel.add(panel4);

// 姓名

JLabel nameLabel = new JLabel();

nameLabel.setText("姓名");

panel1.add(nameLabel);

nameText = new JTextField();

nameText.setPreferredSize(new Dimension(120, 30));

nameText.setText(list.get(a).getName());

panel1.add(nameText);

// 性别

JLabel sexLabel = new JLabel();

sexLabel.setText("性别");

panel2.add(sexLabel);

sexText = new JTextField();

sexText.setPreferredSize(new Dimension(120, 30));

sexText.setText(list.get(a).getSex());

panel2.add(sexText);

//年龄

JLabel ageLabel = new JLabel();

ageLabel.setText("年龄");

panel3.add(ageLabel);

ageText = new JTextField();

ageText.setPreferredSize(new Dimension(120, 30));

ageText.setText(String.valueOf(list.get(a).getAge()));

panel3.add(ageText);

JButton saveBtn = new JButton();

saveBtn.setText("保存");

saveBtn.setPreferredSize(new Dimension(90, 30));

panel4.add(saveBtn);

saveBtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String name = nameText.getText();

String sex = sexText.getText();

int age = Integer.parseInt(ageText.getText());

list.get(a).setName(name);

list.get(a).setSex(sex);

list.get(a).setAge(age);

frame.dispose();

save();

model.fireTableDataChanged();

JOptionPane.showMessageDialog(null, "修改成功","标题",JOptionPane.WARNING_MESSAGE);

}

});

frame.setVisible(true);

}

public void save() {

try {

FileOutputStream fos = new FileOutputStream(file);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(list);

oos.close();

fos.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

主类Student.java

List list = new ArrayList();

File file = new File("C:/Users/cxjaq/Desktop/stu.txt");

StudentTableModel model;

JTextField nameText;

JTextField sexText;

JTextField ageText;

int a=1;

JTable table ;

String name;

String sex;

String age;

JButton modifyBtn = new JButton();

modifyBtn.setText("修改");

modifyBtn.setPreferredSize(new Dimension(90, 30));

panel3.add(modifyBtn);

modifyBtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

//a=table.getSelectedRow();

//ModifyStudentView msv = new ModifyStudentView(list, model,a);

//msv.init();

try {

a=table.getSelectedRow();

ModifyStudentView msv = new ModifyStudentView(list, model,a);

msv.init();

} catch (ArrayIndexOutOfBoundsException e2) {

// TODO: handle exception

JOptionPane.showMessageDialog(null, "请选中修改项","标题",JOptionPane.WARNING_MESSAGE);

}

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值