我继承了代码,并且出于长时间解释的原因,我需要使用空布局。我一直在尝试了解他们在 JPanel 之间的导航。我一直无法弄清楚如何。这就是我现在所拥有的,它编译成下面的 SSCCE。我正在尝试做的是将 JPanel 添加到包含对以前 JPanel 的引用的 ArrayList 中。这样我就可以从用户所在的当前 JPanel 调用“家”JPanel。到目前为止,它转到了前一个 JPanel,但内容是空的。任何帮助都会很棒,谢谢!
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Mainscreen extends JFrame {
public JPanel Home;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mainscreen frame = new Mainscreen();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Mainscreen() {
ArrayList <JPanel> jpLayout = new ArrayList();
final Dataentrylog DEL = new Dataentrylog(this, jpLayout);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(100, 100, 618, 373);
Home=new JPanel();
Home.setBackground(new Color(255, 250, 250));
Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
Home.setVisible(true);
setContentPane(Home);
Home.setLayout(null);
JButton delLog = new JButton("Next JPanel");
delLog.setFont(new Font("Tahoma", Font.PLAIN, 14));
delLog.setForeground(new Color(0, 0, 0));
delLog.setBackground(UIManager.getColor("Menu.selectionBackground"));
Home.add(delLog);
jpLayout.add(Home);
delLog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Home.setVisible(false);
setContentPane(DEL);
getContentPane().setLayout(null);
}
});
delLog.setBounds(44, 214, 213, 61);
}
}
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Dataentrylog extends JPanel {
public Dataentrylog(final JFrame parent, final ArrayList <JPanel> jpLayout) {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setLayout(null);
final JButton btnSignIn = new JButton("Go Back");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
parent.setContentPane(jpLayout.get(0));
setLayout(null);
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}