gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:34:57 27 4
gpt4 key购买 nike

我正在从事 Java Swing 桌面应用程序项目。该应用程序有大约 15 个 GUI 页面。我可以使用分层面板和选项卡式面板将所有 GUI 组件放在一个类中。但是那个类(class)会很大。如果我可以将项目分成几个较小的子项目并让每个子项目有一个或几个 GUI 页面,那将是个好主意。我可以单独处理每个子项目,并在所有子项目完成后将它们集成回一个应用程序。我的问题是如何集成来自不同类的所有 GUI 页面,以便我可以在单击按钮时返回并在不同页面之间强制执行?由于子项目包含 GUI 页面,因此每个子项目都需要有一个 JFrame。我如何才能在 JFrame 1 和 JFrame 2 之间切换并强制执行,并使一个可见而另一个不可见? This question展示了如何创建新的 JFrame。但没有显示如何在 JFrames 之间来回切换。

最佳答案

... 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);
}
}
}

所有这个类真正做的是充当你的“页面”的存储库,并具有允许连续或随机翻阅页面的逻辑,除此之外没有太多,但这就是它真正需要做的,并通过限制它所以,我们限制类(class)的规模。如果需要其他功能,请为这些创建其他类

...例如我们的 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();
}
}

当然你需要有一个 main 方法:

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();
}
});
}
}

但它无论如何都不会是一个巨大的类,您不必担心在用户处翻转多个 JFrames。

关于java - 如何从多个 GUI 类集成多页 Java 桌面应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11802018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com