標題
這Date_selector_panel是重要部分
這表明這部分代碼易寫
public class Titled_date_selector extends JPanel implements Date_selector
{ private Date_selector selector;
private final JLabel title = new JLabel(
/** Wrap an existing Date_selector to add a title bar showing
* the displayed month and year
* user navigates
*/
public Titled_date_selector( Date_selector selector )
{ this
title
title
title
title
selector
( new ActionListener()
{ public void actionPerformed( ActionEvent e )
{ if( e
title
else
my_subscribers
}
}
);
setOpaque(false);
setLayout( new BorderLayout() );
add( title
add( (JPanel)selector
}
/** This constructor lets you specify the background color of the
* title strip that holds the month name and year (the default
* is light yellow)
*
* @param label_background_color the color of the title bar
* null to make it transparent
*/
public Titled_date_selector( Date_selector selector
{ this(selector);
if( label_background_color == null )
title
else
title
}
private ActionListener my_subscribers = null;
public synchronized void addActionListener(ActionListener l)
{ my_subscribers = AWTEventMulticaster
}
public synchronized void removeActionListener(ActionListener l)
{ my_subscribers = AWTEventMulticaster
}
public Date get_selected_date() { return selector
public Date get_current_date() { return selector
public void roll(int f
public int get(int f) { return selector
}
NAVIGATION/導航
下面展示的就是導航欄的實現代碼
ClassLoader loader = getClass()
loader
classloader在找類的地方找圖象資源
public class Navigable_date_selector extends JPanel implements Date_selector
{ private Date_selector selector;
// Names of image files used for the navigator bar
private static final String
NEXT_YEAR =
NEXT_MONTH =
PREVIOUS_YEAR =
PREVIOUS_MONTH =
// These constants are used to identify the button and
// as the button caption in the event that the appropriate
// image file can
private static final String FORWARD_MONTH =
FORWARD_YEAR =
BACK_MONTH =
BACK_YEAR =
private JPanel navigation = new JPanel();
public Navigable_date_selector( Date_selector selector )
{ this
setBorder( null );
setOpaque( false );
setLayout( new BorderLayout() );
add( (JPanel)selector
navigation
navigation
navigation
navigation
navigation
navigation
navigation
add(navigation
}
//
// I left out a few constructors and utility methods that go here
//
private final Navigation_handler navigation_listener
= new Navigation_handler();
/** Handle clicks from the navigation bar buttons */
private class Navigation_handler implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ String direction = e
if (direction==FORWARD_YEAR )selector
else if(direction==BACK_YEAR )selector
else if(direction==FORWARD_MONTH)
{
selector
if( selector
selector
}
else if (direction==BACK_MONTH )
{
selector
if( selector
selector
}
else
{ assert false:
}
}
}
private JButton make_navigation_button(String caption)
{
ClassLoader loader = getClass()
URL image =
(cap
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27437.html