Wróć do poprzedniego JPanela
Mam odziedziczony kod iz powodów do wyjaśnienia jestem zobowiązany do użycia układu zerowego. Próbowałem nawigować między JPanels. Nie byłem w stanie dowiedzieć się, jak to zrobić. Oto, co mam teraz, który kompiluje się w poniższy SSCCE. Próbuję dodać JPanels do ArrayList, który zawiera odniesienie do poprzednich JPanels. W ten sposób mogę wywołać JPanel „home” z bieżącego JPanel, w którym znajduje się użytkownik. Od tej chwili przechodzi do poprzedniego JPanela, ale zawartość jest pusta. Jakakolwiek pomoc będzie wspaniała, dzięki!
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);
}
}