- 小菜G的建站之路
-
不知道你会不会java
我是搞java的,给你一个java实现的8皇后代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class J_Calendar extends JFrame
{
private int year, month;
private String[] str = { "sun","mon","tue","wed","thu","fri","sat" };
private Container c = getContentPane();
private JPanel[] p = { new JPanel(), new JPanel() };
private JLabel[][] label = new JLabel[7][7];
private JButton[] button = { new JButton("Previece"), new JButton("Next") };
private JLabel jl = new JLabel( "Calendar By LingShao", JLabel.CENTER );
private JMenuBar menuBar = new JMenuBar();
private JMenu[] menu =
{
new JMenu( "Year" ),
new JMenu( "Month" ),
new JMenu( "Help" )
};
private JMenuItem about = new JMenuItem( "About.." );
private JMenuItem exit = new JMenuItem( "Exit" );
private JMenu[] menuYear = new JMenu[6];
private JMenu[] menuMonth = new JMenu[4];
private JRadioButtonMenuItem[] rbmiYear = new JRadioButtonMenuItem[120];
private JRadioButtonMenuItem[] rbmiMonth = new JRadioButtonMenuItem[12];
private ImageIcon[] iconDay = new ImageIcon[31];
private ImageIcon[] iconWeek = new ImageIcon[7];
private ImageIcon[] iconBack =
{
new ImageIcon("red.gif"),
new ImageIcon("yellow.gif")
};
public J_Calendar()
{
super( "Calendar By LingShao" );
//set Container and JPanel
c.setLayout( new FlowLayout(FlowLayout.CENTER) );
p[0].setLayout( new GridLayout(7,7) );
p[1].setLayout( new FlowLayout(FlowLayout.CENTER) );
for( int i=0; i<p.length; i++ )
c.add( p[i] );
//set ImageIcon
for( int i=0; i<iconDay.length; i++ )
iconDay[i] = new ImageIcon( i+1 + ".gif" );
for( int i=0; i<iconWeek.length; i++ )
iconWeek[i] = new ImageIcon( str[i] + ".gif" );
//set JLabel
for( int i=0; i<label.length; i++ )
for( int j=0; j<label[i].length; j++ )
{
if( i==0 )
label[i][j] = new JLabel( iconWeek[j] );
else
label[i][j] = new JLabel( iconBack[(i+j)%2] );
p[0].add( label[i][j] );
}
p[1].add( jl );
for( int i=0; i<button.length; i++ )
p[1].add( button[i] );
//set JMenu
for( int y=1980,t=20,i=0; i<menuYear.length; i++ )
menuYear[i] = new JMenu( i*t+y + " - " + (i*t+y+19) );
for( int i=0; i<menuMonth.length; i++ )
menuMonth[i] = new JMenu( "Season " + (i+1) );
for( int y=1980,i=0; i<rbmiYear.length; i++ )
rbmiYear[i] = new JRadioButtonMenuItem( y+i+"" );
for( int i=0; i<rbmiMonth.length; i++ )
rbmiMonth[i] = new JRadioButtonMenuItem( i+1+"" );
//add MENU
setJMenuBar( menuBar );
for( int i=0; i<menu.length; i++ )
menuBar.add( menu[i] );
for( int i=0; i<menuYear.length; i++ )
{
menu[0].add( menuYear[i] );
for( int t=20,j=i*t; j<i*t+t; j++ )
menuYear[i].add( rbmiYear[j] );
}
for( int i=0; i<menuMonth.length; i++ )
{
menu[1].add( menuMonth[i] );
for( int t=3,j=i*t; j<i*t+t; j++ )
menuMonth[i].add( rbmiMonth[j] );
}
menu[2].add( about );
about.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog(
null, "Calendar By LingShao ZJUT" );
}
} );
menu[2].addSeparator();
menu[2].add( exit );
exit.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
System.exit( 0 );
}
} );
//add ButtonGroup
ButtonGroup radioGroupYear = new ButtonGroup();
for( int i=0; i<rbmiYear.length; i++ )
{
radioGroupYear.add( rbmiYear[i] );
rbmiYear[i].setSelected( false );
}
ButtonGroup radioGroupMonth = new ButtonGroup();
for( int i=0; i<rbmiMonth.length; i++ )
{
radioGroupMonth.add( rbmiMonth[i] );
rbmiMonth[i].setSelected( false );
}
//set JButton ActionListener
button[0].addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
if(year==0||month==0||year==1980&&month==1)
{
JOptionPane.showMessageDialog(
null, "NoNo !! Date Limited !!" );
return;
}
if( month==1 )
{
month = 12;
year--;
}
else month--;
rbmiYear[year-1980].setSelected( true );
rbmiMonth[month-1].setSelected( true );
}
} );
button[1].addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
if( year==0||month==0||year==2099&&month==12 )
{
JOptionPane.showMessageDialog(
null, "NoNo !! Date Limited !!");
return;
}
if( month==12 )
{
month = 1;
year++;
}
else month++;
rbmiYear[year-1980].setSelected( true );
rbmiMonth[month-1].setSelected( true );
}
} );
//add JRadioButtonMenuItem ItemListener
for( int i=0; i<rbmiYear.length; i++ )
rbmiYear[i].addItemListener( new ItemListener()
{
public void itemStateChanged( ItemEvent event )
{
for( int j=0; j<rbmiYear.length; j++ )
if( event.getSource()==rbmiYear[j] )
{
if( rbmiYear[j].isSelected() )
{
year = j+1980;
if( month!=0 )
display();
}
return;
}
}
} );
for( int i=0; i<rbmiMonth.length; i++ )
rbmiMonth[i].addItemListener( new ItemListener()
{
public void itemStateChanged( ItemEvent event )
{
for( int j=0; j<rbmiMonth.length; j++ )
{
if( event.getSource()==rbmiMonth[j] )
{
if( rbmiMonth[j].isSelected() )
{
month = j+1;
if( year!=0 )
display();
}
return;
}
}
}
} );
setSize( 300, 380 );
setResizable( false );
setVisible( true );
}
//判断y年是否为闰年
public boolean isLeapyear( int y )
{
return (y%400==0||y%4==0&&y%100!=0);
}
//返回year年month月1日离公元1980年1月1日头尾相隔多少天,此为判断星期的依据
public int getDayOfWeek()
{
int day = 0;
for( int i=1980; i<year; i++ )
day = (day+(isLeapyear(i)?366:365))%7;
switch( month )
{
case 12: day += 30;
case 11: day += 31;
case 10: day += 30;
case 9: day += 31;
case 8: day += 31;
case 7: day += 30;
case 6: day += 31;
case 5: day += 30;
case 4: day += 31;
case 3: day += isLeapyear(year)?29:28;
case 2: day += 31;
case 1: day += 1;
default: break;
}
return day%7;
}
//根据getDayOfWeek()函数返回的整数显示此月份的日历表
public void display()
{
jl.setText( "Year:" + year + " Month:" + month );
int firstDay, lastDay, week = (1+getDayOfWeek())%7;
for( firstDay=0; firstDay<week; firstDay++ )
label[1][firstDay].setIcon( null );
switch(month)
{
case 12:case 10:case 8:case 7:case 5:case 3:case 1:lastDay=31;break;
case 11:case 9:case 6:case 4:lastDay=30;break;
case 2:lastDay=isLeapyear(year)?29:28;break;
default:lastDay=0;break;
}
for( int t=0,i=1; i<label.length; i++ )
for( int j=(i==1?firstDay:0); j<label[i].length; j++,t++ )
if( t<lastDay )
label[i][j].setIcon( iconDay[t] );
else
label[i][j].setIcon( null );
}
public static void main( String[] args )
{
J_Calendar calendar = new J_Calendar();
calendar.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
- 大牌网络
-
问题不清楚!