import java.awt.BorderLayout
import java.awt.Color
import java.awt.Component
import java.awt.Dimension
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.io.File
import javax.swing.BorderFactory
import javax.swing.ImageIcon
import javax.swing.JButton
DateDropFrame extends JFrame给year和month添加一个ItemListener来实时计算指定年和月中有多少天就可以了.
代码是用Swing写的,不过看你的图,不像是Swing界面.
/**
* Create the frame.
*/
public DateDropFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setBounds(100, 100, 450, 300)
contentPane = new JPanel()
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5))
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT))
final JComboBox year = new JComboBox()
year.setModel(new DefaultComboBoxModel(getModel(start, end)))
contentPane.add(year)
final JComboBox month = new JComboBox()
month.setModel(new DefaultComboBoxModel(getModel(1, 12)))
contentPane.add(month)
final JComboBox day = new JComboBox()
contentPane.add(day)
year.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
setDay(year, month, day)
}
})
month.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
setDay(year, month, day)
}
})
setDay(year, month, day)
setContentPane(contentPane)
}
/**
* calculate days in select month &year
*/
private void setDay(JComboBox year, JComboBox month, JComboBox day) {
int y = Integer.parseInt((String) year.getSelectedItem())
int m = Integer.parseInt((String) month.getSelectedItem())
Calendar c = Calendar.getInstance()
c.set(Calendar.YEAR, y)
c.set(Calendar.MONTH, m - 1)
int days = c.getActualMaximum(Calendar.DAY_OF_MONTH)
day.setModel(new DefaultComboBoxModel(getModel(1, days)))
}
/**
* get String array [start, end]
*/
private String[] getModel(int start, int end) {
String[] m = new String[end - start + 1]
for (int i = 0i <m.lengthi++) {
m[i] = String.valueOf(i + start)
}
return m
}
按照你的要求编写的Java swing 带界面的万年历代码如下
//日历import java.awt.BorderLayout
import java.awt.Color
import java.awt.Font
import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.util.Calendar
import javax.swing.BorderFactory
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel
public class CCI extends JFrame implements ActionListener{
JButton jb1=new JButton("<<")
JButton jb2=new JButton("<")
JButton jb3=new JButton(">")
JButton jb4=new JButton(">>")
JPanel jp1=new JPanel()
JPanel jp2=new JPanel()
JPanel jp3=new JPanel()
JPanel jp4=new JPanel()
JLabel jl1=new JLabel()
JLabel jl2=new JLabel()
JLabel[]jl=new JLabel[49]
String []week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}
Calendar c=Calendar.getInstance()
int year,month,day
int nowyear,nowmonth,nowday
CCI(){
super("简单日历")
nowyear=c.get(Calendar.YEAR)
nowmonth=c.get(Calendar.MONTH)+1
nowday=c.get(Calendar.DAY_OF_MONTH)
year=nowyear
month=nowmonth
day=nowday
String s=year+"年"+month+"月"
jl1.setForeground(Color.RED)
jl1.setFont(new Font(null,Font.BOLD,20))
jl1.setText(s)
jb1.addActionListener(this)
jb2.addActionListener(this)
jb3.addActionListener(this)
jb4.addActionListener(this)
jp1.add(jb1)jp1.add(jb2)jp1.add(jl1)jp1.add(jb3)jp1.add(jb4)
jp2.setLayout(null)
createMonthPanel()
jp2.add(jp3)
jl2.setFont(new Font(null,Font.BOLD,20))
jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日")
jp4.add(jl2)
add(jp1,BorderLayout.NORTH)
add(jp2,BorderLayout.CENTER)
add(jp4,BorderLayout.SOUTH)
setSize(500,500)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setLocationRelativeTo(null)
setVisible(true)
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb1){
year=year-1
String s=year+"年"+month+"月"
jl1.setText(s)
jp3.removeAll()
createMonthPanel()
jp3.validate()
}
if(ae.getSource()==jb2){
if(month==1){
year=year-1
month=12
}else{
month=month-1
}
String s=year+"年"+month+"月"
jl1.setText(s)
jp3.removeAll()
createMonthPanel()
jp3.validate()
}
if(ae.getSource()==jb3){
if(month==12){
year=year+1
month=1
}else{
month=month+1
}
String s=year+"年"+month+"月"
jl1.setText(s)
jp3.removeAll()
createMonthPanel()
jp3.validate()
}
if(ae.getSource()==jb4){
year=year+1
String s=year+"年"+month+"月"
jl1.setText(s)
jp3.removeAll()
createMonthPanel()
jp3.validate()
}
}
public static void main(String[] args) {
new CCI()
}
public int getMonthDays(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31
case 2:
if ((year%4==0&&year%100!=0)||year%400==0) {
return 29
} else {
return 28
}
default:
return 30
}
}
public void createMonthPanel(){
c.set(year, month-1, getMonthDays(year,month))
int weekOfMonth=c.get(Calendar.WEEK_OF_MONTH)
if(weekOfMonth==6){
jp3.setLayout(new GridLayout(7,7))
jp3.setBounds(50, 20, 420, 350)
}else{
jp3.setLayout(new GridLayout(6,7))
jp3.setBounds(50, 20, 420, 300)
}
jp3.setBorder(BorderFactory.createEtchedBorder())
for(int i=0i<7i++){
jl[i]=new JLabel(week[i],JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
c.set(year, month-1, 1)
int emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1
int daysOfMonth=getMonthDays(year,month)
for(int i=6+emptyFirsti>=7i--){
int intyear=year
int intmonth=month
if(intmonth==1){
intyear=intyear-1
intmonth=12
}else{
intmonth=intmonth-1
}
int intdays=getMonthDays(intyear,intmonth)
jl[i]=new JLabel((intdays+7-i)+"",JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
jl[i].setForeground(Color.GRAY)
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
for(int i=7+emptyFirsti<daysOfMonth+7+emptyFirsti++){
jl[i]=new JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
if((i+1)%7==0 || (i+1)%7==1){
jl[i].setForeground(Color.RED)
}else if((i-7-emptyFirst+1)==nowday&&month==nowmonth&&year==nowyear)
jl[i].setForeground(Color.BLUE)
else
jl[i].setForeground(Color.BLACK)
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
if(weekOfMonth==6)
for(int i=48i>=daysOfMonth+emptyFirst+7i--){
jl[i]=new JLabel((49-i)+"",JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
jl[i].setForeground(Color.GRAY)
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
else
for(int i=41i>=daysOfMonth+emptyFirst+7i--){
jl[i]=new JLabel((42-i)+"",JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
jl[i].setForeground(Color.GRAY)
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)