Hello, this is a certain company engineer. I'm studying because I think it's bad that I can only understand object-oriented programming vaguely even though I'm an engineer. As part of that, I decided to understand object-oriented writing while creating a task management app. I will leave the process as a memorandum.
For the time being, the specifications are like this.
-Personal desktop application (I don't want to log in or anything else)
・ The basic UI respects trello
https://trello.com/
-Use SQLite
to record registered tasks
So I made the top page.
It works with a code like this. (Before refactoring.)
public class Top extends JFrame{
/**
*Launch application
*/
public static void main(String[] args) {
Top frame = new Top("Task Manager");
frame.setVisible(true);
}
Top(String title){
setTitle(title);
setBounds(100, 100, 1000, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
//TODO task display area
//Task display area layout
JPanel TodoPanel = new JPanel();
TodoPanel.setPreferredSize(new Dimension(300, 600));
TodoPanel.setBackground(Color.WHITE);
TodoPanel.setLayout(new BoxLayout(TodoPanel, BoxLayout.X_AXIS));
//Panel for placing titles
JPanel TodoTitlePanel = new JPanel();
TodoTitlePanel.setPreferredSize(new Dimension(300, 60));
TodoTitlePanel.setBackground(Color.WHITE);
TodoTitlePanel.setLayout(new BoxLayout(TodoTitlePanel, BoxLayout.Y_AXIS));
TodoTitlePanel.setAlignmentY(0.0f);
TodoPanel.add(TodoTitlePanel);
//Title placement
JLabel label = new JLabel("TODO");
label.setFont(new Font("MS gothic", Font.BOLD, 32));
label.setAlignmentX(0.5f);
TodoTitlePanel.add(label);
//Frame border
LineBorder border = new LineBorder(Color.BLACK, 2, true);
TodoPanel.setBorder(border);
//DOING task display area
//Task display area layout
JPanel DoingPanel = new JPanel();
DoingPanel.setPreferredSize(new Dimension(300, 600));
DoingPanel.setBackground(Color.WHITE);
DoingPanel.setLayout(new BoxLayout(DoingPanel, BoxLayout.X_AXIS));
//Panel for placing titles
JPanel DoingTitlePanel = new JPanel();
DoingTitlePanel.setPreferredSize(new Dimension(300, 60));
DoingTitlePanel.setBackground(Color.WHITE);
DoingTitlePanel.setLayout(new BoxLayout(DoingTitlePanel, BoxLayout.Y_AXIS));
DoingTitlePanel.setAlignmentY(0.0f);
DoingPanel.add(DoingTitlePanel);
//Title placement
JLabel doingLabel = new JLabel("DOING");
doingLabel.setFont(new Font("MS gothic", Font.BOLD, 32));
doingLabel.setAlignmentX(0.5f);
DoingTitlePanel.add(doingLabel);
//Frame border
LineBorder border2 = new LineBorder(Color.BLACK, 2, true);
DoingPanel.setBorder(border2);
//DONE task display area
//Task display area layout
JPanel DonePanel = new JPanel();
DonePanel.setPreferredSize(new Dimension(300, 600));
DonePanel.setBackground(Color.WHITE);
DonePanel.setLayout(new BoxLayout(DonePanel, BoxLayout.X_AXIS));
//Panel for placing titles
JPanel DoneTitlePanel = new JPanel();
DoneTitlePanel.setPreferredSize(new Dimension(300, 60));
DoneTitlePanel.setBackground(Color.WHITE);
DoneTitlePanel.setLayout(new BoxLayout(DoneTitlePanel, BoxLayout.Y_AXIS));
DoneTitlePanel.setAlignmentY(0.0f);
DonePanel.add(DoneTitlePanel);
//Frame border
LineBorder border3 = new LineBorder(Color.BLACK, 2, true);
DonePanel.setBorder(border3);
//Title placement
JLabel doneLabel = new JLabel("DONE");
doneLabel.setFont(new Font("MS gothic", Font.BOLD, 32));
doneLabel.setAlignmentX(0.5f);
DoneTitlePanel.add(doneLabel);
//Button layout
JPanel buttonPanel = new JPanel();
JButton createButton = new JButton("Create New");
createButton.setPreferredSize(new Dimension(100,50));
buttonPanel.add(createButton);
JButton editButton = new JButton("Edit task");
editButton.setPreferredSize(new Dimension(100,50));
buttonPanel.add(editButton);
Container contentPane = getContentPane();
contentPane.add(TodoPanel);
contentPane.add(DoingPanel);
contentPane.add(DonePanel);
contentPane.add(buttonPanel);
}
}
It's long. There are three places where the same processing is done. When making labels and panels with swing, it is troublesome because you have to new each time. So, the code below summarizes the duplicated processing.
public class Top extends JFrame{
/**
*Launch application
*/
public static void main(String[] args) {
Top frame = new Top("Task Manager");
frame.setVisible(true);
}
Top(String title){
setTitle(title);
setBounds(100, 100, 1000, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
//TODO task display
makeTaskPanel("TODO");
//DOING task display
makeTaskPanel("DOING");
//DONE task display area
makeTaskPanel("DONE");
//Button layout
JPanel buttonPanel = new JPanel();
TaskRegister taskRegisterPanel = new TaskRegister();
this.add(taskRegisterPanel);
taskRegisterPanel.setVisible(false);
JButton createButton = new JButton("Create New");
buttonPanel.add(createButton);
JButton editButton = new JButton("Edit task");
editButton.setPreferredSize(new Dimension(100,50));
buttonPanel.add(editButton);
Container contentPane = getContentPane();
contentPane.add(buttonPanel);
}
public void makeTaskPanel(String panelTitle){
//Create a task panel with the title text specified in title
JPanel mainPanel = new JPanel();
JPanel titlePanel = new JPanel();
JLabel titleLabel = new JLabel(panelTitle);
LineBorder border = new LineBorder(Color.BLACK, 2, true);
//Task display area layout
mainPanel.setPreferredSize(new Dimension(300, 600));
mainPanel.setBackground(Color.WHITE);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
//Panel for placing titles
titlePanel.setPreferredSize(new Dimension(300, 60));
titlePanel.setBackground(Color.WHITE);
titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
titlePanel.setAlignmentY(0.0f);
mainPanel.add(titlePanel);
//Title placement
titleLabel.setFont(new Font("MS gothic", Font.BOLD, 32));
titleLabel.setAlignmentX(0.5f);
titlePanel.add(titleLabel);
//Frame border
mainPanel.setBorder(border);
Container contentPane = getContentPane();
contentPane.add(mainPanel);
}
}
What was duplicated
Does that mean that duplicate functions should be grouped together as much as possible so that the caller can easily call them? So I will continue to do my best.
Recommended Posts