第132行JPanel bookInventory = new Inventory();附加代码报告错误"类型不匹配:无法从Inventory转换为JPanel" - 有什么建议可以解决这个问题吗?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
public class LibSys extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
//private JPanel panelContent;
private static LibSys frame;
private JPanel panelTools;
private JScrollPane scrollPane;
public static int level = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new LibSys(0);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LibSys(int n) {
level = n;
setTitle("Library Management System");
setIconImage(Toolkit.getDefaultToolkit().getImage(LibSys.class.getResource("/resources/Books-2-icon64.png")));
setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 744);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
JSeparator sep = new JSeparator();
JMenuItem logout = new JMenuItem("Logout");
logout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Login login = new Login();
login.setVisible(true);
dispose();
}
});
menu.add(exit);
menu.add(sep);
menu.add(logout);
menuBar.add(menu);
contentPane = new JPanel();
contentPane.setBackground(SystemColor.text);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
panelTools = new JPanel();
contentPane.add(panelTools, BorderLayout.WEST);
JButton btnToolBookLending = new JButton("Book Lending");
btnToolBookLending.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel bookLend = new BookLending();
//scrollPane.repaint();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(bookLend);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
panelTools.setLayout(new GridLayout(8, 1, 0, 20));
btnToolBookLending.setHorizontalAlignment(SwingConstants.LEFT);
btnToolBookLending.setIcon(new ImageIcon(LibSys.class.getResource("/resources/book-48.png")));
panelTools.add(btnToolBookLending);
//panelContent = new JPanel();
//contentPane.add(panelContent, BorderLayout.CENTER);
//panelContent.setLayout(new FlowLayout(BorderLayout.CENTER, 5, 5));
JButton btnToolInventory = new JButton("Inventory");
btnToolInventory.setHorizontalAlignment(SwingConstants.LEFT);
btnToolInventory.setIcon(new ImageIcon(LibSys.class.getResource("/resources/Inventory-icon-32.png")));
btnToolInventory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel bookInventory = new Inventory();
scrollPane.getViewport().removeAll();
//System.out.println(bookInventory);
scrollPane.setViewportView(bookInventory);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
panelTools.add(btnToolInventory);
//panelTools.add(btnNewButton_1, "2, 2, left, center");
JButton btnBooksReg = new JButton("Book Registration");
btnBooksReg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel bookReg = new BookRegistration();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(bookReg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnBooksReg.setHorizontalAlignment(SwingConstants.LEFT);
btnBooksReg.setIcon(new ImageIcon(LibSys.class.getResource("/resources/book-add-icon-32.png")));
panelTools.add(btnBooksReg);
JButton btnToolMemberProfile = new JButton("Member Profile");
btnToolMemberProfile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel bookMemberProfile = new MemberProfile();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(bookMemberProfile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnToolMemberProfile.setIcon(new ImageIcon(LibSys.class.getResource("/resources/App-login-manager-icon32.png")));
btnToolMemberProfile.setHorizontalAlignment(SwingConstants.LEFT);
panelTools.add(btnToolMemberProfile);
JButton btnToolMemberReg = new JButton("Member Registration");
btnToolMemberReg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel MemberReg = new Registration();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(MemberReg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnToolMemberReg.setIcon(new ImageIcon(LibSys.class.getResource("/resources/user_male_2_add.png")));
btnToolMemberReg.setHorizontalAlignment(SwingConstants.LEFT);
panelTools.add(btnToolMemberReg);
JButton btnToolConfig = new JButton("Configuration");
btnToolConfig.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel adminPanel = new AdminPanel();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(adminPanel);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnToolConfig.setIcon(new ImageIcon(LibSys.class.getResource("/resources/settings.png")));
btnToolConfig.setHorizontalAlignment(SwingConstants.LEFT);
panelTools.add(btnToolConfig);
JButton btnLogout = new JButton("Logout");
btnLogout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Login login = new Login();
login.setVisible(true);
dispose();
}
});
btnLogout.setIcon(new ImageIcon(LibSys.class.getResource("/resources/12345678.png")));
btnLogout.setHorizontalAlignment(SwingConstants.LEFT);
panelTools.add(btnLogout);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBackground(SystemColor.window);
lblNewLabel.setIcon(new ImageIcon(LibSys.class.getResource("/resources/top-banner.png")));
lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
contentPane.add(lblNewLabel, BorderLayout.NORTH);
scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
scrollPane.setColumnHeaderView(panel);
panel.setLayout(new BorderLayout(0, 0));
JButton btnClose = new JButton("");
btnClose.setPreferredSize(new Dimension(33, 33));
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(scrollPane.getViewport().getComponentCount()>0){
scrollPane.getViewport().remove(scrollPane.getViewport().getComponent(0));
scrollPane.getViewport().repaint();
}
}
});
btnClose.setIcon(new ImageIcon(LibSys.class.getResource("/resources/2015.png")));
panel.add(btnClose, BorderLayout.EAST);
}
}
答案 0 :(得分:0)
确保Inventory extends JPanel
这使Inventory
表现为JPanel的子类,因此您可以以相同的方式使用它。