当另一个类调用Singleton.instance.show()时,具有CardLayout的Singleton不会显示卡片。

【字号: 日期:2024-02-24浏览:43作者:雯心
如何解决当另一个类调用Singleton.instance.show()时,具有CardLayout的Singleton不会显示卡片。?

它将建议一个参考问题。public static MainWindow instance = newMainWindow();看起来很可疑,因为您必须先创建一个MainWindowfirst实例才能进行初始化,这建议您现在有两个实例MainWindow,一个在屏幕上,另一个不在屏幕上。

static以这种方式使用不是一个好主意,因为它会导致类似这样的问题。相反,您应该将控制器的引用传递给页面。控制器将定义每个页面可以执行的操作(如果操作正确,则将其定义为interface)

另外,您可以将导航与页面分开,形成单独的机制,这意味着页面不在乎,可以简单地以您想要的任何顺序显示或在其他地方重复使用

本示例定义了一个简单的控制器,页面可以调用该控制器来实现页面导航

import java.awt.BorderLayout;import java.awt.cardlayout;import java.awt.EventQueue;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.List;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;public class cardlayoutExample { public static void main(String[] args) {new cardlayoutExample(); } public cardlayoutExample() {EventQueue.invokelater(new Runnable() { @Override public void run() {try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException | InstantiationException | illegalaccessexception | UnsupportedLookAndFeelException ex) {}JFrame frame = new JFrame('Testing');frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);frame.setLayout(new BorderLayout());frame.add(new Wizard());frame.pack();frame.setLocationRelativeto(null);frame.setVisible(true); }}); } public interface NavigationController {public void nextPage();public void prevIoUsPage();public void lastPage();public void firstPage(); } public interface Page {public NavigationController getNavigationController();public JComponent getView();public String getName(); } public class Wizard extends JPanel implements NavigationController {private List<Page> pages;private Page currentPage;private cardlayout cardlayout;public Wizard() { cardlayout = new cardlayout(); pages = new ArrayList<>(25); setLayout(cardlayout); pages.add(new FirstPage('Page01', this)); pages.add(new SecondPage('Page02', this)); pages.add(new ThirdPage('Page03', this)); for (Page page : pages) {add(page.getView(), page.getName()); } firstPage();}@Overridepublic void nextPage() { int index = pages.indexOf(currentPage); index++; if (index < pages.size()) {cardlayout.next(this);currentPage = pages.get(index); }}@Overridepublic void prevIoUsPage() { int index = pages.indexOf(currentPage); index--; if (index >= 0) {cardlayout.prevIoUs(this);currentPage = pages.get(index); }}@Overridepublic void lastPage() { Page page = pages.get(pages.size() - 1); showPage(page);}@Overridepublic void firstPage() { Page page = pages.get(0); showPage(page);}protected void showPage(Page page) { cardlayout.show(this, page.getName()); currentPage = page;} } public abstract class AbstractPage extends JPanel implements Page, ActionListener {private NavigationController navigationController;private JPanel buttons;private String name;public AbstractPage(String name, NavigationController navigationController) { this.name = name; this.navigationController = navigationController; setLayout(new BorderLayout()); buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT)); add(buttons, BorderLayout.soUTH);}protected void insertButton(JButton button) { button.addActionListener(this); buttons.add(button);}@Overridepublic NavigationController getNavigationController() { return navigationController;}@Overridepublic JComponent getView() { return this;}@Overridepublic String getName() { return super.getName(); } } public class FirstPage extends AbstractPage implements Page {private JButton next = new JButton('Next >');public FirstPage(String name, NavigationController controller) { super(name, controller); JLabel label = new JLabel('First page'); label.setHorizontalAlignment(JLabel.CENTER); add(label); insertButton(next);}@Overridepublic void actionPerformed(ActionEvent e) { if (e.getSource() == next) {getNavigationController().nextPage(); }} } public class SecondPage extends AbstractPage implements Page {private JButton next = new JButton('Next >');private JButton prevIoUs = new JButton('< PrevIoUs');public SecondPage(String name, NavigationController controller) { super(name, controller); JLabel label = new JLabel('Second page'); label.setHorizontalAlignment(JLabel.CENTER); add(label); insertButton(prevIoUs); insertButton(next);}@Overridepublic void actionPerformed(ActionEvent e) { if (e.getSource() == next) {getNavigationController().nextPage(); } else if (e.getSource() == prevIoUs) {getNavigationController().prevIoUsPage(); }} } public class ThirdPage extends AbstractPage implements Page {private JButton prevIoUs = new JButton('< PrevIoUs');public ThirdPage(String name, NavigationController controller) { super(name, controller); JLabel label = new JLabel('Third page'); label.setHorizontalAlignment(JLabel.CENTER); add(label); insertButton(prevIoUs);}@Overridepublic void actionPerformed(ActionEvent e) { if (e.getSource() == prevIoUs) {getNavigationController().prevIoUsPage(); }} }}

此示例将控制器与页面分开,以便按钮不属于页面本身。这样可以释放页面/视图,使其成为您需要的任何内容

import java.awt.BorderLayout;import java.awt.cardlayout;import java.awt.Component;import java.awt.EventQueue;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.border.EmptyBorder;public class cardlayoutExample2 { public static void main(String[] args) {new cardlayoutExample2(); } public cardlayoutExample2() {EventQueue.invokelater(new Runnable() { @Override public void run() {try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException | InstantiationException | illegalaccessexception | UnsupportedLookAndFeelException ex) {}JFrame frame = new JFrame('Testing');frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);frame.setLayout(new BorderLayout());frame.add(new WizardPane());frame.pack();frame.setLocationRelativeto(null);frame.setVisible(true); }}); } public class WizardPane extends JPanel {private List<String> pages;private String currentPage;private JButton first;private JButton prevIoUs;private JButton next;private JButton last;private cardlayout cardlayout;private JPanel contentPane;public WizardPane() { setLayout(new BorderLayout()); cardlayout = new cardlayout(); pages = new ArrayList<>(3); contentPane = new JPanel(cardlayout); contentPane.setBorder(new EmptyBorder(4, 4, 4, 4)); pages.add('Page01'); pages.add('Page02'); pages.add('Page03'); contentPane.add(new FirstPage(), 'Page01'); contentPane.add(new SecondPage(), 'Page02'); contentPane.add(new ThirdPage(), 'Page03'); JPanel actionsPane = new JPanel(new GridBagLayout()); actionsPane.setBorder(new EmptyBorder(4, 4, 4, 4)); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; actionsPane.add((first = new JButton('<< First')), gbc); gbc.gridx++; gbc.weightx = 1; gbc.anchor = GridBagConstraints.WEST; actionsPane.add((prevIoUs = new JButton('< PrevIoUs')), gbc); gbc.gridx++; gbc.anchor = GridBagConstraints.EAST; actionsPane.add((next = new JButton('Next >')), gbc); gbc.gridx++; gbc.weightx = 0; actionsPane.add((last = new JButton('Last >>')), gbc); add(contentPane); add(actionsPane, BorderLayout.soUTH); NavigationHandler handler = new NavigationHandler(); first.addActionListener(handler); prevIoUs.addActionListener(handler); next.addActionListener(handler); last.addActionListener(handler); gotoFirstPage();}protected void gotoFirstPage() { currentPage = pages.get(0); cardlayout.show(contentPane, currentPage);}protected void gotoPrevIoUsPage() { int index = pages.indexOf(currentPage); index--; if (index >= 0) {currentPage = pages.get(index);cardlayout.show(contentPane, currentPage); }}protected void gotoNextPage() { int index = pages.indexOf(currentPage); index++; if (index < pages.size()) {currentPage = pages.get(index);cardlayout.show(contentPane, currentPage); }}protected void gotoLastPage() { currentPage = pages.get(pages.size() - 1); cardlayout.show(contentPane, currentPage);}protected class NavigationHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) {if (e.getSource() == first) { gotoFirstPage();} else if (e.getSource() == prevIoUs) { gotoPrevIoUsPage();} else if (e.getSource() == next) { gotoNextPage();} else if (e.getSource() == last) { gotoLastPage();} }} } public class FirstPage extends JPanel {public FirstPage() { setLayout(new BorderLayout()); JLabel label = new JLabel('Page One'); label.setHorizontalAlignment(JLabel.CENTER); add(label);} } public class SecondPage extends JPanel {public SecondPage() { setLayout(new BorderLayout()); JLabel label = new JLabel('Page Two'); label.setHorizontalAlignment(JLabel.CENTER); add(label);} } public class ThirdPage extends JPanel {public ThirdPage() { setLayout(new BorderLayout()); JLabel label = new JLabel('Page Three'); label.setHorizontalAlignment(JLabel.CENTER); add(label);} }}

或者,您可以使用基于模型的方法(可能更可取),该方法定义了组件的显示顺序。例如

解决方法

public class MainWindow extends JPanel {public static MainWindow instance = new MainWindow();private CardLayout cards = new CardLayout();public MainWindow() { setLayout(cards); add(new FirstPage(),Pages.FIRST.toString()); add(new SecondPage(),Pages.SECOND.toString()); add(new ThirdPage(),Pages.THIRD.toString());}public void showPage(Pages page) { cards.show(this,page.toString());}}

showPage(page);如果我在的构造函数中调用该方法,则该方法可以正常工作MainWindow。但是,当我尝试MainWindow.instance.showPage(Pages.SECOND);从ActionListener调用时,FirstPage什么也没有发生。我检查了该showPage(page)方法是否正常工作。我检查了是否触发了ActionEvent并输入正确的if/ else子句。我做错了什么,为什么我的第二页没有显示?

public class FirstPage extends JPanel { private JButton showSecond = new JButton('Show Second'); private JButton showThird = new JButton('Show Third'); public FirstPage() {insertButton(showSecond);insertButton(showThird); } private void insertButton(JButton button) {button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {if (e.getSource() == showSecond) { MainWindow.instance.showPage(Pages.SECOND);} else { MainWindow.instance.showPage(Pages.THIRD);} }});this.add(button); }}

相关文章: