gpt4 book ai didi

java - 扩展 AbstractTableModel 中的自定义 setValueAt 方法

转载 作者:行者123 更新时间:2023-11-30 07:01:28 24 4
gpt4 key购买 nike

所以我正在使用 Java 开发 DVD 租赁系统,但遇到了 View 问题。这是我的代码:

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.*;
public class RentalSystem extends JFrame {

JMenuBar menuBar;
JMenuItem insertMember;
JMenuItem deleteMember;
JMenuItem editMember;
JMenuItem retrieveMember;
JMenuItem insertRelease;
JMenuItem deleteRelease;
JMenuItem editRelease;
JMenuItem retrieveRelease;
JMenu menu;
JMenu menu2;
JComboBox searchMember;
JComboBox searchRelease;
JButton adminButton;
JButton userButton;
JButton existingUser;
JButton newUser;
JTable memberTable;
JTable releaseTable;
int existingUserCheck = 1; //0 = existing user, 1 = new user
int adminCheck = 1; //0 = admin, 1 = customer


public RentalSystem()
{
super("Tape/DVD/Game Rental System");
setBounds(0,0,1000,500);
//while(adminCheck == 0)
{
display2();
while(existingUserCheck == 0) {

}
}

}
private class MenuActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if(e.getSource() == "insertMember");
{
String name = JOptionPane.showInputDialog("Enter Last Name:");
String name2 = JOptionPane.showInputDialog("Enter First Name:");
String ss = JOptionPane.showInputDialog("Enter Social Security Number:");
int a = Integer.parseInt(ss);
String email = JOptionPane.showInputDialog("Enter Email Address:");
String phoneNumber = JOptionPane.showInputDialog("Enter Phone Number:");
int b = Integer.parseInt(phoneNumber);
String address = JOptionPane.showInputDialog("Enter Address:");

Member member = new Member(name, name2, a, email, b, address);
//add member to table

}
if(e.getSource() == "deleteMember");
{

}
if(e.getSource() == "editMember");
{

}
if(e.getSource() == "retrieveMember");
{

}
if(e.getSource() == "insertRelease");
{
String title = JOptionPane.showInputDialog(this ,"Enter Title:");
String type = JOptionPane.showInputDialog(this ,"Enter Type(Tape/DVD/Game):");
String releaseYear = JOptionPane.showInputDialog(this ,"Enter Year of Release:");
int a = Integer.parseInt(releaseYear);
String price = JOptionPane.showInputDialog(this ,"Enter Price:");
int b = Integer.parseInt(price);


Item item = new Item(title, type, a, b);
//add item to table

}
if(e.getSource() == "deleteRelease");
{

}
if(e.getSource() == "editRelease");
{

}
if(e.getSource() == "retrieveRelease");
{

}
}
}

private class ComboBoxActionListener implements ActionListener {


public void actionPerformed(ActionEvent e) {

String s = (String) searchMember.getSelectedItem();
String f = (String) searchRelease.getSelectedItem();


if(s.equals("Last Name")){

}
else if(s.equals("First Name")){
}
else if(s.equals("Phone Number")){
}
else if(s.equals("Member ID")){
}

if(f.equals("Title")){

}
else if(f.equals("Category")){
}
else if(f.equals("Type")){
}
else if(f.equals("Rental ID")){

}


if(e.getSource() == "Existing User")
{

}


}

}

private class ButtonListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {

if(e.getSource() == adminButton)
{
menuBar = new JMenuBar();
menu = new JMenu("Members");
menuBar.add(menu);
menu2 = new JMenu("Releases");
menuBar.add(menu2);

insertMember = new JMenuItem("Insert New Member");
deleteMember = new JMenuItem("Delete Member");
editMember = new JMenuItem("Edit Member");
retrieveMember = new JMenuItem("Retrieve Member");
menu.add(insertMember);
menu.add(deleteMember);
menu.add(editMember);
menu.add(retrieveMember);
insertRelease = new JMenuItem("Insert New Release");
deleteRelease = new JMenuItem("Delete Release");
editRelease = new JMenuItem("Edit Release");
retrieveRelease = new JMenuItem("Retrieve Release");
menu2.add(insertRelease);
menu2.add(deleteRelease);
menu2.add(editRelease);
menu2.add(retrieveRelease);

JScrollPane c = new JScrollPane();
JScrollPane b = new JScrollPane();


TableModel memberModel = new MemberTableModel();
TableModel itemModel = new ItemTableModel();
memberTable = new JTable(memberModel);
releaseTable = new JTable(itemModel);

c.add(memberTable);
c.add(releaseTable);

String[] searchMemberChoices = {"Search Members By:", "Last Name", "First Name","Phone Number", "Member ID"};
String[] searchReleaseChoices = {"Search Releases By:", "Title" , "Category", "Type", "Release ID" };
searchMember = new JComboBox(searchMemberChoices);
searchRelease = new JComboBox(searchReleaseChoices);


//add(c, BorderLayout.CENTER);
//add(b, BorderLayout.SOUTH);
add(searchMember, BorderLayout.WEST);
add(searchRelease, BorderLayout.EAST);
add(menuBar, BorderLayout.NORTH);

setVisible(true);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

insertMember.addActionListener(new MenuActionListener());
deleteMember.addActionListener(new MenuActionListener());
editMember.addActionListener(new MenuActionListener());
retrieveMember.addActionListener(new MenuActionListener());
insertRelease.addActionListener(new MenuActionListener());
deleteRelease.addActionListener(new MenuActionListener());
editRelease.addActionListener(new MenuActionListener());
retrieveRelease.addActionListener(new MenuActionListener());
searchMember.addActionListener(new ComboBoxActionListener());
searchRelease.addActionListener(new ComboBoxActionListener());
}
if(e.getSource() == userButton)
{
adminCheck = 1;

menuBar = new JMenuBar();
menu = new JMenu("Members");
menuBar.add(menu);
menu2 = new JMenu("Releases");
menuBar.add(menu2);


retrieveMember = new JMenuItem("Retrieve Member");
menu.add(retrieveMember);
retrieveRelease = new JMenuItem("Retrieve Release");
menu2.add(retrieveRelease);

JScrollPane c = new JScrollPane();
JScrollPane b = new JScrollPane();


TableModel memberModel = new MemberTableModel();
TableModel itemModel = new ItemTableModel();
memberTable = new JTable(memberModel);
releaseTable = new JTable(itemModel);



c.add(memberTable);
c.add(releaseTable);


String[] searchMemberChoices = {"Search Members By:", "Last Name", "First Name","Phone Number", "Member ID"};
String[] searchReleaseChoices = {"Search Releases By:", "Title" , "Category", "Type", "Release ID" };
searchMember = new JComboBox(searchMemberChoices);
searchRelease = new JComboBox(searchReleaseChoices);


//add(c, BorderLayout.CENTER);
//add(b, BorderLayout.SOUTH);
add(searchMember, BorderLayout.WEST);
add(searchRelease, BorderLayout.EAST);
add(menuBar, BorderLayout.NORTH);


setVisible(true);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);


insertMember.addActionListener(new MenuActionListener());
deleteMember.addActionListener(new MenuActionListener());
editMember.addActionListener(new MenuActionListener());
retrieveMember.addActionListener(new MenuActionListener());
insertRelease.addActionListener(new MenuActionListener());
deleteRelease.addActionListener(new MenuActionListener());
editRelease.addActionListener(new MenuActionListener());
retrieveRelease.addActionListener(new MenuActionListener());
searchMember.addActionListener(new ComboBoxActionListener());
searchRelease.addActionListener(new ComboBoxActionListener());

}



if(e.getSource() == existingUser);
{

}
if(e.getSource() == newUser)
{
existingUserCheck = 1;
JTextField field1 = new JTextField("");
JTextField field2 = new JTextField("");
JTextField field3 = new JTextField("");
JTextField field4 = new JTextField("");
JTextField field5 = new JTextField("");
JTextField field6 = new JTextField("");

JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("Last Name:"));
panel.add(field1);
panel.add(new JLabel("First Name:"));
panel.add(field2);
panel.add(new JLabel("Social Security #:"));
panel.add(field3);
panel.add(new JLabel("Email:"));
panel.add(field4);
panel.add(new JLabel("Phone Number:"));
panel.add(field5);
panel.add(new JLabel("Address"));
panel.add(field6);
add(panel);
int result = JOptionPane.showConfirmDialog(null, panel, "Registration",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {

int ss = Integer.parseInt(field3.getText());
int phoneNumber = Integer.parseInt(field5.getText());

//memberTable.addMember(field1.getText(),field2.getText(), ss, field4.getText(), phoneNumber, field6.getText());

}
}
}
}
class MemberTableModel extends AbstractTableModel {

private String[] headers = {"Last Name", "First Name", "SS#", "Email", "Phone Number", "Address"};


@Override
public int getColumnCount() {

return headers.length;
}

@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 0;
}

@Override
public Object getValueAt(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
public void setValueAt(Member a, int rowIndex, int colIndex)
{

}

}
class ItemTableModel extends AbstractTableModel {

@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 0;
}

@Override
public Object getValueAt(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
public void setValueAt()
{

}

}
public void display()//displays first screen:admin/user log-in
{
userButton = new JButton("User");
adminButton = new JButton("Admin");

JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(userButton);
panel.add(adminButton);
add(panel);

userButton.addActionListener(new ButtonListener());
adminButton.addActionListener(new ButtonListener());


setVisible(true);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void display2()//displays second screen:user log-in / register
{
existingUser = new JButton("Existing User");
newUser = new JButton("New User");

JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(newUser);
panel.add(existingUser);
add(panel);

existingUser.addActionListener(new ButtonListener());
newUser.addActionListener(new ButtonListener());


setVisible(true);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}
public void display3() //displays user screen/functionality
{

}
public static void main(String[] args) {

JFrame frame = new RentalSystem();

}
}

现在,Member 和 Item 只是我用来表示用户(成员)和电影/游戏(项目)的对象类。

public class Member {
int status;
String firstName;
String lastName;
int ss;
String email;
int phoneNumber;
String address;
int memberID;

public Member()
{

}
public Member(String firstName, String lastName, int ss, String email, int phoneNumber, String address)
{
this.firstName = firstName;
this.lastName = lastName;
this.ss = ss;
this.email = email;
this.phoneNumber = phoneNumber;
this.address = address;
}

public void setName(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getName()
{
return firstName + " " + lastName;
}
public void setSS(int ss)
{
this.ss = ss;
}
public int getSS()
{
return ss;
}
public void setEmail(String email)
{
this.email = email;
}
public String getEmail()
{
return email;
}
public void setPhoneNumber(int phoneNumber)
{
this.phoneNumber = phoneNumber;
}
public int getPhoneNumber()
{
return phoneNumber;
}
public void setAddress(String address)
{
this.address = address;
}
public String getAddress()
{
return address;
}
public void setMemberID(int memberID)
{
this.memberID = memberID;
}
public int getMemberID()
{
return memberID;
}
public void setStatus(int status)
{
this.status = status;
}
public int getStatus()
{
return status;
}
}

public class Item {

String title;
String type;
int releaseYear;
int rentalID;
int price;
boolean inStock;

public Item()
{

}
public Item(String title, String type, int releaseYear, int price)
{
this.title = title;
this.type = type;
this.releaseYear = releaseYear;
this.price = price;

}
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return title;
}
public void setReleaseYear(int releaseYear)
{
this.releaseYear = releaseYear;
}
public int getReleaseYear()
{
return releaseYear;
}
public void setRentalID(int rentalID)
{
this.rentalID = rentalID;
}
public int getRentalID()
{
return rentalID;
}
public void setPrice(int price)
{
this.price = price;
}
public int getPrice()
{
return price;
}

}

我的问题是,我对如何创建自定义的、覆盖的“SetValueAt()”方法以使用来自成员/项目的信息填充 JTables 感到困惑。另一个问题是 Listener 类中的所有空 if 语句。我不确定如何着手实现。如果有人可以就这些问题中的任何一个或我可能遗漏的任何其他问题提供一些指导,我将不胜感激任何反馈。

最佳答案

实际上,您需要实现的不仅仅是 setValueAt() 方法。您需要决定如何存储所有数据。您需要实现 getValueAt(...) 方法,以便数据可以显示在表格中。

查看 Creating a Table Model 上 Swing 教程中的 TableDemo.java 代码有关创建自定义 TableModel 的基础知识。它将向您展示一种实现各种方法的方法。

但是,该 TableModel 并不是一个很好的通用模型,因为数据基本上是硬编码的,您无法添加新的数据行。

要获得更好但更复杂的解决方案,请查看 Row Table Model .这是一个更通用的 TableModel,具有更大的灵 active 。但是,您仍然需要为要添加到表中的自定义对象实现几个方法。查看博客中的 JButtonTableModel.java 代码,了解所需更改的示例。

关于java - 扩展 AbstractTableModel 中的自定义 setValueAt 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29809509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com