它将建议一个参考问题。
public static MainWindow instance = newMainWindow();看起来很可疑,因为您必须先创建一个
MainWindowfirst
实例才能进行初始化,这建议您现在有两个实例
MainWindow,一个在屏幕上,另一个不在屏幕上。
static以这种方式使用不是一个好主意,因为它会导致类似这样的问题。相反,您应该将控制器的引用传递给页面。控制器将定义每个页面可以执行的 *** 作(如果 *** 作正确,则将其定义为
interface)
另外,您可以将导航与页面分开,形成单独的机制,这意味着页面不在乎,可以简单地以您想要的任何顺序显示或在其他地方重复使用
Example#1-基于控制器的页面
本示例定义了一个简单的控制器,页面可以调用该控制器来实现页面导航
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(); } @Override public void nextPage() { int index = pages.indexOf(currentPage); index++; if (index < pages.size()) { cardLayout.next(this); currentPage = pages.get(index); } } @Override public void previousPage() { int index = pages.indexOf(currentPage); index--; if (index >= 0) { cardLayout.previous(this); currentPage = pages.get(index); } } @Override public void lastPage() { Page page = pages.get(pages.size() - 1); showPage(page); } @Override public 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); } @Override public NavigationController getNavigationController() { return navigationController; } @Override public JComponent getView() { return this; } @Override public 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); } @Override public 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); } @Override public 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); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == previous) { getNavigationController().previousPage(); } } }}
Example#2-中央控制器的例子
此示例将控制器与页面分开,以便按钮不属于页面本身。这样可以释放页面/视图,使其成为您需要的任何内容
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); } }}
Example#3-基于模型
或者,您可以使用基于模型的方法(可能更可取),该方法定义了组件的显示顺序。例如
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)