我有三个 Java 类:
一个 JFrame:HomeView,一个 JPanel:TopicListView,另一个 JPanel:ReplyListView。
在 HomeView 中,我有一个可以单击以显示 TopicListView 的菜单项。在 TopicListView 中,我想要一个按钮,我可以单击它来显示回复列表视图。单击按钮时,它将调用 openReplyListView() 方法。该方法将创建一个新的 JPanel 并用它替换当前的 JPanel。但是,openReplyListView() 中的代码不起作用。
注意:我没有使用 CardLayout。
任何有关如何使其正常工作的帮助将不胜感激。
提前致谢。
主页视图类:
public class HomeView extends JFrame {
private JPanel contentPane;
private JMenuBar menuBar;
private JMenu mnForum;
private JMenuItem mntmReply;
private JMenuItem mntmTopic;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HomeView frame = new HomeView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public HomeView() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1280, 720);
setJMenuBar(getMenuBar_1());
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
private JMenuBar getMenuBar_1() {
if (menuBar == null) {
menuBar = new JMenuBar();
menuBar.add(getMnForum());
}
return menuBar;
}
private JMenu getMnForum() {
if (mnForum == null) {
mnForum = new JMenu("Forum");
mnForum.add(getMntmTopic());
mnForum.add(getMntmReply());
}
return mnForum;
}
private JMenuItem getMntmReply() {
if (mntmReply == null) {
mntmReply = new JMenuItem("Reply");
mntmReply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JPanel panel = new ReplyView();
getContentPane().removeAll();
getContentPane().add(panel);
getContentPane().validate();
getContentPane().repaint();
}
});
}
return mntmReply;
}
private JMenuItem getMntmTopic() {
if (mntmTopic == null) {
mntmTopic = new JMenuItem("Topic");
mntmTopic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JPanel panel = new TopicListView();
getContentPane().removeAll();
getContentPane().add(panel);
getContentPane().validate();
getContentPane().repaint();
}
});
}
return mntmTopic;
}
}
主题列表视图类:
public class TopicListView extends JPanel {
private JTable table;
private JScrollPane scrollPane;
private JLabel lblTopics;
/**
* Create the panel.
*/
public TopicListView() {
setLayout(null);
add(getTable());
add(getScrollPane());
add(getLblTopics());
}
**Code snippet for the table and the scrollpane:**
private void openReplyListView(){
JPanel panel = new ReplyListView();
getContentPane().removeAll();
getContentPane().add(panel);
getContentPane().validate();
getContentPane().repaint();
}
ReplyListView 类(一般与 TopicListView 相同)
public class ReplyListView extends JPanel {
private JTable table;
private JScrollPane scrollPane;
private JLabel lblTest;
/**
* Create the panel.
*/
public ReplyListView() {
setLayout(null);
add(getTable());
add(getScrollPane());
add(getLblTest());
}
private JTable getTable() {
if (table == null) {
table = new JTable();
table.setBounds(414, 114, 464, 354);
}
return table;
}
private JScrollPane getScrollPane() {
if (scrollPane == null) {
scrollPane = new JScrollPane(getTable());
scrollPane.setBounds(414, 114, 464, 349);
}
return scrollPane;
}
private JLabel getLblTest() {
if (lblTest == null) {
lblTest = new JLabel("TEST");
lblTest.setFont(new Font("Tahoma", Font.PLAIN, 30));
lblTest.setBounds(593, 35, 81, 68);
}
return lblTest;
}
}