日期:2014-05-20  浏览次数:20783 次

关于自己做的小东西,有的地方不理解。。。
[code=Java][/code]import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

class QQFrame extends JFrame
{
public QQFrame()
{
setTitle( "QQ" );
setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
setLocation( 50, 50 );
buttonPanel = new JPanel();
panel = new JPanel();
add( buttonPanel, BorderLayout.WEST );
add( panel, BorderLayout.CENTER );
panel.add( textArea );
buttenPanel = new JPanel();
add( buttenPanel, BorderLayout.NORTH );
buttenPanel.add( jb );
Color = new JPanel();
Color.add( clock );
add( Color, BorderLayout.SOUTH );
}

public final static int DEFAULT_WIDTH = 300;
public final static int DEFAULT_HEIGHT = 300;

private JPanel buttonPanel;
private JPanel panel;
private JPanel buttenPanel;
private JPanel Color;

JButton jb = new JButton( "Connet" );
JLabel clock = new JLabel( "Clock" );

JTextArea textArea = new JTextArea( 5, 10 );

public void makeButton( String name, Color backgroundColor )
{
JButton button = new JButton( name );
buttonPanel.add( button );
buttonPanel.setLayout( new GridLayout( 3,1, 50, 50 ) );
ColorAction action = new ColorAction( backgroundColor );
button.addActionListener( action );
}

private class ColorAction implements ActionListener
{
private Color backgroundColor;

public ColorAction( Color c )
{
backgroundColor = c;
}

public void actionPerformed( ActionEvent event )
{
buttonPanel.setBackground( backgroundColor );
}
}
}

class WindowClose extends WindowAdapter
{
public void windowClosing( WindowEvent e )
{
System.out.println( "The windos will be closed in 3 sec " );
try 
{
Thread.sleep( 3000 );

catch (InterruptedException e1) 
{
e1.printStackTrace();
}
System.exit( 0 );
}
}

class MyThread extends Thread
{
private JLabel clock;

public MyThread( JLabel clock )
{
this.clock = clock;
}

public String getTime()
{
Calendar c = new GregorianCalendar();

String time = c.get( Calendar.YEAR ) + "-" + ( c.get( Calendar.MONTH ) + 1 ) + "-" + c.get( Calendar.DATE ) + " ";


int h = c.get( Calendar.HOUR_OF_DAY );
int m = c.get( Calendar.MINUTE );
int s = c.get( Calendar.SECOND );

String ph = h < 10 ? "0" : "";
String pm = m < 10 ? "0" : "";
String ps = s < 10 ? "0" : "";

time += ph + h + ":" + pm + m + ":" + ps + s;
return time;
}

public void run()
{
while( true )
{
clock.setText( this.getTime() );
try
{
Thread.sleep( 1000 );
}
catch( Exception ee )
{
ee.getStackTrace();
}
}
}
}

public class QQTest
{
public static void main( String[] args ) throws Exception
{
QQFrame frame = new QQFrame();
JLabel clock = new JLabel( "Clock" );
clock.setHorizontalAlignment( JLabel.CENTER );

Thread t = new MyThread( clock );