アプリでタスクを一覧表示する(続き)- Javaでデスクトップトップアプリ(予定&ToDo管理)を作る-029
前回に引き続いて、「明日」「スケジュール」「いつか」「完了済み」のパネルの部分も完成させていきましょう。
やることは前回と同じなので簡単です。
MainFrameのactionPerformedメソッド内のswitch文内を書き換えます。
一応、コードを全て載せておきます。
[MainFrame.java]
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class MainFrame extends JFrame implements ActionListener{
JPanel mainPanel;
CardLayout layout;
AddModePanel addModePanel;
JPanel todayModePanel ;
JPanel tomorrowModePanel;
JPanel scheduleModePanel;
JPanel somedayModePanel;
JPanel doneModePanel;
List todayTasks,tomorrowTasks,scheduleTasks,somedayTasks,doneTasks;
public MainFrame(String title){
setTitle(title);//Frameのタイトルの設定
setBounds(100, 100, 900, 600);//Frameの左上x座標,y座標,幅,高さの設定
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//☓ボタンが押された時に終了する
Container contentPane = getContentPane();
JPanel headerPanel = new JPanel();
headerPanel.setBackground(new Color(67,135,233));
headerPanel.setPreferredSize(new Dimension(900,50));
JPanel sidePanel = new JPanel();
sidePanel.setBackground(new Color(120,120,120));
sidePanel.setPreferredSize(new Dimension(190,600));
this.addModePanel = new AddModePanel();
/*View用のパネル追加*/
this.todayModePanel = new JPanel();
this.tomorrowModePanel = new JPanel();
this.scheduleModePanel = new JPanel();
this.somedayModePanel = new JPanel();
this.doneModePanel = new JPanel();
/*CardLayout*/
this.mainPanel = new JPanel();
this.layout = new CardLayout();
this.mainPanel.setLayout(layout);
this.mainPanel.add(addModePanel.getPanel(),"Add");
this.mainPanel.add(todayModePanel,"Today");
this.mainPanel.add(tomorrowModePanel,"Tomorrow");
this.mainPanel.add(scheduleModePanel,"Schedule");
this.mainPanel.add(somedayModePanel,"Someday");
this.mainPanel.add(doneModePanel,"Done");
contentPane.add(headerPanel, BorderLayout.NORTH);
contentPane.add(sidePanel, BorderLayout.WEST);
contentPane.add(mainPanel);
JLabel headerLabel = new JLabel("To Do");
headerLabel.setForeground(Color.WHITE);
headerLabel.setPreferredSize(new Dimension(900, 50));
headerLabel.setHorizontalAlignment(JLabel.CENTER);
headerLabel.setFont(new Font("Century", Font.BOLD, 26));
headerPanel.add(headerLabel,BorderLayout.CENTER);
SideButton buttonAddMode = new SideButton("追加");
buttonAddMode.setActionCommand("Add");
buttonAddMode.addActionListener(this);
SideButton buttonTodayMode = new SideButton("今日");
buttonTodayMode.setActionCommand("Today");
buttonTodayMode.addActionListener(this);
SideButton buttonTomorrowMode = new SideButton("明日");
buttonTomorrowMode.setActionCommand("Tomorrow");
buttonTomorrowMode.addActionListener(this);
SideButton buttonScheduleMode = new SideButton("スケジュール");
buttonScheduleMode.setActionCommand("Schedule");
buttonScheduleMode.addActionListener(this);
SideButton buttonSomedayMode = new SideButton("いつか");
buttonSomedayMode.setActionCommand("Someday");
buttonSomedayMode.addActionListener(this);
SideButton buttonDoneMode = new SideButton("完了済み");
buttonDoneMode.setActionCommand("Done");
buttonDoneMode.addActionListener(this);
sidePanel.add(buttonAddMode);
sidePanel.add(buttonTodayMode);
sidePanel.add(buttonTomorrowMode);
sidePanel.add(buttonScheduleMode);
sidePanel.add(buttonSomedayMode);
sidePanel.add(buttonDoneMode);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd==null){
return;
}else{
switch(cmd){
case "Today" :
Calendar calendar = Calendar.getInstance();
Map mtProperty = new HashMap();
mtProperty.put(PropertyName.STARTYEAR.toString(), calendar.get(Calendar.YEAR));
mtProperty.put(PropertyName.STARTMONTH.toString(), calendar.get(Calendar.MONTH));
mtProperty.put(PropertyName.STARTDATE.toString(), calendar.get(Calendar.DATE));
mtProperty.put(PropertyName.STARTWEEK.toString(), calendar.get(Calendar.WEEK_OF_YEAR));
mtProperty.put(PropertyName.FINISH.toString(), false);
this.view(mtProperty,this.todayModePanel);
calendar=null;
mtProperty=null;
break;
case "Tomorrow" :
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);
Map mtProperty1 = new HashMap();
mtProperty1.put(PropertyName.STARTYEAR.toString(), tomorrow.get(Calendar.YEAR));
mtProperty1.put(PropertyName.STARTMONTH.toString(), tomorrow.get(Calendar.MONTH));
mtProperty1.put(PropertyName.STARTDATE.toString(), tomorrow.get(Calendar.DATE));
mtProperty1.put(PropertyName.STARTWEEK.toString(), tomorrow.get(Calendar.WEEK_OF_YEAR));
mtProperty1.put(PropertyName.FINISH.toString(), false);
this.view(mtProperty1,this.tomorrowModePanel);
tomorrow=null;
mtProperty1=null;
break;
case "Schedule" :
Map mtProperty2 = new HashMap();
mtProperty2.put(PropertyName.WHETHER.toString(), true);
mtProperty2.put(PropertyName.FINISH.toString(), false);
this.view(mtProperty2,this.scheduleModePanel);
mtProperty2=null;
break;
case "Someday" :
Map mtProperty3 = new HashMap();
mtProperty3.put(PropertyName.FINISH.toString(), false);
this.view(mtProperty3,this.somedayModePanel);
mtProperty3=null;
break;
case "Done" :
Map mtProperty4 = new HashMap();
mtProperty4.put(PropertyName.FINISH.toString(), false);
this.view(mtProperty4,this.doneModePanel);
mtProperty4=null;
break;
default :
}
}
layout.show(this.mainPanel,cmd);
}
public void view(Map pr,JPanel panel){
int n=0;//Taskを数える用の変数
JPanel outer = new JPanel();//TaskViewを詰める用のパネル
SpringLayout layout1 = new SpringLayout();//ScrollPaneを設定するためのレイアウト
outer.setLayout(layout1);
SpringLayout layout2 = new SpringLayout();//更新ボタンを設置するためのレイアウト
panel.setLayout(layout2);
JScrollPane scroll=new JScrollPane(outer) ;
TaskProperty matching = new TaskProperty(pr);
List matchingTasks = Main.box.search(matching);
LinkedList taskView = new LinkedList();
if(!matchingTasks.isEmpty()){
for(Iterator i = matchingTasks.iterator(); i.hasNext();){
Task task = (Task) i.next();
taskView.add(new TaskView(task));
if(n>0){
layout1.putConstraint(SpringLayout.NORTH, taskView.get(n).getPanel(), 1, SpringLayout.SOUTH, taskView.get(n-1).getPanel());//前のタスクの1px下に配置
}
outer.add(taskView.get(n).getPanel());
n=n+1;
}
}else{
return;
}
outer.setPreferredSize(new Dimension(700,61*(n)));//タスクの数に合わせてパネルのサイズを大きくする
scroll.setPreferredSize(new Dimension(710,480));
JButton update= new JButton("更新");
layout2.putConstraint(SpringLayout.SOUTH, update, -10, SpringLayout.SOUTH, panel);//更新ボタンの位置設定
layout2.putConstraint(SpringLayout.EAST, update, -10, SpringLayout.EAST, panel);
panel.add(scroll);
panel.add(update);
return;
}
}
アプリ開発記の次の記事はこちら => パネルの中身を書き換える- Javaでデスクトップトップアプリ(予定&ToDo管理)を作る-030