Пишу GUI калькулятор на Java. Создал класс Panel, в котором реализовал цифровую панель и кнопки основных действий. Хочу добавить функционала, но код класса Panel становится трудно читать (для новичка). Можно обойтись комментариями для отделения отдельных функций, но хотелось бы изучить структуру Java-проекта. По сути, класс Panel сейчас содержит функционал нескольких классов. Как мне разбить этот класс на несколько, чтобы "Отрисовка панели" была в классе Panel, а реализация кнопок и ручного ввода с клавиатуры в других, и связать эти классы между собой? Буду благодарен за пример кода. Вот как выглядит класс Panel
package com.calculator.main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Panel extends JPanel {
private JButton numbers []= new JButton[10];
private Font main_font = new Font("Times New Roman", Font.BOLD,20);
private JTextField output = new JTextField();
private JTextField log = new JTextField();
private JButton backspace = new JButton("C");
private JButton equ = new JButton("=");
private JButton plus = new JButton("+"), minus = new JButton("-"), div = new JButton(":"), multy = new JButton("*");
private int first_velue;
private String operation;
public Panel(){
setLayout(null);
setFocusable(true);
grabFocus();
numbers[0]= new JButton("0");
numbers[0].setBounds(70, 250, 50,50);
numbers[0].setFont(main_font);
add(numbers[0]);
for(int x = 0; x<3; x++){
for (int y =0; y<3;y++){
numbers[x*3+y+1]= new JButton((x*3+y+1)+"");
numbers[x*3+y+1].setBounds(y*(50+10)+10, x*(50+10)+70, 50,50);
numbers[x*3+y+1].setFont(main_font);
add(numbers[x*3+y+1]);
}
}
backspace.setBounds(10, 250, 50,50);
backspace.setFont(main_font);
add(backspace);
equ.setBounds(130, 250, 50, 50);
equ.setFont(main_font);
add(equ);
plus.setBounds(190, 70, 50, 50);
plus.setFont(main_font);
add(plus);
minus.setBounds(190, 130, 50, 50);
minus.setFont(main_font);
add(minus);
multy.setBounds(190, 190, 50, 50);
multy.setFont(main_font);
add(multy);
div.setBounds(190, 250, 50, 50);
div.setFont(main_font);
add(div);
output.setBounds(10,10,330,50);
output.setEditable(false);
output.setFont(main_font);
add(output);
log.setBounds(250, 70, 80, 230);
log.setEditable(false);
log.setFont(main_font);
add(log);
// Должен заканчиваться класс Panel
ActionListener l = (ActionEvent button_is_presed)->{
JButton b =(JButton) button_is_presed.getSource();
output.setText(output.getText()+b.getText());
};
for(JButton b :numbers){
b.addActionListener(l);
}
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e){
char symvol = e.getKeyChar();
if(symvol=='+'){
first_velue = Integer.valueOf(output.getText());
log.setText(first_velue+"");
output.setText("");
operation ="+";
}
if(symvol=='-'){
first_velue = Integer.valueOf(output.getText());
output.setText("");
operation ="-";
}
if(symvol=='/'){
first_velue = Integer.valueOf(output.getText());
output.setText("");
operation =":";
}
if(symvol=='*'){
first_velue = Integer.valueOf(output.getText());
output.setText("");
operation ="*";
}
if (symvol==KeyEvent.VK_ENTER){
int second_value = Integer.valueOf(output.getText());
if ("+".equals(operation)){
output.setText(first_velue+second_value+"");
};
if(symvol==KeyEvent.VK_BACK_SPACE){
String tmp =output.getText();
output.setText(tmp.substring(0,tmp.length()-1));
};
if ("-".equals(operation)){
output.setText(first_velue-second_value+"");
};
if ("*".equals(operation)){
output.setText(first_velue*second_value+"");
};
if (":".equals(operation)){
output.setText(first_velue/second_value+"");
};
};
if(!Character.isDigit(symvol)){
return;
}
else{
output.setText(output.getText()+ symvol);
}
}
}
);
backspace.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String tmp =output.getText();
output.setText(tmp.substring(0,tmp.length()-1));
}
});
plus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
first_velue = Integer.valueOf(output.getText());
output.setText("");
operation ="+";
}
});
minus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
first_velue = Integer.valueOf(output.getText());
output.setText("");
operation ="-";
}
});
div.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
first_velue = Integer.valueOf(output.getText());
output.setText("");
operation =":";
}
});
multy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
first_velue = Integer.valueOf(output.getText());
output.setText("");
operation ="*";
}
});
equ.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int second_value = Integer.valueOf(output.getText());
if ("+".equals(operation)){
output.setText(first_velue+second_value+"");
};
if ("-".equals(operation)){
output.setText(first_velue-second_value+"");
};
if ("*".equals(operation)){
output.setText(first_velue*second_value+"");
};
if (":".equals(operation)){
output.setText(first_velue/second_value+"");
};
};
});
}
}