建立一个 Derby 日历,第 2 部分: 嵌入选项(上)
2010-04-19 00:00:00 来源:WEB开发网您最终可能希望以不同的方式构造它,但是这些例子中多数界面都在构造函数中建立。这里只需要创建一个 600 像素宽、400 像素高的窗口并让它可见就行了,如 图 2 所示。
图 2. 空白窗口
查看原图(大图)
输入表单
一旦建立了框架,就可以添加界面元素了。从 清单 2 所示的输入表单开始。
清单 2. 提供输入表单
import javax.swing.*;
import java.awt.*;
public class CalendarFrame extends JFrame {
String [] months = {"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
String [] days = {"1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "30",
"31"};
String [] years = {"2005", "2006", "2007", "2008"};
JComboBox newMonthCombo = null;
JComboBox newDayCombo = null;
JComboBox newYearCombo = null;
JTextField titleBox = null;
JTextArea descriptionBox = null;
JTextField reminderBox = null;
public CalendarFrame (){
super();
this.setSize(600, 400);
this.getContentPane().setLayout(new java.awt.GridLayout(1, 2));
//Left-hand panel
JPanel title = new JPanel();
title.add(new JLabel("<html><h2>Add New Event</h2></html>"));
//date of the event
JPanel newDate = new JPanel();
newDate.add(new JLabel("New event for:"));
newMonthCombo = new JComboBox (months);
newDate.add (newMonthCombo);
newDayCombo = new JComboBox (days);
newDate.add (newDayCombo);
newYearCombo = new JComboBox (years);
newDate.add ( newYearCombo);
//title of the event
JPanel eventTitle = new JPanel();
eventTitle.add(new JLabel("Title:"));
titleBox = new JTextField (20);
eventTitle.add (titleBox);
//description of the event
JPanel description = new JPanel();
description.add(new JLabel("Description:"));
descriptionBox = new JTextArea (8, 15);
description.add (descriptionBox);
//reminders to
JPanel reminders = new JPanel();
reminders.setLayout(new java.awt.GridLayout(2, 1));
reminders.add(new JLabel("Reminders to:"));
reminderBox = new JTextField (20);
reminders.add (reminderBox);
//save button
JPanel save = new JPanel();
JButton saveButton = new JButton ("save");
saveButton.addActionListener(new SaveListener());
save.add (saveButton);
//add to panel
JPanel left = new JPanel ();
left.add (title);
left.add (newDate);
left.add (eventTitle);
left.add (description);
left.add (reminders);
left.add (save);
this.add (left);
//right side
JPanel right = new JPanel ();
this.add (right);
}
class SaveListener implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent e) {
String month = new String()
.valueOf((newMonthCombo.getSelectedIndex()+1));
String day = newDayCombo.getSelectedItem().toString();
String year = newYearCombo.getSelectedItem().toString();
String newTitle = titleBox.getText();
String newDescription = descriptionBox.getText();
String newReminders = reminderBox.getText();
System.out.println("Save event for "+month+"/"+day+
"/"+year);
System.out.println(newTitle);
System.out.println(newDescription);
System.out.println(newReminders);
}
}
public static void main (String args []) {
CalendarFrame w = new CalendarFrame ();
w.setVisible(true);
}
}
更多精彩
赞助商链接