不知道是不是你說的窗口
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括山陰網(wǎng)站建設(shè)、山陰網(wǎng)站制作、山陰網(wǎng)頁制作以及山陰網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,山陰網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到山陰省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
使用javaswing JFrame設(shè)計(jì)窗口 + 布局就可實(shí)現(xiàn),,如下例(添加了詳細(xì)注釋):
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());//設(shè)置布局方式,BorderLayout東西南北中布局
b1 = new JButton("點(diǎn)擊");
b1.addActionListener(this);//為按鈕添加監(jiān)聽
c.add(b1, BorderLayout.SOUTH);//添加按鈕到c容器中,并分配在容器南(下)方
jta = new JTextArea();
c.add(jta, BorderLayout.CENTER);//添加文本區(qū)到c容器中,并分配在居中位置
this.setTitle("按鈕事件");//設(shè)置窗口標(biāo)題
this.setSize(300, 300);//設(shè)置窗體大小
this.setVisible(true);//窗體設(shè)置為顯示
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關(guān)閉窗體
//常用的一種關(guān)閉窗體的方法
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
//使用判斷按鈕名稱的方法觸發(fā)事件
if("點(diǎn)擊".equals(e.getActionCommand())) {
jta.setText("按鈕被點(diǎn)擊了!");
}
//也可以獲取對象名實(shí)現(xiàn)判斷
// if(e.getSource() == b1) {
// jta.setText("按鈕使用getSource方法被點(diǎn)擊了!");
// }
}
public static void main(String[] args) {
new JFrameTest();
}
}
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("發(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();
}
}
}
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);
}
}
方法一:
類 JFrame
javax.swing.JFrame
JFrame中的方法void setDefaultCloseOperation(int)可以設(shè)置
以下為改方法的用法:
setDefaultCloseOperation
public void setDefaultCloseOperation(int operation)設(shè)置用戶在此窗體上發(fā)起
"close" 時默認(rèn)執(zhí)行的操作。必須指定以下選項(xiàng)之一:
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定義):不執(zhí)行任何操作;要求程序在已注冊的
WindowListener 對象的 windowClosing 方法中處理該操作。
HIDE_ON_CLOSE(在 WindowConstants 中定義):調(diào)用任意已注冊的 WindowListener
對象后自動隱藏該窗體。
DISPOSE_ON_CLOSE(在 WindowConstants 中定義):調(diào)用任意已注冊 WindowListener
的對象后自動隱藏并釋放該窗體。
EXIT_ON_CLOSE(在 JFrame 中定義):使用 System exit
方法退出應(yīng)用程序。僅在應(yīng)用程序中使用。
默認(rèn)情況下,該值被設(shè)置為 HIDE_ON_CLOSE。更改此屬性的值將導(dǎo)致激發(fā)屬性更改事件,其屬性名稱為
"defaultCloseOperation"。
注:當(dāng) Java 虛擬機(jī) (VM) 中最后一個可顯示窗口被釋放后,虛擬機(jī)可能會終止
要實(shí)現(xiàn)你說的,應(yīng)該采用
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);
//設(shè)置關(guān)閉時什么也不做
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
//監(jiān)聽關(guān)閉按鈕的點(diǎn)擊操作
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();
}
}
當(dāng)前名稱:java教程窗口代碼 java編寫窗口程序
網(wǎng)頁地址:http://aaarwkj.com/article40/hhhgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、云服務(wù)器、網(wǎng)頁設(shè)計(jì)公司、虛擬主機(jī)、搜索引擎優(yōu)化、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)