网站建设资讯

NEWS

网站建设资讯

java小窗口代码,java编写窗口

急需一个java编程实现的简单聊天窗口代码

import java.awt.*;

专注于为中小企业提供做网站、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业西湖免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

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("发送");

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("发送失败");

}

}

}

}

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); //文本域,第一个参数为行数,第二个参数为列数

ta.setEditable(false); //文本域只读

JScrollPane sp=new JScrollPane(ta); //滚动窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("发送");

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(); //获取文本框中的内容

if(!message.equals("")){

tf.setText(""); //清空文本框

ta.append("BB:"+message+"\n"); //添加到文本域并换行

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("发送失败!");

}

}

}

}

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();

}

}

}

求代码:利用Java做一个新的弹出窗口的小程序

重点地是窗体弹出类: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("点击弹出一个窗口");

jButton1.addActionListener(new Frame3_jButton1_actionAdapter(this));

this.getContentPane().add(jButton1, null);

}

public void jButton1_actionPerformed(ActionEvent e) {

this.dispose();//释放当前窗口

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("点我(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();

}

}

//将这三个文件分别编译后执行

第二个文件名是Frame1.java,用命令javac Frame1.java可以编译,命令java Frame1.class可以执行

Java编写一个简单的窗口程序

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("学生查询");

mi1 = new JMenuItem("确认");

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, "你点击了确定按钮");

}

});

mi2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

JOptionPane.showMessageDialog(null, "你点击了取消按钮");

}

});

} public static void main(String[] args) {

// TODO Auto-generated method stub

new MenuTest();

}}

用java做一个窗口

java做窗口的话,需要用swing技术,之后创建JFrame 等组件,即可完成窗口创建工作。

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("学生查询");

mi1 = new JMenuItem("确认");

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) {

// 具体实现代码根据实际要求填写

System.out.println("click");

JOptionPane.showMessageDialog(null, "你点击了确定按钮");

}

});

mi2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 具体实现代码根据实际要求填写

JOptionPane.showMessageDialog(null, "你点击了取消按钮");

}

});

} public static void main(String[] args) {

new MenuTest();//执行菜单创建

}}

java程序关闭窗口代码

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的关闭窗口代码

//fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//继承Frame的

fr.addWindowListener(new WindowAdapter() {    

public void windowClosing(WindowEvent e) {        

System.exit(0);//退出系统   

}

});

fr.setVisible(true);

}

public FirstFrame(String str) {

super(str);

}

}


网站名称:java小窗口代码,java编写窗口
当前链接:http://njwzjz.com/article/dsisgod.html