我正在做一个项目而且我遇到了困难。我想将一个变量从一个类传递给另一个类。这两个班级张贴在下面。
我在谈论变量“private int index”,我希望从我的ActionListener到另一个ActionListener。这可以通过将其设置为静态来实现,但这意味着我必须将其设为“公共”,并且我尝试使用getter和setter来实现它,但它不能正常工作。
你能帮帮我吗?ShowMembersGUI.java
package gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import domains.Member;
import mappers.MemberMapper;
public class ShowMembersGUI {
private JFrame showMembersFrame;
private JButton editMemberBtn, closeBtn, removeMemberBtn;
private JTable table;
private JScrollPane scrollPane;
private JPanel p;
private Object id, fName, lName;
private int index;
public ShowMembersGUI() {
MemberMapper mMapper = new MemberMapper();
List<Member> data = mMapper.selectAllMembers();
final MyTableModel m = new MyTableModel(data);
table = new JTable(m);
scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
DefaultTableCellRenderer leftRenderer = new DefaultTableCellRenderer();
leftRenderer.setHorizontalAlignment(JLabel.LEFT);
table.getColumnModel().getColumn(1).setCellRenderer(leftRenderer);
editMemberBtn = new JButton("Rediger Medlem");
editMemberBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
// int column = table.getSelectedColumn();
id = (Object) table.getValueAt(row, 0);
index = (Integer) id;
JOptionPane.showMessageDialog(null, index);
EditMemberGUI editMemberGUI = new EditMemberGUI();
editMemberGUI.showGUI();
}
});
removeMemberBtn = new JButton("Fjern Medlem");
removeMemberBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
// int column = table.getSelectedColumn();
id = (Object) table.getValueAt(row, 0);
fName = (Object) table.getValueAt(row, 1);
lName = (Object) table.getValueAt(row, 2);
Object[] options = { "Ja", "Nej" };
int n = JOptionPane
.showOptionDialog(null,
"Er du sikker du vil slette medlem?\n\n"
+ "Medlemsnr: " + id + "\n\n" + fName
+ " " + lName, "Advarsel",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);
if (n == JOptionPane.YES_OPTION) {
MemberMapper mMapper = new MemberMapper();
int i = (Integer) id;
mMapper.removeMember(i);
m.removeMemberFromTable(row);
m.fireTableDataChanged();
} else {
}
}
});
closeBtn = new JButton("Close");
closeBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
showMembersFrame.setVisible(false);
}
});
p = new JPanel(new FlowLayout());
p.add(editMemberBtn);
p.add(removeMemberBtn);
p.add(closeBtn);
}
public JTable getTable() {
return table;
}
public void setTable(JTable table) {
this.table = table;
}
public void showGUI() {
showMembersFrame = new JFrame();
showMembersFrame.setTitle("Vis Medlemmer");
showMembersFrame.setLayout(new BorderLayout());
showMembersFrame.setSize(900, 600);
showMembersFrame.add(scrollPane, BorderLayout.CENTER);
showMembersFrame.add(p, BorderLayout.SOUTH);
// showMembersFrame.pack();
showMembersFrame.setVisible(true);
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
class MyTableModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Member> members = new ArrayList<Member>();
public MyTableModel(List<Member> members) {
this.members = members;
}
public void addMember(Member m) {
members.add(m);
int row = members.size() - 1;
fireTableRowsInserted(row, row);
}
public void removeMemberFromTable(int row) {
members.remove(row);
fireTableRowsDeleted(row, row);
}
public List<Member> getTeachers() {
return new ArrayList<Member>(members);
}
@Override
public int getColumnCount() {
return 8;
}
@Override
public int getRowCount() {
return members.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Member m = members.get(rowIndex);
switch (columnIndex) {
case 0:
return m.getMemberId();
case 1:
return m.getfName();
case 2:
return m.getlName();
case 3:
return m.getAddress();
case 4:
return m.getZipCode();
case 5:
return m.getCity();
case 6:
return m.getEmail();
case 7:
return m.getPhoneNo();
default:
return "";
}
}
public String getColumnName(int column) {
switch (column) {
case 0:
return "MedlemsID";
case 1:
return "Fornavn";
case 2:
return "Efternavn";
case 3:
return "Adresse";
case 4:
return "Postnummer";
case 5:
return "By";
case 6:
return "Email";
case 7:
return "Telefonnr";
default:
return "";
}
}
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 1:
return Integer.class;
case 2:
return String.class;
case 3:
return String.class;
default:
return String.class;
}
}
}
EditMemberGUI.java
package gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import mappers.MemberMapper;
import domains.Member;
public class EditMemberGUI {
private JFrame editMemberFrame;
private JPanel btnsPanel, panel;
private JButton editBtn, cancelBtn;
private JLabel fNameLbl, lNameLbl, addressLbl, zipCodeLbl, cityLbl, emailLbl,
phoneNoLbl;
private JTextField fNameTF, lNameTF, addressTF, zipCodeTF, cityTF, emailTF,
phoneNoTF; ;
private GridLayout layout;
public EditMemberGUI(){
layout = new GridLayout(0,2,1,0);
panel = new JPanel();
panel.setLayout(layout);
layout.setVgap(0);
fNameLbl = new JLabel("Fornavn: ");
fNameTF = new JTextField(15);
lNameLbl = new JLabel("Efternavn: ");
lNameTF = new JTextField(15);
addressLbl = new JLabel("Adresse: ");
addressTF = new JTextField(15);
zipCodeLbl = new JLabel("Postnummer: ");
zipCodeTF = new JTextField(15);
cityLbl = new JLabel("By: ");
cityTF = new JTextField(15);
emailLbl = new JLabel("Email: ");
emailTF = new JTextField(15);
phoneNoLbl = new JLabel("Telefonnr: ");
phoneNoTF = new JTextField(15);
panel.add(fNameLbl);
panel.add(fNameTF);
panel.add(lNameLbl);
panel.add(lNameTF);
panel.add(addressLbl);
panel.add(addressTF);
panel.add(zipCodeLbl);
panel.add(zipCodeTF);
panel.add(cityLbl);
panel.add(cityTF);
panel.add(emailLbl);
panel.add(emailTF);
panel.add(phoneNoLbl);
panel.add(phoneNoTF);
//------------
btnsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
editBtn = new JButton("Redigér");
editBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
ShowMembersGUI s = new ShowMembersGUI();
int zipCode = Integer.parseInt(zipCodeTF.getText());
Member m = new Member(fNameTF.getText(), lNameTF.getText(),
addressTF.getText(), zipCode, cityTF.getText(),
emailTF.getText(), phoneNoTF.getText());
MemberMapper mMapper = new MemberMapper();
int i = s.getIndex();
mMapper.editMember(m, i);
int dialog = JOptionPane.showConfirmDialog(null, "Vil du gerne se oversigten over medlemmer?",
"WARNING", JOptionPane.YES_NO_OPTION);
if(dialog == JOptionPane.YES_OPTION) {
ShowMembersGUI showMembersGUI = new ShowMembersGUI();
showMembersGUI.showGUI();
editMemberFrame.setVisible(false);
} else {
editMemberFrame.setVisible(false);
}
}
});
cancelBtn = new JButton("Cancel");
cancelBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
editMemberFrame.setVisible(false);
}
});
btnsPanel.add(editBtn);
btnsPanel.add(cancelBtn);
}
public void showGUI() {
editMemberFrame = new JFrame();
editMemberFrame.setTitle("Redigér Medlem");
editMemberFrame.setLayout(new BorderLayout());
editMemberFrame.setSize(400,300);
editMemberFrame.add(panel, BorderLayout.CENTER);
editMemberFrame.add(btnsPanel, BorderLayout.SOUTH);
editMemberFrame.pack();
editMemberFrame.setVisible(true);
}
}
答案 0 :(得分:1)
您需要对您的实例的引用,例如:
1)将ShowMembersGUI
的引用传递给您的EditMemberGUI
:
editMemberBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
// int column = table.getSelectedColumn();
id = (Object) table.getValueAt(row, 0);
index = (Integer) id;
JOptionPane.showMessageDialog(null, index);
EditMemberGUI editMemberGUI = new EditMemberGUI(ShowMembersGUI.this);
editMemberGUI.showGUI();
}
});
或仅传递index
,只需将EditMemberGUI editMemberGUI = new EditMemberGUI(ShowMembersGUI.this);
更改为EditMemberGUI editMemberGUI = new EditMemberGUI(index);
2)将EditMemberGUI
的结构更改为public EditMemberGUI(Integer index)
或public EditMemberGUI(ShowMembersGUI gui)
3)将参数设置为EditMemberGUI
的实例变量(showMembersGUI / index)。
4)然后在您的actionListener中使用它,例如int i = EditMemberGUI.this.showMembersGUI.getIndex();
或int i = index;
我希望它可以帮到你。