资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java教程窗口代码 java编写窗口程序

Java中怎么新建窗口?我是新手麻烦代码中主要语句解释一下

不知道是不是你说的窗口

创新互联服务项目包括山阴网站建设、山阴网站制作、山阴网页制作以及山阴网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,山阴网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到山阴省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

使用javaswing JFrame设计窗口 + 布局就可实现,,如下例(添加了详细注释):

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextArea;

public class JFrameTest extends JFrame implements ActionListener {

private static final long serialVersionUID = -2829899643559384548L;

private JButton b1 = null;//按钮

private JTextArea jta = null;//文本

public JFrameTest() {

Container c = this.getContentPane();

c.setLayout(new BorderLayout());//设置布局方式,BorderLayout东西南北中布局

b1 = new JButton("点击");

b1.addActionListener(this);//为按钮添加监听

c.add(b1, BorderLayout.SOUTH);//添加按钮到c容器中,并分配在容器南(下)方

jta = new JTextArea();

c.add(jta, BorderLayout.CENTER);//添加文本区到c容器中,并分配在居中位置

this.setTitle("按钮事件");//设置窗口标题

this.setSize(300, 300);//设置窗体大小

this.setVisible(true);//窗体设置为显示

// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体

//常用的一种关闭窗体的方法

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e) {

//使用判断按钮名称的方法触发事件

if("点击".equals(e.getActionCommand())) {

jta.setText("按钮被点击了!");

}

//也可以获取对象名实现判断

// if(e.getSource() == b1) {

// jta.setText("按钮使用getSource方法被点击了!");

// }

}

public static void main(String[] args) {

new JFrameTest();

}

}

急需一个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程序关闭窗口代码

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

方法一:

类 JFrame

javax.swing.JFrame

JFrame中的方法void setDefaultCloseOperation(int)可以设置

以下为改方法的用法:

setDefaultCloseOperation

public void setDefaultCloseOperation(int operation)设置用户在此窗体上发起

"close" 时默认执行的操作。必须指定以下选项之一:

DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不执行任何操作;要求程序在已注册的

WindowListener 对象的 windowClosing 方法中处理该操作。

HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener

对象后自动隐藏该窗体。

DISPOSE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册 WindowListener

的对象后自动隐藏并释放该窗体。

EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit

方法退出应用程序。仅在应用程序中使用。

默认情况下,该值被设置为 HIDE_ON_CLOSE。更改此属性的值将导致激发属性更改事件,其属性名称为

"defaultCloseOperation"。

注:当 Java 虚拟机 (VM) 中最后一个可显示窗口被释放后,虚拟机可能会终止

要实现你说的,应该采用

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

方法二:

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class Test extends JFrame {

public Test(){

this.setTitle("title");

this.setSize(300,200);

this.setLocation(100,100);

//设置关闭时什么也不做

this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

//监听关闭按钮的点击操作

this.addWindowListener(new WindowAdapter(){

//new 一个WindowAdapter 类 重写windowClosing方法

//WindowAdapter是个适配器类 具体看jdk的帮助文档

public void windowClosing(WindowEvent e) {

//这里写对话框

if(JOptionPane.showConfirmDialog(null,

"退出","提

示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){

System.exit(0);

}

}

});

this.setVisible(true);

}

public static void main(String[] args) {

new Test();

}

}


分享题目:java教程窗口代码 java编写窗口程序
网页URL:http://cdkjz.cn/article/hhhgho.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220