gpt4 book ai didi

java - 从终止当前面板的另一个类(方法)的面板调用方法

转载 作者:行者123 更新时间:2023-12-03 22:58:08 25 4
gpt4 key购买 nike

我有三个类(class){ FirstFrame, Login, Register } ... FirstFrame 中有一个方法在 Logi 之间切换和 Register

FirstFrame代码:

public class FirstFrame extends javax.swing.JFrame {

public FirstFrame() {
initComponents();
setLayout(new BorderLayout());
changeTo(Login.class);
}

public void changeTo(Class<?> c){
getContentPane().removeAll();
try {
Object obj = c.newInstance();
getContentPane().add((Component) obj);
pack();
setVisible(true);
System.out.println("here");
} catch (InstantiationException ex) {
Logger.getLogger(FirstFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(FirstFrame.class.getName()).log(Level.SEVERE, null, ex);
}

}
}

Register我有一个存储数据然后返回到 Login 的按钮(据说)

如果我从 FirstFrame 中的任何按钮调用,效果会很好

这里的问题是如何调用 changeTo(Class<?> c)从里面Register

最佳答案

  1. 使用 CardLayout帮助您进行交换,因为这正是它的用途。
  2. 为 FirstFrame 提供一个允许其他类进行交换的公共(public)方法。
  3. 将 FirstFrame 的实例传递给需要调用此方法的对象。这可以通过构造函数或 setter 方法参数来完成。

例如,

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class FirstFrame extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = 300;
private Login login = new Login(this);
private Register register = new Register(this);
private CardLayout cardLayout = new CardLayout();

public FirstFrame() {
setLayout(cardLayout);
add(login, Login.class.toString());
add(register, Register.class.toString());
}

public void swapCards(String key) {
cardLayout.show(this, key);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

private static void createAndShowGui() {
FirstFrame mainPanel = new FirstFrame();

JFrame frame = new JFrame("FirstFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

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

class SwapCardAction extends AbstractAction {
private String key;
private FirstFrame firstFrame;

public SwapCardAction(String name, int mnemonic, String key,
FirstFrame firstFrame) {
super(name);
this.key = key;
this.firstFrame = firstFrame;
putValue(MNEMONIC_KEY, mnemonic);
}

@Override
public void actionPerformed(ActionEvent evt) {
firstFrame.swapCards(key);
}
}

class Register extends JPanel {

public Register(final FirstFrame firstFrame) {
setBackground(new Color(255, 200, 200));
JButton gotToLoginBtn = new JButton(new SwapCardAction("Login",
KeyEvent.VK_L, Login.class.toString(), firstFrame));
JPanel btnPanel = new JPanel();
btnPanel.setOpaque(false);
btnPanel.add(gotToLoginBtn);

setLayout(new BorderLayout());
JLabel label = new JLabel("Register", SwingConstants.CENTER);
add(label, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
}

}

class Login extends JPanel {

public Login(FirstFrame firstFrame) {
JButton gotToRegisterBtn = new JButton(new SwapCardAction("Register",
KeyEvent.VK_R, Register.class.toString(), firstFrame));
setBackground(new Color(150, 200, 255));
JPanel btnPanel = new JPanel();
btnPanel.add(gotToRegisterBtn);
btnPanel.setOpaque(false);

setLayout(new BorderLayout());
JLabel label = new JLabel("Login", SwingConstants.CENTER);
add(label, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
}

}

关于java - 从终止当前面板的另一个类(方法)的面板调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22888771/

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