Cześć.
Pierwszy raz próbuję swoich sił z napisaniem programu z użyciem swinga i napotkałem pewien problem.
Obecnie wygląda to w ten sposób. Chciałbym, aby przyciski po lewej stronie umieszczone w NavigationPanel uruchamiały różne metody w klasie MainPanel, która jest umieszczona w białym polu po prawej.
MainPanel miałby wyświetlać np. formularz który pozwala wprowadzić dane do programu albo wyświetlać wcześniej wprowadzone dane.
Wszystko przy założeniu że nawigacja po lewej ma być oddzielną klasą od pola które wyświetla rzeczy po prawej. Chcę wiedzieć jak rozbić wszystko na wiele klas, żeby kod był bardziej uporządkowany.
Na koniec żeby metody w MainFrame mogły dodawać obiekty dynamicznie do swojego kontenera.
Main.java
import frame.ProgramFrame;
public class Main {
public static void main(String[] args) {
new ProgramFrame();
}
}
ProgramFrame.java
package frame;
import registerLogic.Register;
import javax.swing.*;
import java.awt.*;
public class ProgramFrame extends JFrame {
HeaderPanel header = new HeaderPanel();
FooterPanel footer = new FooterPanel();
NavigationPanel navigation = new NavigationPanel();
MainPanel mainPanel = new MainPanel();
Register reg = new Register();
public ProgramFrame(){
// frame options
this.setLayout(new BorderLayout(3,3));
this.setTitle("Employee register");
this.setSize(1024,720);
this.setVisible(true); // make this visible
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // defines action to X button - default is HIDE_ON_CLOSE
this.getContentPane().setBackground(Color.black);
// setting panels
this.add(header, BorderLayout.NORTH);
this.add(footer, BorderLayout.SOUTH);
this.add(navigation, BorderLayout.WEST);
this.add(mainPanel, BorderLayout.CENTER);
}
}
NavigationPanel.java
package frame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class NavigationPanel extends JPanel {
JButton addDataBtn = new JButton();
JButton viewDataBtn = new JButton();
public NavigationPanel(){
this.setBackground(Color.lightGray);
this.setPreferredSize(new Dimension(200,100));
// addData button
addDataBtn.setBounds(0,0,100,100);
addDataBtn.setText("Add data");
addDataBtn.setFocusable(false);
addDataBtn.addActionListener(NavigationPanel::AddDataBtnAction);
this.add(addDataBtn);
// viewData button
viewDataBtn.setBounds(0,0,100,100);
viewDataBtn.setText("View data");
viewDataBtn.setFocusable(false);
viewDataBtn.addActionListener(NavigationPanel::ViewDataBtnAction);
this.add(viewDataBtn);
}
private static void AddDataBtnAction(ActionEvent e) {
// ProgramFrame.mainPanel.viewAddData();
}
private static void ViewDataBtnAction(ActionEvent e) {
// ProgramFrame.mainPanel.ViewDataPanel();
}
}
MainPanel.java
package frame;
import javax.swing.*;
public class MainPanel extends JPanel {
public MainPanel(){}
public void viewAddData(){
// dodanie jakichś przycisków i pól tekstowch
// wywoływanie metod z klasy w polu ProgramFrame.reg;
}
public void ViewDataPanel(){}
}