import java.awt.*;
專注于為中小企業(yè)提供做網(wǎng)站、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)西湖免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ClientDemo01 {
public static void main(String[] args){
JFrame f=new JFrame("AA");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(15,30);
ta.setEditable(false); //文本域只讀
JScrollPane sp=new JScrollPane(ta); //滾動窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("發(fā)送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
socket=new Socket("192.168.0.4",5000);
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread01 mt=new MyThread01(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener01(tf,ta,bos));
}
}
class ButtonActionListener01 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText();
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("AA:"+message+"\n"); //添加到文本域并換行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("發(fā)送失敗");
}
}
}
}
class MyThread01 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread01(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("BB:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
} import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ServerDemo01{
public static void main(String[] args){
JFrame f=new JFrame("BB");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(12,30); //文本域,第一個參數(shù)為行數(shù),第二個參數(shù)為列數(shù)
ta.setEditable(false); //文本域只讀
JScrollPane sp=new JScrollPane(ta); //滾動窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("發(fā)送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ServerSocket server=null;
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
server=new ServerSocket(5000);
//ta.append("等待AA連接...\n");
socket=server.accept();
//ta.append("AA已連接\n");
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread1 mt=new MyThread1(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener1(tf,ta,bos));
}
}
class ButtonActionListener1 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText(); //獲取文本框中的內(nèi)容
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("BB:"+message+"\n"); //添加到文本域并換行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("發(fā)送失?。?);
}
}
}
}
class MyThread1 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread1(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("AA:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
重點(diǎn)地是窗體彈出類:WindowsCenter.java
文件一父窗文件名Frame3.java
package untitled1;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame3 extends JFrame {
JButton jButton1 = new JButton();
public Frame3() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
setSize(400,400);
jButton1.setBounds(new Rectangle(86, 125, 186, 52));
jButton1.setText("點(diǎn)擊彈出一個窗口");
jButton1.addActionListener(new Frame3_jButton1_actionAdapter(this));
this.getContentPane().add(jButton1, null);
}
public void jButton1_actionPerformed(ActionEvent e) {
this.dispose();//釋放當(dāng)前窗口
WindowsCenter.Center(new Frame2());//彈出第三個窗口
}
public static void main (String[] args)
{
WindowsCenter.Center(new Frame3());//彈出主窗口
}
}
class Frame3_jButton1_actionAdapter implements ActionListener {
private Frame3 adaptee;
Frame3_jButton1_actionAdapter(Frame3 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
//要彈出的子窗口文件名:Frame2.java
package untitled1;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Frame2 extends JFrame
{
private JButton jButton1=new JButton();
public Frame2 ()
{
try {
jbInit();
}
catch(Exception exception) {
exception.printStackTrace();
}
this.setVisible(true);
}
private void jbInit () throws Exception
{
this.setBounds(300,180,400,300);
getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(127, 120, 139, 36));
jButton1.setMnemonic('C');
jButton1.setText("點(diǎn)我(C)");
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(jButton1);
}
public void jButton1_actionPerformed (ActionEvent e)
{
this.setVisible(false);
JFrame jf1=new JFrame("子窗口");
jf1.setBounds(100,50,800,600);
jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
jf1.setVisible(true);
}
}
//窗體居中彈出類文件名:WindowsCenter.java類
package untitled1;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.*;
//窗體居中類
public class WindowsCenter {
public WindowsCenter() {
}
public static void Center(JFrame frame){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.show();
}
}
//將這三個文件分別編譯后執(zhí)行
第二個文件名是Frame1.java,用命令javac Frame1.java可以編譯,命令java Frame1.class可以執(zhí)行
package inter.frame;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;public class MenuTest { /**
* @param args
*/
JFrame frame;
JMenuBar mb;
JMenu m;
JMenuItem mi1;
JMenuItem mi2; public MenuTest() {
initFrame();
initAction();
} public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("學(xué)生查詢");
mi1 = new JMenuItem("確認(rèn)");
mi2 = new JMenuItem("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("click");
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了確定按鈕");
}
});
mi2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了取消按鈕");
}
});
} public static void main(String[] args) {
// TODO Auto-generated method stub
new MenuTest();
}}
java做窗口的話,需要用swing技術(shù),之后創(chuàng)建JFrame 等組件,即可完成窗口創(chuàng)建工作。
package inter.frame;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;public class MenuTest { /**
* @param args
*/
JFrame frame; //定義一個窗口架構(gòu)
JMenuBar mb;//定義窗口的菜單工具欄
JMenu m; //定義菜單
JMenuItem mi1;//定義菜單的內(nèi)容
JMenuItem mi2; //定義菜單的內(nèi)容
public MenuTest() {
initFrame();
initAction();
}
public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("學(xué)生查詢");
mi1 = new JMenuItem("確認(rèn)");
mi2 = new JMenuItem("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300); //設(shè)置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置退出時關(guān)閉窗口
frame.setVisible(true);//設(shè)置窗口可見
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實現(xiàn)代碼根據(jù)實際要求填寫
System.out.println("click");
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了確定按鈕");
}
});
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實現(xiàn)代碼根據(jù)實際要求填寫
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了取消按鈕");
}
});
} public static void main(String[] args) {
new MenuTest();//執(zhí)行菜單創(chuàng)建
}}
import?java.applet.*;
import?java.awt.Color;
import?java.awt.Frame;
import?javax.swing.JFrame;
import?java.awt.event.*;
public?class?FirstFrame?extends?Frame?{
public?static?void?main(String?args[])?{
FirstFrame?fr?=?new?FirstFrame("First?contianer!");
fr.setSize(240,?240);
//繼承JFrame的關(guān)閉窗口代碼
//fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//繼承Frame的
fr.addWindowListener(new?WindowAdapter()?{????
public?void?windowClosing(WindowEvent?e)?{????????
System.exit(0);//退出系統(tǒng)???
}
});
fr.setVisible(true);
}
public?FirstFrame(String?str)?{
super(str);
}
}