资讯

精准传达 • 有效沟通

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

简单的Java窗口代码 java生成窗口

给段最简单的java代码 让我新手看一下

最简单的java代码肯定就是这个了,如下:

目前成都创新互联公司已为超过千家的企业提供了网站建设、域名、网页空间、网站运营、企业网站设计、鸡冠网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

public class MyFirstApp

{

public static void main(String[] args)

{

System.out.print("Hello world");

}

}

“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!

一个窗体,一个按钮,最简单的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做窗口的话,需要用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编程实现的简单聊天窗口代码

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用frame实现图中2个窗口 怎么写啊?

图片看起来很模糊,隐约看到需要一个登录窗口,那就分享一下以前练习的登录窗口demo吧。

先上效果图:

登录界面

源码如下:

AbsoluteLoginFrame.java

public class AbsoluteLoginFrame extends JFrame {

private static final int LOGIN_WIDTH = 600;

private static final int LOGIN_HEIGHT = 400;

private static final long serialVersionUID = -2381351968820980500L;

public AbsoluteLoginFrame(){

  //设置窗口标题

  setTitle("登录界面");

  //设置一个初始面板,填充整个窗口

  JPanel loginPanel = new JPanel();

  //设置背景颜色

  loginPanel.setBackground(new Color(204, 204, 204));//#CCC

  loginPanel.setLayout(null);

  JPanel centerPanel = new JPanel();

  centerPanel.setBackground(Color.WHITE);

  centerPanel.setBounds(114, 70, 360, 224);

  centerPanel.setLayout(null);

  JLabel jLabel = new JLabel("用户名:");

  jLabel.setOpaque(true);

  jLabel.setBackground(Color.YELLOW);

  jLabel.setBounds(60, 60, 54, 20);

  JLabel label = new JLabel("密    码:");

  label.setOpaque(true);

  label.setBackground(Color.CYAN);

  label.setBounds(60, 90, 54, 20);

  JTextField textField = new JTextField(15);

  textField.setBounds(130, 60, 166, 21);

  JPasswordField passwordField = new JPasswordField(15);

  passwordField.setBounds(130, 90, 166, 21);

  JButton jButton = new JButton("登录");

  jButton.setBounds(148, 120, 62, 28);

  centerPanel.add(jLabel);

  centerPanel.add(label);

  centerPanel.add(textField);

  centerPanel.add(jButton);

  centerPanel.add(passwordField);

  loginPanel.add(centerPanel);

  getContentPane().add(loginPanel);//将初始面板添加到窗口中

  setSize(LOGIN_WIDTH, LOGIN_HEIGHT);//设置窗口大小

  setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//设置窗口位置

  setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗口默认关闭方式

  setResizable(false);

  setVisible(true);

}

public static void main(String[] args) {

  new AbsoluteLoginFrame();

}

}

Screen.java

public class Screen {

private int width;

private int height;

public Screen(){

  Toolkit toolkit = Toolkit.getDefaultToolkit();

  Dimension screenSize = toolkit.getScreenSize();

  this.width = screenSize.width;

  this.height = screenSize.height;

}

public static Point getCenterPosition(int width, int height){

  Screen screen = new Screen();

  int x = (screen.getWidth() - width) / 2;

  int y = (screen.getHeight() - height) / 2;

  return new Point(x, y);

}

public int getWidth() {

  return width;

}

public void setWidth(int width) {

  this.width = width;

}

public int getHeight() {

  return height;

}

public void setHeight(int height) {

  this.height = height;

}

}


本文名称:简单的Java窗口代码 java生成窗口
当前URL:http://cdkjz.cn/article/hhjdih.html
多年建站经验

多一份参考,总有益处

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

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

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220