gpt4 book ai didi

java - 从 JPanel 返回信息

转载 作者:行者123 更新时间:2023-12-02 06:17:08 26 4
gpt4 key购买 nike

我有一个 JFrame,它有一个 Panel,供 JButtons 在两个 JPanels 之间导航( >注册面板确认面板)。两个JPanel通过使用CardPanel/CardLayout相互切换。当用户在 RegistrationPanel 中输入信息时,是否有办法将此信息返回到下面列出的 StartFrame

import java.awt.BorderLayout;

public class StartFrame extends JFrame implements ActionListener
{
JButton nextButton = new JButton("Next");
JButton previousButton = new JButton("Previous");
JButton registerButton = new JButton("Register");
JPanel cardPanel = new JPanel();
CardLayout cardLayout = new CardLayout(10, 10);

public StartFrame()
{
setLayout(new BorderLayout(5, 5));
nextButton.addActionListener(this);
previousButton.addActionListener(this);
previousButton.setVisible(false);
registerButton.addActionListener(this);
registerButton.setVisible(false);

Panel buttonsPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.add(previousButton);
buttonsPanel.add(nextButton);
buttonsPanel.add(registerButton);

setCardLayout();
add(BorderLayout.SOUTH, buttonsPanel);
add(BorderLayout.CENTER, cardPanel);
}

private void setCardLayout()
{
cardPanel.setLayout(cardLayout);
cardPanel.add(new RegistrationPanel(), "registration");
cardPanel.add(new ConfirmationPanel(), "confirmation");
cardLayout.show(cardPanel, "registration");
}

@Override
public void actionPerformed(ActionEvent event)
{
if (event.getSource().equals(nextButton))
{
cardLayout.next(cardPanel);
previousButton.setVisible(true);
registerButton.setVisible(true);
nextButton.setVisible(false);
}

else
{
cardLayout.previous(cardPanel);
previousButton.setVisible(false);
registerButton.setVisible(false);
nextButton.setVisible(true);
}
}
}

最佳答案

这取决于谁在传递此信息中扮演“主动”角色。 RegistrationPanel 是否应该将信息传递回 StartFrame(例如,当单击 RegsitrationPanel 上的按钮时?)或者 StartFrame 是否应该从 RegistrationPanel 获取信息(例如,当 StartFrame 上的按钮被单击时)被点击)?

在第一种情况下,RegistrationPanel 需要对 StartFrame 的引用(最好以更“抽象”的形式,以避免对这个具体类的依赖)。在第二种情况下,StartFrame 需要引用 RegistrationPanel。

这里粗略地概述了这些方法:

对于第一种情况:

class RegistrationPanel 
{
private StartFrame startFrame;
public RegistrationPanel(StartFrame startFrame)
{
this.startFrame = startFrame;
}

// Called e.g. when a button was clicked on this panel
void passBackInformationToStartFrame()
{
startFrame.setInfo(this.getInfo());
}
}


class StartFrame ...
{
...
private void setCardLayout()
{
cardPanel.setLayout(cardLayout);

// Remove:
//cardPanel.add(new RegistrationPanel(), "registration");

// Add:
cardPanel.add(new RegistrationPanel(this), "registration");

....
}
}

对于第二种情况

public class StartFrame extends JFrame implements ActionListener
{
...
private RegistrationPanel registrationPanel

private void setCardLayout()
{
cardPanel.setLayout(cardLayout);

// Remove:
//cardPanel.add(new RegistrationPanel(), "registration");

// Add:
registrationPanel = new RegistrationPanel();
cardPanel.add(registrationPanel, "registration");
}

// Called e.g. when a button is clicked on this frame:
void fetchInformationFromRegistrationPanel()
{
Info info = registrationPanel.getInfo();
...
}

关于java - 从 JPanel 返回信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21354529/

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