gpt4 book ai didi

Java JTable 不可见

转载 作者:行者123 更新时间:2023-11-29 03:32:32 25 4
gpt4 key购买 nike

我正在创建一个地址簿,并且已经具备了很多功能。但是看不到实际的表。数据就在那里。我可以保存并加载它,但我实际上看不到任何数据。我已经尝试了很多东西,setContentPane() 就是其中之一。我也花时间在 Oracle 网站上,但无济于事。我有显示该表的示例表,并尝试复制一些代码,但同样无济于事。

public AddressBookGUI()
{
/*
* Initialise
*/

addresses = new ArrayList<Person>();
Person a = new Person("Kathy", "Smith", "1 mith Sqaoiudh", "Lonodn", "London", "BN76 8QQ", "01980472", "LKSJ@kied.con");
addresses.add(a);
addressBook = new AddressBook(addresses, columnNames);

// JTable addressTable = new JTable(new TableModel());
//addressTable.setModel(new addressTable.TableModel());

createAddAddressFrame();
createEditAddressFrame();
createDeleteAddressFrame();
createAddressBookFrame();
}

//Create the address book frame
private void createAddressBookFrame() {

//Create frame
addressBookFrame = new JFrame("My Address Book");

//Create menu bar
mb = new JMenuBar();

//Create file menu
fileMenu = new JMenu("File");
anew = new JMenuItem("New");
open = new JMenuItem("Open");
save = new JMenuItem("Save");
saveAs = new JMenuItem("Save As");
print = new JMenuItem("Print");
quit = new JMenuItem("Quit");

//Create edit menu
editMenu = new JMenu("Edit");
sort = new JMenu("Sort");
byName = new JMenuItem("By Name");
byPC = new JMenuItem("By Post Code");
editSelectedPerson = new JMenuItem("Edit Selected Person");
find = new JMenuItem("Find");
findAgain = new JMenuItem("Find Again");
//Add menu items to menu bar and menu
mb.add(fileMenu);
fileMenu.add(anew);
fileMenu.add(open);
fileMenu.add(save);
fileMenu.add(saveAs);
fileMenu.addSeparator();
fileMenu.add(print);
fileMenu.add(quit);
mb.add(editMenu);
editMenu.add(editSelectedPerson);
editMenu.add(sort);
sort.add(byName);
sort.add(byPC);
editMenu.addSeparator();
editMenu.add(find);
editMenu.add(findAgain);

//Set menu bar
addressBookFrame.setJMenuBar(mb);

//Add buttons to bottom of frame
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

//Add buttons
addButton = new JButton("Add");
editButton = new JButton("Edit...");
deleteButton = new JButton("Delete");
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);

//Add button panel
addressBookFrame.add(buttonPanel, BorderLayout.SOUTH);

//Add action Listeners
//BUTTONS
//Add
class addListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//Set text of JTextFields
addAddressFrame.setVisible(true);
}
}
ActionListener addList = new addListener();
addButton.addActionListener(addList);

//Edit
class editListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
row = myTable.getSelectedRow();
if (row != -1) {
//jtf1.setText((String)addressBook.getValueAt(row, 0));
//repeat
}
}
}
ActionListener editList = new editListener();
editButton.addActionListener(editList);

//Delete
class delListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
row = myTable.getSelectedRow();
if (row != -1) {
deleteAddressFrame.setVisible(true);
}
}
}
ActionListener delList = new delListener();
deleteButton.addActionListener(delList);


//MENU ITEMS
//New
class newListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
addresses.clear();
refreshAddressBook();
}
}
ActionListener newList = new newListener();
anew.addActionListener(newList);

//open
class openListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
addresses.clear();
loadFile();
}
}
ActionListener openList = new openListener();
open.addActionListener(openList);

//save
class saveListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{

}
}
ActionListener saveList = new saveListener();
save.addActionListener(saveList);

//save as
class saveAsListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
saveToFile();
}
}
ActionListener saveAsList = new saveAsListener();
saveAs.addActionListener(saveAsList);

//print
class printListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
printAddresses();
}
}
ActionListener printList = new printListener();
print.addActionListener(printList);

//quit
class quitListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
ActionListener quitList = new quitListener();
quit.addActionListener(quitList);

//edit selected person
class espListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{

}
}
ActionListener espList = new espListener();
editSelectedPerson.addActionListener(espList);

//sort by name
class sbnListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
sortByName();
}
}
ActionListener sbnList = new sbnListener();
byName.addActionListener(sbnList);

//sort by post code
class sbpcListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
}
}
ActionListener sbpcList = new sbpcListener();
byPC.addActionListener(sbpcList);

//find
class findListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
}
}
ActionListener findList = new findListener();
find.addActionListener(findList);

//find again
class findAgainListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
}
}
ActionListener findAgainList = new findAgainListener();
findAgain.addActionListener(findAgainList);

myTable = new JTable();
myTable.setPreferredScrollableViewportSize(new Dimension(500,800));
myTable.setFillsViewportHeight(true);
//myTable.setModel(addressBook);

JScrollPane scrollPane = new JScrollPane(myTable);
scrollPane.setOpaque(true);
addressBookFrame.add(scrollPane, BorderLayout.CENTER);
//addressBookFrame.add(myTable, BorderLayout.CENTER);

//FInally show the address book
addressBookFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addressBookFrame.setTitle("My Address Book");
addressBookFrame.setSize(800, 400);
addressBookFrame.setVisible(true);

}

最佳答案

在这条语句中添加注释,以便为 JTable myTable 设置 TableModel

//myTable.setModel(addressBook);

确保 AddressBook 确实是一个正常运行的自定义 TableModel

关于Java JTable 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17390257/

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