所以我正在为Java中的电影租赁系统开发GUI,这里有一个显示用户应该看到的内容的方法。
public void displayUser() //displays user screen/functionality
{
getContentPane().removeAll();
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();
JTextField field1 = new JTextField("");
JTextField field2 = new JTextField("");
JTextField field3 = new JTextField("");
JTextField field4 = new JTextField("");
JTextField field5 = new JTextField("");
JTextField field6 = new JTextField("");
field1.setEditable(false);
field2.setEditable(false);
field3.setEditable(false);
field4.setEditable(false);
field5.setEditable(false);
field6.setEditable(false);
JPanel infoPanel = new JPanel(new GridLayout(0, 1));
infoPanel.add(new JLabel("Last Name:"));
infoPanel.add(field1);
infoPanel.add(new JLabel("First Name:"));
infoPanel.add(field2);
infoPanel.add(new JLabel("Social Security #:"));
infoPanel.add(field3);
infoPanel.add(new JLabel("Email:"));
infoPanel.add(field4);
infoPanel.add(new JLabel("Phone Number:"));
infoPanel.add(field5);
infoPanel.add(new JLabel("Address"));
infoPanel.add(field6);
add(infoPanel, BorderLayout.CENTER);
TableModel memberModel = new MemberTableModel();
TableModel itemModel = new ItemTableModel();
memberTable = new JTable(memberModel);
releaseTable = new JTable(itemModel);
c.add(memberTable);
b.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.WEST);
add(b, BorderLayout.EAST);
add(searchMember, BorderLayout.WEST);
add(searchRelease, BorderLayout.EAST);
add(menuBar, BorderLayout.NORTH);
add(infoPanel, BorderLayout.CENTER);
setVisible(true);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
repaint();
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());
}
我正在实现我自己的TableModel类(未显示),从AbstractTableModel扩展 问题是JTable(memberTable和itemTable)没有显示出来。我使用TableModel初始化Tables,声明两个JScrollPanes,每个窗格添加一个表,并将窗格添加到框架。我不明白为什么他们不可见。有人可以提供一些帮助吗?
注意:表格是全局声明的,超出了这一方法的范围。
答案 0 :(得分:2)
根据JScrollPane API,您不希望直接向JScrollPanes添加组件,因为它会挤压其视口,而是您希望将组件添加到JScrollPane的视口作为视口&# 39;观点。
更改:
c.add(memberTable);
b.add(releaseTable);
到
c.setViewportView(memberTable);
b.setViewportView(releaseTable);
答案 1 :(得分:2)
看看以下几行:
add(c, BorderLayout.WEST);
add(b, BorderLayout.EAST);
add(searchMember, BorderLayout.WEST);
add(searchRelease, BorderLayout.EAST);
您要向WEST和EAST添加两个组件,这将无法正常工作。每个BorderLayout部分只能有一个组件。请注意,您可以通过向JPanel添加组件,然后将该JPanel添加到BorderLayout来嵌套布局。