所以我正在使用Java制作DVD租赁系统并遇到了查看问题。 这是我的代码:
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只是我用来表示用户(成员)和电影/游戏(Items)的对象类。
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()'而感到困惑。使用Members / Items中的信息填充JTable的方法。 另一个问题是Listener类中的所有空if语句。我不确定如何进行实施。如果有人可以就这些问题或其他任何我可能错过的问题提供一些指导,我将非常感谢任何反馈。
答案 0 :(得分:3)
实际上,您需要实现比setValueAt()方法更多的功能。您需要决定如何存储所有数据。您需要实现getValueAt(...)方法,以便可以在表中显示数据。
查看Creating a Table Model上Swing教程中的TableDemo.java
代码,了解创建自定义TableModel的基础知识。它将向您展示实现各种方法的一种方法。
但是,TableModel不是一个非常好的通用模型,因为数据基本上是硬编码的,并且您无法添加新的数据行。
要获得更好但更复杂的解决方案,请查看Row Table Model。这是一个更通用的TableModel,具有更大的灵活性。但是,您仍需要为要添加到表中的自定义对象实现几种方法。查看博客中的JButtonTableModel.java
代码,了解所需更改的示例。
答案 1 :(得分:1)
让我们忽略所有有趣的"代码问题并关注TableModel
表格是一个二维数据矩阵,分布在一系列行和列
上 TableModel
提供了JTable
(视图)可以与TableModel
代表的数据进行交互的合同......
public interface TableModel
{
public int getRowCount();
public int getColumnCount();
public String getColumnName(int columnIndex);
public Class<?> getColumnClass(int columnIndex);
public boolean isCellEditable(int rowIndex, int columnIndex);
public Object getValueAt(int rowIndex, int columnIndex);
public void setValueAt(Object aValue, int rowIndex, int columnIndex);
public void addTableModelListener(TableModelListener l);
public void removeTableModelListener(TableModelListener l);
}
这很重要,因为您的数据需要符合此合同。
你有一个Member
类,它有一系列已定义的属性,每个属性代表一个可能的列值,这使得每个Member
实例都是&#34;行&#34;数据。
您需要决定的第一件事是如何管理所有Member
。既然你已经声明过你需要一个动态的&#34; (或可变)模型,使用的更好的数据结构之一可能是ArrayList
,因为它可以增长以满足您的需求并具有快速非顺序访问(您可以快速从随机点获取值)。
public class MemberTableModel extends AbstractTableModel {
private String[] headers = {"Last Name", "First Name", "SS#", "Email", "Phone Number", "Address"};
private List<Member> members;
public MemberTableModel() {
members = new ArrayList<>(25);
}
public MemberTableModel(List<Member> members) {
this();
this.members.addAll(members);
}
public MemberTableModel(Member... members) {
this(Arrays.asList(members));
}
@Override
public int getRowCount() {
return members.size();
}
@Override
public int getColumnCount() {
return headers.length;
}
@Override
public String getColumnName(int column) {
return headers[column];
}
好的,现在我们需要一些我们来获取特定行的列...
protected Member getMemberAt(int row) {
return members.get(row);
}
@Override
public Object getValueAt(int row, int col) {
Object value = null;
Member member = getMemberAt(row);
switch (col) {
case 0:
// Need getter for last name
break;
case 1:
// Need getter for first name
break;
case 2:
value = member.getSS();
break;
case 3:
value = member.getEmail();
case 4:
value = member.getPhoneNumber();
case 5:
value = member.getAddress();
}
return value;
}
设置值几乎是相同的过程......
@Override
public void setValueAt(Object value, int rowIndex, int colIndex) {
Member member = getMemberAt(rowIndex);
switch (colIndex) {
case 0:
member.setName(value.toString(), member.getLastName());
break;
case 1:
member.setName(member.getFirstName(), value.toString());
break;
case 2:
if (value instanceof Integer) {
member.setSS((Integer) value);
}
break;
case 3:
member.setEmail(value.toString());
break;
case 4:
if (value instanceof Integer) {
member.setPhoneNumber((Integer)value);
}
break;
case 5:
member.setAddress(value.toString());
break;
}
fireTableRowsUpdated(rowIndex, colIndex);
}
}
注意,此方法没有验证,您应该检查这些值是否符合您对数据的基本要求......
但是,要使表格可编辑,您需要覆盖模型的isCellEdtiable
方法...
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
从模型中添加或删除成员,您需要在模型中提供功能,例如......
public void add(Member member) {
int size = members.size();
members.add(member);
fireTableRowsInserted(size, size);
}
public void remove(Member member) {
int index = members.indexOf(member);
if (index > -1) {
members.remove(member);
fireTableRowsDeleted(index, index);
}
}
详细了解How to Use Tables和Collections Trail了解详情