最簡(jiǎn)單的java代碼肯定就是這個(gè)了,如下:
目前成都創(chuàng)新互聯(lián)公司已為超過(guò)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、網(wǎng)站運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、雞冠網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是應(yīng)該是所有學(xué)java的新手看的第一個(gè)代碼了。如果是零基礎(chǔ)的新手朋友們可以來(lái)我們的java實(shí)驗(yàn)班試聽(tīng),有免費(fèi)的試聽(tīng)課程幫助學(xué)習(xí)java必備基礎(chǔ)知識(shí),有助教老師為零基礎(chǔ)的人提供個(gè)人學(xué)習(xí)方案,學(xué)習(xí)完成后有考評(píng)團(tuán)進(jìn)行專(zhuān)業(yè)測(cè)試,幫助測(cè)評(píng)學(xué)員是否適合繼續(xù)學(xué)習(xí)java,15天內(nèi)免費(fèi)幫助來(lái)報(bào)名體驗(yàn)實(shí)驗(yàn)班的新手快速入門(mén)java,更好的學(xué)習(xí)java!
public?class?Demo?extends?JFrame
{
JButton?jb;?//一個(gè)按鈕
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做窗口的話(huà),需要用swing技術(shù),之后創(chuàng)建JFrame 等組件,即可完成窗口創(chuàng)建工作。
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; //定義一個(gè)窗口架構(gòu)
JMenuBar mb;//定義窗口的菜單工具欄
JMenu m; //定義菜單
JMenuItem mi1;//定義菜單的內(nèi)容
JMenuItem mi2; //定義菜單的內(nèi)容
public MenuTest() {
initFrame();
initAction();
}
public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("學(xué)生查詢(xún)");
mi1 = new JMenuItem("確認(rèn)");
mi2 = new JMenuItem("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300); //設(shè)置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置退出時(shí)關(guān)閉窗口
frame.setVisible(true);//設(shè)置窗口可見(jiàn)
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實(shí)現(xiàn)代碼根據(jù)實(shí)際要求填寫(xiě)
System.out.println("click");
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了確定按鈕");
}
});
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實(shí)現(xiàn)代碼根據(jù)實(shí)際要求填寫(xiě)
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了取消按鈕");
}
});
} public static void main(String[] args) {
new MenuTest();//執(zhí)行菜單創(chuàng)建
}}
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); //滾動(dòng)窗格
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); //文本域,第一個(gè)參數(shù)為行數(shù),第二個(gè)參數(shù)為列數(shù)
ta.setEditable(false); //文本域只讀
JScrollPane sp=new JScrollPane(ta); //滾動(dòng)窗格
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();
}
}
}
圖片看起來(lái)很模糊,隱約看到需要一個(gè)登錄窗口,那就分享一下以前練習(xí)的登錄窗口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(){
? //設(shè)置窗口標(biāo)題
? setTitle("登錄界面");
? //設(shè)置一個(gè)初始面板,填充整個(gè)窗口
? JPanel loginPanel = new JPanel();
? //設(shè)置背景顏色
? 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("用戶(hù)名:");
? 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);//設(shè)置窗口大小
? setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//設(shè)置窗口位置
? setDefaultCloseOperation(EXIT_ON_CLOSE);//設(shè)置窗口默認(rèn)關(guān)閉方式
? 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;
}
}