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 );
t.start();
frame.makeButton( "yellow", Color.YELLOW );
frame.makeButton( "blue", Color.blue );
frame.makeButton( "red", Color.red );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
WindowListener listener = new WindowClose();
frame.addWindowListener( listener );
}
}
有几点疑问1.我想在下部显示时间为什么显示不了捏?2.左边3个按钮是想改变文本域的背景的。。但是只会改背景。该怎么办捏3.想在关闭窗口的时候有提示但是做不出来只能用线程自己等待3秒退出4.做的事一个QQ聊天的程序。。但是I/O流还用不熟做到这里不会了5.上面的connet按钮想要实现点击的时候弹出对话框,输入名字和密码。还是不太会6.文本域好小啊做刚合适的时候。。在意放大窗口就又小了苦恼。。好郁闷哇学了JAVA只有16天只能做出来这么一点东西希望大家帮帮我,给我点思路~小女子这厢有礼了
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 );
t.start();
frame.makeButton( "yellow", Color.YELLOW );
frame.makeButton( "blue", Color.blue );
frame.makeButton( "red", Color.red );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
WindowListener listener = new WindowClose();
frame.addWindowListener( listener );
}
}
有几点疑问1.我想在下部显示时间为什么显示不了捏?2.左边3个按钮是想改变文本域的背景的。。但是只会改背景。该怎么办捏3.想在关闭窗口的时候有提示但是做不出来只能用线程自己等待3秒退出4.做的事一个QQ聊天的程序。。但是I/O流还用不熟做到这里不会了5.上面的connet按钮想要实现点击的时候弹出对话框,输入名字和密码。还是不太会6.文本域好小啊做刚合适的时候。。在意放大窗口就又小了苦恼。。好郁闷哇学了JAVA只有16天只能做出来这么一点东西希望大家帮帮我,给我点思路~小女子这厢有礼了
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 );
t.start();
frame.makeButton( "yellow", Color.YELLOW );
frame.makeButton( "blue", Color.blue );
frame.makeButton( "red", Color.red );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
WindowListener listener = new WindowClose();
frame.addWindowListener( listener );
}
}上面程序显示的不好,重新发次
能写这个已经很不错了,不过写程序之前先设计一下结构,对于QQ那样的应用,肯定不是一个类能搞定的,首先根据MVC模式来分开,再做各个部分。通讯部分你可以参考下jabber协议的几个实现。这个就可以不用自己写了。
我用muse写过一个IM工具,实现起来还是比较简单的。
可以用SWT或Netbeans自带的FreeDesign的Layout
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;class QQFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 3370114939072183456L; public QQFrame(JLabel clock) {
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);
jb.addActionListener(new ConnectAction(this));
Clock = new JPanel();
Clock.add(clock);
add(Clock, 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 Clock; JButton jb = new JButton("Connet");
// JLabel clock = new JLabel( "Clock" ); JTextArea textArea = new JTextArea(10, 15); public void makeButton(String name, Color backgroundColor) {
JButton button = new JButton(name);
buttonPanel.add(button);
buttonPanel.setLayout(new GridLayout(3, 1, 50, 50));
button.addActionListener(new ColorAction(backgroundColor));
} private class ConnectAction implements ActionListener {
private JFrame frame; ConnectAction(JFrame frame) {
this.frame = frame;
} public void actionPerformed(ActionEvent event) {
LoginFrame login = new LoginFrame();
login.setVisible(true);
frame.setEnabled(false); login.addWindowListener(new WindowEv()); } class WindowEv extends WindowAdapter {
public void windowClosing(WindowEvent e) {
frame.setEnabled(true);
//frame.setFocusable(true);
frame.setVisible(true);
}
}
} private class ColorAction implements ActionListener {
private Color backgroundColor; public ColorAction(Color c) {
backgroundColor = c;
} public void actionPerformed(ActionEvent event) {
textArea.setBackground(backgroundColor);
}
} private class LoginFrame extends JFrame { /**
*
*/
private static final long serialVersionUID = 8039757979078003161L;
private JTextField user = new JTextField("", 10);
private JPasswordField psw = new JPasswordField("", 10);
private JButton loginBtn = new JButton("Login");
LoginFrame() {
setTitle("QQ");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocation(200, 200);
JPanel userPanel = new JPanel();
JPanel pswPanel = new JPanel();
add(userPanel, BorderLayout.NORTH);
add(pswPanel, BorderLayout.CENTER);
JLabel uL = new JLabel("user");
userPanel.add(uL);
userPanel.add(user);
JLabel pL = new JLabel("password");
pswPanel.add(pL);
psw.setEchoChar('#');
pswPanel.add(psw);
add(loginBtn, BorderLayout.SOUTH); loginBtn.addActionListener(new LoginsAction(this)); } class LoginsAction implements ActionListener { JFrame frame; LoginsAction(JFrame f) {
this.frame = f;
} @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (user.getText().trim().equalsIgnoreCase("aaa")
&& new String(psw.getPassword()).equals("aaa")) {
frame.dispose();
WindowEvent we = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING); frame.dispatchEvent(we);
} else {
JOptionPane.showMessageDialog(null, "error");
}
} }
}
}class WindowClose extends WindowAdapter {
public void windowClosing(WindowEvent e) {
JOptionPane.showConfirmDialog(null, "Are u sure to quit?");
}
}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 { JLabel clock = new JLabel("Clock");
clock.setHorizontalAlignment(JLabel.CENTER);
QQFrame frame = new QQFrame(clock);
Thread t = new MyThread(clock);
t.start(); frame.makeButton("yellow", Color.YELLOW);
frame.makeButton("blue", Color.blue);
frame.makeButton("red", Color.red); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); WindowListener listener = new WindowClose();
frame.addWindowListener(listener);
}
}