導航:首頁 > 編程知識 > 如何用編程的方法做窗口

如何用編程的方法做窗口

發布時間:2024-12-30 15:58:53

❶ 如何用java寫一個窗口,,還有怎麼弄到自己的程序里,最好有具體的小

//定義一個從JFrame繼承的類,就可以顯示一個窗口了
//在下面的例子的文本框中輸入一個姓名,上面的標簽會顯示一個歡迎語句
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Hi extends JFrame
{
public static void main(String[] args)
{
new Hi();
}
Hi()
{
super("歡迎");
final JLabel l=new JLabel();
final JTextField t=new JTextField(32);
t.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String txt=t.getText();
if(""!=txt)
{
l.setText("你好,"+txt+",歡迎你使用java編程!");
}
}
});
setLayout(new GridLayout(2,1));
add(l);
add(t);
setResizable(false);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}

❷ 急需一個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();
}
}
}

❸ 如何讓窗口

要讓窗口顯示或執行特定操作,通常需要通過編程語言或圖形用戶界面框架提供的介面來實現。

窗口在計算機程序中是一個重要的組成部分,它為用戶提供了一個與程序交互的界面。要讓窗口顯示或執行特定的操作,我們需要依賴於編程語言或特定的圖形用戶界面框架。這些工具和框架為我們提供了創建、管理和操作窗口的介面。

創建窗口:大多數GUI框架都提供了創建窗口的函數或方法。例如,在Python的Tkinter庫中,我們可以使用`Tk`函數來創建一個新的窗口。在創建窗口時,我們還可以指定窗口的標題、大小、位置等屬性。

操作窗口:一旦窗口被創建,我們就可以通過調用框架提供的方法來操作它。例如,我們可以顯示或隱藏窗口、調整其大小、移動其位置、更改其標題等。這些操作通常是通過調用窗口對象的方法來實現的。

響應事件:窗口還可以響應用戶的操作,如點擊按鈕、輸入文本等。為了實現這一點,我們需要為窗口或窗口中的組件注冊事件處理程序。當用戶執行特定操作時,這些程序將被調用,並可以執行相應的邏輯,如更新窗口的內容、顯示消息框等。

總之,要讓窗口顯示或執行特定操作,我們需要使用編程語言或GUI框架提供的介面來創建、管理和操作窗口。通過調用這些方法,我們可以實現窗口的顯示、隱藏、移動、調整大小等操作,並可以響應用戶的操作來執行相應的邏輯。

閱讀全文

與如何用編程的方法做窗口相關的資料

熱點內容
國外優秀購物網站 瀏覽:715
並發編程中哪些情況會發生死鎖 瀏覽:438
從小學編程的好處是什麼 瀏覽:118
xilinxucf文件 瀏覽:721
華為手機電腦微信版文件無法打開 瀏覽:304
廣州網路商城開發公司嗎 瀏覽:799
qq基本頭像 瀏覽:773
飢荒寫入配置文件 瀏覽:115
微信打文件 瀏覽:70
如何用分析雲做數據關聯 瀏覽:377
ios10如何關閉系統升級 瀏覽:811
數據太多計算總出錯怎麼回事 瀏覽:767
正常空氣流量數據多少 瀏覽:785
如何把數據導入標准曲線 瀏覽:378
ueditor寫入資料庫 瀏覽:102
iqooz3怎麼清除設置數據 瀏覽:478
alias視頻教程 瀏覽:774
win7沒有網路驅動器 瀏覽:58
配置文件怎麼下載 瀏覽:284
無盡之劍3太陽劍升級 瀏覽:291

友情鏈接