设为首页 收藏本站
查看: 1179|回复: 0

[经验分享] 前段时间应聘IBM CRL北京笔试我的代码 in 2008

[复制链接]

尚未签到

发表于 2017-5-27 09:51:55 | 显示全部楼层 |阅读模式
@Author:cjcj  cj.yangjun@gmail.com <c-j.iteye.com>
CRL给的我第2次的笔试机会,但面试的时候没发挥好,且过几个月就毕业等客观原因被刷了。题目不难,主要看代码风格和组织结构,听说TW还有pair program,汗。代码贴这了,因为简历上有写这里的BLOG地址,可能需要看看代码风格什么的。废话不说,贴代码了。
题目:日历
效果图:



/*
* @(#)CanlendarColor.java     1.1 08/12/30
* Copyright 2008 by CJ. All rights reserved.
* This is a color management, involving all the UI of the  color defined.
* These color are used to be called by other class.
*/
package Calendar.Common;
import java.awt.Color;
/**
* Description:The CanlendarColor class is used to set the control's color.
* All the variables are set to static,
* so variables will be the beginning of the proceedings in the run-time initialization.
* Interface manages all the colors. Increased scalability of the code.
**/
public class CanlendarColor {
/** IBM is the blue, so the calendar's background has become blue */
public static Color backGroundColor = Color.blue;
/** The following are the main panel (MainPanel.java achieve) the color settings,
*  including the weekend color, font color, the color of the box, etc. */
public static Color palletTableColor = Color.white;
public static Color selectedBackColor = Color.orange;
public static Color weekFontColor = Color.white;
public static Color dateFontColor = Color.black;
public static Color weekendFontColor = Color.red;
/** The following are the main control panel (ControlPanel.java) color management */
public static Color controlcolor = Color.white;
public static Color cfgTextColor = Color.black;
public static Color rbBorderColor = Color.blue;
public static Color rbFontColor = Color.black;
public static Color rbButtonColor = Color.white;
public static Color rbBtFontColor = Color.black;
}




/*
* @(#)MyControlButton.java     1.1 08/12/30
* Copyright 2008 by CJ. All rights reserved.
*  Including a label and two button to control date.
*/
package Calendar.Common;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
/**
* Class <code>ControlButton</code> is the root of the class hierarchy.
*
* @author  cj
*/
public class MyControlButton extends JPanel {
private static final long serialVersionUID = 1L;
/**
* the minimum  value
* In each of the buttons on the button,
* the button is on the definition of a maximum or minimum value,
* based on the value of the display to limit the maximum
*/
public int min            = 0;
/**
* the maximum  value
*/
public int max            = 0;
/**
* the current  value
*/
public int now            = 0;
/**
* the down_button
* For the year, month, day to increase
*/
public JButton buttonDOWN = null;
/**
* the up_button
* For the year, month, day to reduce
*/
public JButton buttonUP   = null;
/**
* the display label
* Used to display the current value of the label
*/
public JLabel labelDateNum= null;
private Font font          = new Font("宋体", Font.PLAIN, 12);
/**
* the content panel
*/
private JPanel panelContent = null;
/**
* Description :The class Construction;Set the 'now','max','min' value
* @param int
*  now value
* @param int
* min value
*
*/
public MyControlButton(int now, int min, int max) {
if (now >= min && now <= max) {
this.now = now;
this.min = min;
this.max = max;
}
initControls();
}
/**
* Description :this method is used to init all control including one label and two button.
*/
private void initControls() {
this.setLayout(new FlowLayout(1, 2, 1));
this.setBackground(CanlendarColor.controlcolor);
labelDateNum = new JLabel("", JLabel.CENTER);
/**
* set the default value,This value is now decided by the parameters now.
*/
labelDateNum.setText("" + now);
/**
* set border
*/
labelDateNum.setBorder(new LineBorder(CanlendarColor.rbBorderColor, 1));
/**
* set the foreground color
*/
labelDateNum.setForeground(CanlendarColor.rbFontColor);
/**
* set the position
*/
labelDateNum.setPreferredSize(new Dimension(35, 15));
/**
*  set the font
*/
labelDateNum.setFont(font);
buttonUP = new JButton("^");
buttonUP.setBorder(new LineBorder(CanlendarColor.rbBorderColor, 1));
buttonUP.setBackground(CanlendarColor.rbButtonColor);
buttonUP.setForeground(CanlendarColor.rbBtFontColor);
buttonUP.setPreferredSize(new Dimension(15, 10));
buttonUP.setFont(font);
/**
* Don't allow to be focused
*/
buttonUP.setFocusable(false);
buttonDOWN = new JButton("v");
buttonDOWN.setBorder(new LineBorder(CanlendarColor.rbBorderColor, 1));
buttonDOWN.setBackground(CanlendarColor.rbButtonColor);
buttonDOWN.setForeground(CanlendarColor.rbBtFontColor);
buttonDOWN.setPreferredSize(new Dimension(15, 10));
buttonDOWN.setFont(font);
buttonDOWN.setFocusable(false);
/**
* Load control into panel
*/
add(labelDateNum);
add(getPanel());
}
/**
* Description :this method is used to init the panel.
*/
public JPanel getPanel() {
if (null == panelContent) {
/**
* set the content panel properties
*/
panelContent = new JPanel(new BorderLayout(0, 2));
panelContent.add(buttonUP, BorderLayout.CENTER);
panelContent.add(buttonDOWN, BorderLayout.SOUTH);
}
return panelContent;
}
}



/*
* @(#)MainFrame.java     1.1 08/12/30
* Copyright 2008 by CJ. All rights reserved.
*  This is the main frame including the ok_button,cancel_button and the main panel.
*  
*/
package Calendar.MainFrame;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import Calendar.Common.CanlendarColor;
import Calendar.Panels.MainTablePanel;
public class MainFrame extends JFrame{
/**
* Description:This is the main type of interface, and show that he was responsible for loading the main panel.
*  Add another 2 button and some personal info
* The main task is to load objects main panel. The main panel is the main form of the calendar.
**/
private static final long serialVersionUID = 1L;
/**
* The Calendar class is an abstract class that provides methods for converting
* between a specific instant in time and a set of fields calendar fields
* such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating
* the calendar fields, such as getting the date of the next week.
* An instant in time can be represented by a millisecond value that
* is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
*/
private  Calendar currentMonth = new GregorianCalendar();   
/**
* This is the main form of the calendar
* he needs to be used to initialize the table values from the object Calendar.
* Translate the handle of the main window in order to  account when the table when double-click the cell to control
* and than control the window.
*
*/
private MainTablePanel tablePanel   = new MainTablePanel(this, currentMonth);  


/** the Frame's content panel */
private JPanel  panelContent   = null;
/** OK Button */
private JButton buttonOK  = null;
/** Cancel Button */
private JButton buttonCancel   = null;
/** The selected date text field that tell the now date */
public  JTextField textDate    = null;
/**
* Description :Construction.Call anther function named init()
*/
public MainFrame() {
super();
initControls();
}
/**
* Description :this init() method is used to init all control including buttons and text field
*/
private void initControls() {
/**
* set the frame title
*/
setTitle("Calendar  by CJ for resume.Email:cj.yangjun@gmail.com");     
/**
* set the size
*/
setSize(400, 200);
/**
*  set the button name
*/
buttonOK = new JButton("Ok");
/**
*  set the border
*/
buttonOK.setBorder(new LineBorder(CanlendarColor.rbBorderColor, 1));
/**
* set the background color
*/
buttonOK.setBackground(CanlendarColor.rbButtonColor);
/**
* set the foreground color
*/
buttonOK.setForeground(CanlendarColor.rbBtFontColor);
buttonCancel = new JButton("Cancel");
buttonCancel.setBorder(new LineBorder(CanlendarColor.rbBorderColor, 1));
buttonCancel.setBackground(CanlendarColor.rbButtonColor);
buttonCancel.setForeground(CanlendarColor.rbBtFontColor);


textDate = new JTextField();
textDate.setEditable(false);
/**
*  set the JTextField text.
*/
textDate.setText("The Selected Date is:"
+ new SimpleDateFormat("dd-MM-yyyy").format(tablePanel.getDate()));


/**
* add the content panel.
*/
getContentPane().add(getContentPanel(), BorderLayout.CENTER);
/**
* add the Listener function
*/
addListener();
}
private JPanel getContentPanel() {
if (null == panelContent) {
/**
* init the content panel about border,background,add().
*/
panelContent = new JPanel(new BorderLayout(), true);
panelContent.setBorder(new LineBorder(CanlendarColor.backGroundColor, 2));
panelContent.setBackground(CanlendarColor.backGroundColor);
panelContent.add(tablePanel, BorderLayout.CENTER);
panelContent.add(buttonOK, BorderLayout.WEST);
panelContent.add(buttonCancel, BorderLayout.EAST);
panelContent.add(textDate, BorderLayout.NORTH);
}
return panelContent;
}
/**
* Description :this addListener() method is used to response the buttonOK and buttonCancel.
*  setting the current date from the other method tablePanel.getDate().
*/
private void addListener() {
buttonCancel.addMouseListener(new MouseAdapter() {
/**
* mouse pressed on buttonCancel
*/
public void mousePressed(MouseEvent e) {
/**
* exit the program
*/
dispose();
}
});


buttonOK.addMouseListener(new MouseAdapter() {
/**
*  mouse pressed on buttonOK
*/
public void mousePressed(MouseEvent e) {     
/**
* set the value from MainPanel's method getDate().
* Access to up-to-date period, and then update the information
*/
textDate.setText(
"The Selected Date is:"
+ new SimpleDateFormat("dd-MM-yyyy").format(tablePanel.getDate()));
}
});
}
/**
* Description :main() method is the program beginning.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
/**
*  new Frame object and show frame
*/
MainFrame app = new MainFrame();           
app.setVisible(true);
}
});
}
}




/*
* @(#)MainPanel.java     1.1 08/12/30
* Copyright 2008 by CJ. All rights reserved.
*  This is the main panel including the calendar table.
*/
package Calendar.Panels;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.*;
import Calendar.Common.CanlendarColor;
import Calendar.Common.MonthMaker;
import Calendar.MainFrame.MainFrame;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Description:The MainPanel class is used to init and refresh the month by the Calendar Object from the class named ControlPane
* this is the main panel of calendar,the main function is to define forms, real-time operating table set,
* and in response to legitimate click the mouse and double-click the mouse incident events.
* Click the mouse events need to amend the control panel (ControlPanel.java) of the label, it needs to be ControlPanel control.
* Double-click the mouse events need to amend the main frame (MainFrame.java) of the label, it needs to be MainFrame control.
*
**/
public class MainTablePanel extends JPanel {
private static final long serialVersionUID = 1L;
/**
* the handle of mainframe
*/
private MainFrame frameCalendar      = null;     
/**
* the Calendar Object
*/
private Calendar calendarCurrent     = null;   
/**
* define table head info
*/
private String[] calendarHead        = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat" };         
private ControlButtonPanel myControlButton = null;   
/**
*  used to store the month values
*/
private String[][] calendarDate      = new String[7][7];
/**
* the value of selected row
*/
private int selectedRow;
/**
* the value of selected column
*/
private int selectedColumn;
private DefaultTableModel model = null;
/**
* the main table object
*/
private JTable table            = null;      
/**
* Description :Construction
* @param MainFrame: is the handle of mainframe used to set the JTextField
*/
public MainTablePanel(MainFrame mainframe, Calendar calendarCurrent) {
/**
* get the handle of frame
*/
this.frameCalendar = mainframe;
/**
* get the value of calendar
*/
this.calendarCurrent = calendarCurrent;
initControls();
addListener();
}
@SuppressWarnings("serial")
/**
* set the talel model
*/
private DefaultTableModel getTalbeModel() {
if (null == model) {
model = new DefaultTableModel(calendarDate, calendarHead) {
public boolean isCellEditable(int rowIndex, int mColIndex) {
return false;
}
};
}
return model;
}
/**
* Description :this init() method is used to init the table control and myControlButton control.
*/
@SuppressWarnings("serial")
private void initControls(){
/**
* define ContralPanel
*/
myControlButton=new ControlButtonPanel(this, calendarCurrent);
/**
* set the table head
*/
calendarDate[0][0] = "Sun";
calendarDate[0][1] = "Mon";
calendarDate[0][2] = "Tue";
calendarDate[0][3] = "Wed";
calendarDate[0][4] = "Thu";
calendarDate[0][5] = "Fri";
calendarDate[0][6] = "Sat";
/**
* init the table
*/
table = new JTable(getTalbeModel());
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
/**
* set the cell of table and fill the color
*/
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int temprow, int tempcolumn) {
setHorizontalAlignment(JLabel.CENTER);
if (temprow == 0)
setBackground(CanlendarColor.backGroundColor);
else if (temprow == selectedRow && tempcolumn == selectedColumn) {
setBackground(CanlendarColor.selectedBackColor);
} else
setBackground(CanlendarColor.palletTableColor);
if ((tempcolumn == 0 && temprow != 0) || (tempcolumn == 6 && temprow != 0))
setForeground(CanlendarColor.weekendFontColor);
else if (temprow != 0 && tempcolumn != 0 && tempcolumn != 6)
setForeground(CanlendarColor.dateFontColor);
else
setForeground(CanlendarColor.weekFontColor);
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, temprow, tempcolumn);
}
};
/**
* refresh the cell
*/
for (int i = 0; i < calendarHead.length; i++) {
table.getColumn(calendarHead).setCellRenderer(tcr);
}
/**
* Don't allowed the row to be selected
*/
table.setRowSelectionAllowed(false);
/**
* call another method setMonth in this file
*/
setMonth(calendarCurrent);
setLayout(new BorderLayout());
/**
* add all controls into the panel
*/
add(table, BorderLayout.CENTER);
add(myControlButton, BorderLayout.NORTH);
}
/**
* Description :this addListener() method is used to response the action by the mouse pressed and set the legal date
*/
private void addListener(){
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
try {
/**
*  single click ,change the legal day in the myButton object
*/
if (e.getClickCount() == 1) {
selectedRow = table.getSelectedRow();
selectedColumn = table.getSelectedColumn();
if (selectedRow > 0&& !calendarDate[selectedRow][selectedColumn].equals("")) {
calendarCurrent.set(Calendar.DAY_OF_MONTH, Integer.valueOf(calendarDate[selectedRow][selectedColumn]).intValue());
myControlButton.setDay(Integer.valueOf(calendarDate[selectedRow][selectedColumn]).intValue());
}
}
else if (2 == e.getClickCount()) {
/**
*  double click , change the TextField context in Frame
*/
frameCalendar.textDate.setText("The Selected Date is:"+ new SimpleDateFormat("dd-MM-yyyy").format(getDate()));
}
} catch (Exception ce) {
ce.printStackTrace();
}
}
});
}
/**
* Description :this setMonth() method is used to call another method named 'monthMaker()' in MonthMaker.java
* and receive 2-dimensional array to refresh the table content.
* @param:Calendar:
*/
public void setMonth(Calendar calendarCurrent) {
/**
*  get the calendar handle
*/
this.calendarCurrent = calendarCurrent;
/**
* get the new value used to refresh table
*/
String[][] tmpDate = MonthMaker.monthMaker(calendarCurrent);
/**
* init the table again
*/
for (int i = 1; i < 7; i++)
for (int j = 0; j < 7; j++) {
calendarDate[j] = tmpDate[i - 1][j];
table.setValueAt("" + tmpDate[i - 1][j], i, j);
}
}
/**
* Description :this getDate() method is used to return the value from the method getDate() in ControlPanel.java
*@return Date: return the current date.
*/
public Date getDate(){
return myControlButton.getDate();
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-381617-1-1.html 上篇帖子: IBM AIX 5.3 系统管理 -- 磁盘存储管理一 下篇帖子: 理解 pureQuery:IBM 最新的 Java 数据库应用编程范例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表