建立一个 Derby 日历,第 2 部分: 嵌入选项(上)
2010-04-19 00:00:00 来源:WEB开发网其中的内容很多(大部分有 Java 代码的章节都是如此),我们一步一步地来看。首先使用 GridLayout 将刚刚创建的框架分成两半。左侧创建输入新事件需要的各种下拉菜单和文本字段。还创建了一个保存按钮。
按钮在单击的时候需要作出响应,于是增加了 ActionListener 类。ActionListener 采用内部类 ShowListener 的形式。现在,当用户单击 save 按钮时,应用程序直接把信息写入命令行。比如,首先创建一个如 图 3 所示的事件。
图 3. 输入表单
查看原图(大图)
单击 save 按钮的时候就会在命令行中看到以下内容:
清单 3. 命令行内容
Save event for 7/3/2005
Sis's birthday
Don't forget to call!
reminders@nicholaschase.com
在把这些信息保存到数据库之前,首先完成整个框架。
显示面板
框架右侧包含显示已有事件所需要的控件。包括选择当前日期的下拉菜单和发出请求的按钮,如 清单 4 所示。
清单 4. 提供显示区域
import javax.swing.*;
import java.awt.*;
public class CalendarFrame extends JFrame {
...
JTextArea descriptionBox = null;
JTextField reminderBox = null;
JComboBox showMonthCombo = null;
JComboBox showDayCombo = null;
JComboBox showYearCombo = null;
JTextArea events = null;
JLabel monthToShow = null;
JLabel dayToShow = null;
JLabel yearToShow = null;
public CalendarFrame (){
super();
this.setSize(600, 400);
this.getContentPane().setLayout(new java.awt.GridLayout(1, 2));
//Left-hand panel
JPanel title = new JPanel();
...
//right side
JPanel right = new JPanel ();
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
JPanel rightTitle = new JPanel ();
rightTitle.add (new JLabel ("<html><H2> Events </H2></html>"));
JPanel showDate = new JPanel();
showDate.add(new JLabel("Events for:"));
showMonthCombo = new JComboBox (months);
showDate.add (showMonthCombo);
showDayCombo = new JComboBox (days);
showDate.add (showDayCombo);
showYearCombo = new JComboBox (years);
showDate.add ( showYearCombo);
JPanel showButtonPanel = new JPanel ();
JButton showButton = new JButton ("show");
showButton.addActionListener(new ShowListener());
showButton.setHorizontalAlignment(SwingConstants.CENTER);
showButtonPanel.add (showButton);
JPanel dateToShow = new JPanel ();
JLabel monthToShow = new JLabel ("1");
JLabel dayToShow = new JLabel ("1");
JLabel yearToShow = new JLabel ("2005");
dateToShow.add(monthToShow);
dateToShow.add (new JLabel ("/"));
dateToShow.add (dayToShow);
dateToShow.add (new JLabel ("/"));
dateToShow.add (yearToShow);
JPanel showEvents = new JPanel();
JScrollPane scrollEvents = new JScrollPane();
events = new JTextArea(10, 20);
//scrollEvents.add(events);
showEvents.add(events);
right.add (rightTitle);
right.add (showDate);
right.add (showButtonPanel);
right.add (dateToShow);
right.add(showEvents);
this.add (right);
}
class SaveListener implements java.awt.event.ActionListener
{
...
}
class ShowListener implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
String month =
new String().valueOf((showMonthCombo.getSelectedIndex()+1));
String day = showDayCombo.getSelectedItem().toString();
String year = showYearCombo.getSelectedItem().toString();
System.out.println("Show events for "+month+"/"+day+"/"+year);
}
}
public static void main (String args []) {
CalendarFrame w = new CalendarFrame ();
w.setVisible(true);
}
}
更多精彩
赞助商链接