编程语言
首页 > 编程语言> > 如何从多个GUI类集成多页Java桌面应用程序

如何从多个GUI类集成多页Java桌面应用程序

作者:互联网

我正在开发Java Swing桌面应用程序项目.该应用程序有大约15个GUI页面.我可以使用Layered Panes和Tabbed Panes将所有GUI组件放在一个类中.但那个班级将是巨大的.如果我可以将项目划分为几个较小的子项目并让每个项目都有一个或几个GUI页面,那将是一个想法.我可以单独处理每个子项目,并在所有子项目完成后将它们集成到一个应用程序中.
我的问题是如何集成来自不同类的所有GUI页面,以便我可以在按钮点击的同时导航回来并强制不同的页面?由于子项目包含GUI页面,因此每个子页面都需要有一个JFrame.我如何在JFrame 1和JFrame 2之间切换回来并强制使一个可见而另一个不可见?
This question显示了如何创建新的JFrame.但没有显示如何在JFrame中来回切换.

解决方法:

… The application has about 15 GUI pages. I can use Layered Panes and Tabbed Panes to put all the GUI components in one class. But that class will be huge.

不必要. GUI可以非常简单,并且可以有一个允许其他类添加页面的方法,比如说一个名为registerPage(…)的东西:

public void registerPage(JComponent page, String name) {
  pageHolder.add(page, name);
  nameComboModel.addElement(name);
}

然后给出类方法,以允许一个方法转到下一页或上一页或随机页面.例如,像这样小的类可以工作:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LotsOfPagesPanel extends JPanel {
   private CardLayout cardlayout = new CardLayout();
   private JPanel pageHolder = new JPanel(cardlayout);
   private DefaultComboBoxModel<String> nameComboModel = new DefaultComboBoxModel<String>();
   private JComboBox<String> nameCombo = new JComboBox<String>(nameComboModel);

   public LotsOfPagesPanel() {
      JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      btnPanel.add(new JButton(new PrevAction(this, "Previous", KeyEvent.VK_P)));
      btnPanel.add(new JButton(new NextAction(this, "Next", KeyEvent.VK_N)));
      JPanel bottomPanel = new JPanel();
      bottomPanel.add(btnPanel);
      bottomPanel.add(nameCombo);

      nameCombo.addActionListener(new NameComboListener());
      pageHolder.setBorder(BorderFactory.createEtchedBorder());

      setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      setLayout(new BorderLayout(5, 5));
      add(pageHolder, BorderLayout.CENTER);
      add(bottomPanel, BorderLayout.PAGE_END);
   }

   public void previousPage() {
      cardlayout.previous(pageHolder);
   }

   public void nextPage() {
      cardlayout.next(pageHolder);
   }

   public void show(String name) {
      cardlayout.show(pageHolder, name);
   }

   public void registerPage(JComponent page, String name) {
      pageHolder.add(page, name);
      nameComboModel.addElement(name);
   }

   private class NameComboListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         String selection = nameCombo.getSelectedItem().toString();
         show(selection);
      }
   }
}

所有这一类真正做的就是作为你的“页面”的存储库,并且具有允许连续或随机翻页的逻辑,而不是其他,但这就是它真正需要做的全部,并且通过限制它,我们限制班级的规模.如果需要其他功能,请为这些功能创建其他类

…比如我们的Action类,包括PrevAction类:

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

public class PrevAction extends AbstractAction {
   private LotsOfPagesPanel lotsOfPages;

   public PrevAction(LotsOfPagesPanel lotsOfPages, String name, Integer keyCode) {
      super(name);
      this.lotsOfPages = lotsOfPages;
      putValue(MNEMONIC_KEY, keyCode);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      lotsOfPages.previousPage();
   }
}

和NextAction.java

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

public class NextAction extends AbstractAction {
   private LotsOfPagesPanel lotsOfPages;

   public NextAction(LotsOfPagesPanel lotsOfPages, String name, Integer keyCode) {
      super(name);
      this.lotsOfPages = lotsOfPages;
      putValue(MNEMONIC_KEY, keyCode);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      lotsOfPages.nextPage();
   }
}

当然,你需要一个主要的方法:

import java.awt.Color;
import java.awt.Dimension;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class LotsOfPagesMain {
   private static final String[] LABELS = { "One", "Two", "Three", "Four",
         "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
         "Thirteen", "Fourteen", "Fifteen" };
   private static final Dimension LABEL_SIZE = new Dimension(400, 300);

   private static void createAndShowGui() {
      LotsOfPagesPanel lotsOfPages = new LotsOfPagesPanel();
      Random random = new Random();

      // I'm using JLabels as a simple substitute for your complex JPanel GUI "pages"
      for (String labelText : LABELS) {
         JLabel label = new JLabel(labelText, SwingConstants.CENTER);
         label.setPreferredSize(LABEL_SIZE);
         label.setOpaque(true);
         label.setBackground(new Color(random.nextInt(170) + 85, random
               .nextInt(170) + 85, random.nextInt(170) + 85));
         lotsOfPages.registerPage(label, labelText);
      }

      JFrame frame = new JFrame("LotsOfPages");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(lotsOfPages);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

但它绝不会是一个庞大的类,你不必担心在用户处翻转多个JFrame.

标签:java,user-interface,swing,layout-manager,cardlayout
来源: https://codeday.me/bug/20190715/1469508.html