
正文
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); //文本域java窗体常用代码,第一个参数为行数java窗体常用代码,第二个参数为列数
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(); //获取文本框中java窗体常用代码的内容
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("BB:"+message+"\n"); //添加到文本域并换行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("发送失败java窗体常用代码!");
}
}
}
}
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();
}
}
}
相关问答
Q1: 急求JAVA简易计算器窗体设计代码
窗体写好java窗体常用代码了java窗体常用代码,运算java窗体常用代码你自己写
import java.awt.Button;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SimpleCalculator {
private JFrame f = new JFrame();
private JPanel firstPanel = new JPanel();
private JLabel firstNumLabel = new JLabel("第一个数字");
private JTextField number1 = new JTextField();
private JPanel secondPanel = new JPanel();
private Button add = new Button("+");
private Button mul = new Button("*");
private Button clear = new Button("清除");
private JPanel thirdPanel = new JPanel();
private JLabel resultLabel = new JLabel("结果为");
private JTextField result = new JTextField();
public SimpleCalculator(){
firstPanel.setLayout(new GridLayout(1, 2));
firstPanel.add(firstNumLabel);
firstPanel.add(number1);
secondPanel.setLayout(new GridLayout(1, 3));
secondPanel.add(add);
secondPanel.add(mul);
secondPanel.add(clear);
thirdPanel.setLayout(new GridLayout(1, 2));
thirdPanel.add(resultLabel);
thirdPanel.add(result);
f.add(new JLabel("简易计算器"));
f.add(firstPanel);
f.add(secondPanel);
f.add(thirdPanel);
f.setLayout(new GridLayout(4, 1));
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new SimpleCalculator();
}
}
Q2: java两个窗体之间怎么传递参数? 请相信解释以下例子的代码,最好尽量都注释一下,谢谢。
在第一个窗体类里直接new 一个窗体对象就行了,直接引用不存在传递
图中只是用三方工具类传了一下
display (String s)方法直接写到第一个类里也是可以的
Q3: JAVA怎么写代码使一个窗口打开另一个窗口
1、首先,我们需要在代码中导入相应java窗体常用代码的包,以便能够使用 JFrame 类。然后,新建一个窗口类继承自 JFrame 类。
2、在窗口类中创建一个初始化方法,我们需要在该方法中初始化窗口类对象,并将其显示出来。
3、对窗口对象进行初始化时,我们先设置好窗口的标题。
4、再设置窗口的大小,参数分别为窗口的长和宽,单位是像素。
5、接着设置窗口左上角的坐标位置,以确定窗口的位置。参数分别为窗口左上角顶点的 x 坐标和 y 坐标。
6、最后,调用 setVisible 方法将窗口显示出来。参数为 true 表示显示,为 false 表示隐藏。
7、窗口类写好后,我们在 main 方法中创建一个窗口类对象,然后调用该对象的初始化方法就可以将窗口显示出来java窗体常用代码了。
Q4: 求java窗体代码,可以显示默认长文本,代码格式最好规范可以直接使用。
importjava.awt.*;importjava.awt.event.*;importjava.awt.geom.*;importjava.util.*;importjavax.swing.*;/***多线程java窗体常用代码,小球演示.打开Windows任务管理器,可看到线程变化。可搜索到,run()方法/.start()**du:程序技巧体会java窗体常用代码:所谓产生一个小球,即是new其类对象,其属性携带画小球java窗体常用代码的坐标、颜色、所在容器等参数。**一个类,属性用来作为参数容器用,方法.完成功能。**///运行类publicclassBouncePress{//publicstaticvoidmain(String[]args){JFrameframe=newBouncePressFrame();//生成窗口。执行构造。-----业务逻辑。frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//similarto//window//listenerframe.show();}}classBouncePressFrameextendsJFrame{privateBallPressCanvascanvas;publicBouncePressFrame(){setSize(600,500);//窗口大小setTitle("BounceBall");ContainercontentPane=getContentPane();//Swing的窗口不能直接放入东西,只能在其上的ContentPane上放。canvas=newBallPressCanvas();//生成一个新面板。-----canvascontentPane.add(canvas,BorderLayout.CENTER);//窗口中心加入该面板。JPanelbuttonPanel=newJPanel();//再生成一个新面板。----buttonPanel//调用本类方法addButton。addButton(buttonPanel,"Start",//生成一个按钮"Start"---加入面板buttonPanelnewActionListener(){//|------按钮绑上action监听器。publicvoidactionPerformed(ActionEventevt){//|小球容器对象的addBall(Thread.NORM_PRIORITY-4,Color.black);//事件处理时,执行---addBall()方法。---产生小球(参数对象)---加入List中---开始画球。}});//按一次,addBall()一次---产生一个新小球---加入List中---开始画此新小球。//---画球线程BallPressThread的run()---小球(参数对象).move()---每次画时,先移动,再判断,再画。//---BallPressCanvas类的canvas对象.paint()---自动调BallPressCanvas类的paintComponent(Graphics//g)方法。//---该方法,从List中循环取出所有小球,第i个球,---调该小球BallPress类//.draw()方法---调Graphics2D方法画出小球。--使用color/addButton(buttonPanel,"Express",newActionListener(){publicvoidactionPerformed(ActionEventevt){addBall(Thread.NORM_PRIORITY+2,Color.red);}});addButton(buttonPanel,"Close",newActionListener(){publicvoidactionPerformed(ActionEventevt){System.exit(0);}});contentPane.add(buttonPanel,BorderLayout.SOUTH);}publicvoidaddButton(Containerc,Stringtitle,ActionListenerlistener){JButtonbutton=newJButton(title);//生成一个按钮。c.add(button);//加入容器中。button.addActionListener(listener);//按钮绑上action监听器。}/**主要业务方法。*/publicvoidaddBall(intpriority,Colorcolor){//生成小球(参数对象)BallPressb=newBallPress(canvas,color);//生成BallPress对象,携带、初始化//画Ball形小球,所需参数:所在容器组件,所需color--black/red.//小球加入List中。canvas.add(b);//面板canvas的ArrayList中加入BallPress对象。BallPressThreadthread=newBallPressThread(b);//生成画小球的线程类BallPressThread对象。传入BallPress对象(携带java窗体常用代码了画球所需//容器、color参数)。thread.setPriority(priority);thread.start();//callrun(),ballstarttomove//画球线程开始。---BallPressThread的run()---小球(参数对象).move()---先移动,再画。canvas.paint---BallPressCanvas类的}}//画球的线程类。classBallPressThreadextendsThread{privateBallPressb;publicBallPressThread(BallPressaBall){b=aBall;}//画球开始。publicvoidrun(){try{for(inti=1;i自动绘制面板,且自动调paintComponent(Graphics//g)方法,---重写该方法,绘制面板(及其上组件)。//作用2)该类对象属性ArrayListballs---兼作小球(参数对象)的容器。classBallPressCanvasextendsJPanel{privateArrayListballs=newArrayList();publicvoidadd(BallPressb){balls.add(b);//向ArrayList中添加球。当按下按钮,添加多个球时,都保存在这个List中。}//重写java窗体常用代码了javax.swing.JComponent的paintComponent()方法。//paint()方法自动调用该方法。publicvoidpaintComponent(Graphicsg){super.paintComponent(g);Graphics2Dg2=(Graphics2D)g;for(inti=0;i=canvas.getWidth()){//小球右边已经到画板右边。x=canvas.getWidth()-15;dx=-dx;//开始反向运动。}if(y=canvas.getHeight()){//小球已到画板顶。y=canvas.getHeight()-15;dy=-dy;}canvas.paint(canvas.getGraphics());//画出面板对象canvas----(及其上所有组件)////.paint()方法,自动调用}}/*importjava.awt.*;importjava.awt.event.*;importjava.awt.geom.*;importjava.util.*;importjavax.swing.*;*//***单线程,小球演示搜索不到,run()方法/.start()*//*publicclassBounce{publicstaticvoidmain(String[]args){JFrameframe=newBounceFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//similarto//window//listenerframe.show();}}不懂的再问啊。。。
Q5: 一个窗体,一个按钮,最简单的java代码怎写?
public class Demo extends JFrame
{
JButton jb; //一个按钮
public static void main(String []args){
new Demo();
}
public Demo()
{
this.setLayout(new FlowLayout());
jb=new JButton("按扭");
this.add(jb);
this.setSize(400,300);
this.setVisible(true);
this.setLocation(500, 200);
}
}
java窗体常用代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java窗体应用、java窗体常用代码的信息别忘了在本站进行查找喔。






