网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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怎么写代码使一个窗口打开另一个窗口

1、首先,我们需要在代码中导入相应的包,以便能够使用 JFrame 类。然后,新建一个窗口类继承自 JFrame 类。

2、在窗口类中创建一个初始化方法,我们需要在该方法中初始化窗口类对象,并将其显示出来。

3、对窗口对象进行初始化时,我们先设置好窗口的标题。

4、再设置窗口的大小,参数分别为窗口的长和宽,单位是像素。

5、接着设置窗口左上角的坐标位置,以确定窗口的位置。参数分别为窗口左上角顶点的 x 坐标和 y 坐标。

6、最后,调用 setVisible 方法将窗口显示出来。参数为 true 表示显示,为 false 表示隐藏。

7、窗口类写好后,我们在 main 方法中创建一个窗口类对象,然后调用该对象的初始化方法就可以将窗口显示出来了。

用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半屏窗口代码 java窗口代码大全
网页网址:http://bjjierui.cn/article/dddiogc.html

其他资讯