java圖型代碼練習(xí) java繪圖程序代碼

JAVA的圖形用戶界面代碼

package hao;

目前創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、祁門網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.io.File;

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextPane;

import javax.swing.text.BadLocationException;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyledDocument;

public class ChatPanel extends JPanel {

private static final long serialVersionUID = 1L;

JButton send,record,saveRecord,image;

JTextArea inputArea;

JTextPane text;//注意用法****************************************************************************

JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null;

public StyledDocument doc = null; JScrollPane scrollPane;JPanel textChat;

JButton music;

public ChatPanel() {

setLayout(new BorderLayout());

text = new JTextPane();

text.setEditable(false);

doc = text.getStyledDocument();//跟蹤文本和圖片寫到該區(qū)域的位置*************************************

scrollPane = new JScrollPane(text);

//注意下面對(duì)JComboBox的巧用***********************************************************************

String[] str_name = { "宋體", "黑體", "Dialog", "Gulim" };

String[] str_Size = { "12", "14", "18", "22", "30", "40" };

String[] str_Style = { "常規(guī)", "斜體", "粗體", "粗斜體" };

String[] str_Color = { "黑色", "紅色", "藍(lán)色", "黃色", "綠色" };

String[] str_BackColor = { "無(wú)色", "灰色", "淡紅", "淡藍(lán)", "淡黃", "淡綠" };

fontName = new JComboBox(str_name);

fontSize = new JComboBox(str_Size);

fontStyle = new JComboBox(str_Style);

fontColor = new JComboBox(str_Color);

fontBackColor = new JComboBox(str_BackColor);

fontName.setBackground(new Color(255,153,255));

fontSize.setBackground(new Color(255,153,255));

fontStyle.setBackground(new Color(255,153,255));

fontColor.setBackground(new Color(255,153,255));

fontBackColor.setBackground(new Color(255,153,255));

Box box = Box.createVerticalBox();//創(chuàng)建一個(gè)可以容納多個(gè)Box組件的Box*******************************

Box box_1 = Box.createHorizontalBox();

Box box_2 = Box.createHorizontalBox();

Box box_4 = Box.createHorizontalBox();

box.add(box_1);

box.add(box_2);

box.add(box_4);

JLabel b1= new JLabel("字體~~"), b2 = new JLabel("樣式~~"),b3 = new JLabel("字號(hào)~~"),b4 = new JLabel("顏色~~"),b5 = new JLabel("背景~~");

b1.setBackground(new Color(255,153,255));

b2.setBackground(new Color(255,153,255));

b3.setBackground(new Color(255,153,255));

b4.setBackground(new Color(255,153,255));

b5.setBackground(new Color(255,153,255));

box_1.add(b1);

box_1.add(fontName);

box_1.add(Box.createHorizontalStrut(8));

box_1.add(b2);

box_1.add(fontStyle);

box_1.add(Box.createHorizontalStrut(8));

box_1.add(b3);

box_1.add(fontSize);

box_2.add(Box.createHorizontalStrut(8));

box_2.add(b4);

box_2.add(fontColor);

box_2.add(Box.createHorizontalStrut(8));

box_4.add(b5);

box_4.add(fontBackColor);

textChat = new JPanel();

textChat.setLayout(new BorderLayout());

textChat.setBackground(new Color(255,153,255));

inputArea = new JTextArea(3, 20);

inputArea.setLineWrap(true); //設(shè)置文本區(qū)的換行策略。88888*********************************

send = new JButton("發(fā)送");

record=new JButton("顯示記錄");

saveRecord=new JButton("儲(chǔ)存記錄");

image=new JButton("表情");

send.setBackground(new Color(255,153,255));

record.setBackground(new Color(255,153,255));

saveRecord.setBackground(new Color(255,153,255));

image.setBackground(new Color(255,153,255));

Box box_3 = Box.createHorizontalBox();

box_3.add(send); box_3.add(Box.createHorizontalStrut(8));//設(shè)置按鈕間距*************************888

box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //設(shè)置按鈕間距*************************888

box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//設(shè)置按鈕間距*************************888

box_3.add(image);

box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設(shè)置Box的邊框線********************

box_3.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));

textChat.add(box,BorderLayout.NORTH);

textChat.add(inputArea,BorderLayout.CENTER);

textChat.add(box_3, BorderLayout.SOUTH);

inputArea.requestFocus(true);

inputArea.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設(shè)置輸入窗口邊框線*******************

text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//設(shè)置輸入窗口邊框線*******************

JPanel audioPanel = new JPanel();//最上面的邊框************************************************************************

audioPanel.setBackground(new Color(255,153,255));

audioPanel.setLayout(new GridLayout(1,1));

music = new JButton("想聽就聽");

music.setPreferredSize(new Dimension(320,50));

music.setBorder(BorderFactory.createLineBorder(Color.BLACK,10));//設(shè)置輸入窗口邊框線*******************

audioPanel.add(music);

add(audioPanel, BorderLayout.NORTH);

add(scrollPane,BorderLayout.CENTER);

add(textChat, BorderLayout.SOUTH);

}

void insertIcon(ImageIcon image) {

text.setCaretPosition(doc.getLength());

text.insertIcon(image);

insert(new MessageStyle());//?????????????????????????????????????????????????????????????????????????????/

}

public void insert(MessageStyle attrib) {

try {

doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());//寫完后接著換行************

} catch (BadLocationException e) {

e.printStackTrace();

}

}

public MessageStyle getMessageStyle(String line) {

MessageStyle att = new MessageStyle();

att.setText(line);

att.setName((String) fontName.getSelectedItem());

att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));

String temp_style = (String) fontStyle.getSelectedItem();

if (temp_style.equals("常規(guī)")) {

att.setStyle(MessageStyle.GENERAL);

}

else if (temp_style.equals("粗體")) {

att.setStyle(MessageStyle.BOLD);

}

else if (temp_style.equals("斜體")) {

att.setStyle(MessageStyle.ITALIC);

}

else if (temp_style.equals("粗斜體")) {

att.setStyle(MessageStyle.BOLD_ITALIC);

}

String temp_color = (String) fontColor.getSelectedItem();

if (temp_color.equals("黑色")) {

att.setColor(new Color(0, 0, 0));

}

else if (temp_color.equals("紅色")) {

att.setColor(new Color(255, 0, 0));

}

else if (temp_color.equals("藍(lán)色")) {

att.setColor(new Color(0, 0, 255));

}

else if (temp_color.equals("黃色")) {

att.setColor(new Color(255, 255, 0));

}

else if (temp_color.equals("綠色")) {

att.setColor(new Color(0, 255, 0));

}

String temp_backColor = (String) fontBackColor.getSelectedItem();

if (!temp_backColor.equals("無(wú)色")) {

if (temp_backColor.equals("灰色")) {

att.setBackColor(new Color(200, 200, 200));

}

else if (temp_backColor.equals("淡紅")) {

att.setBackColor(new Color(255, 200, 200));

}

else if (temp_backColor.equals("淡藍(lán)")) {

att.setBackColor(new Color(200, 200, 255));

}

else if (temp_backColor.equals("淡黃")) {

att.setBackColor(new Color(255, 255, 200));

}

else if (temp_backColor.equals("淡綠")) {

att.setBackColor(new Color(200, 255, 200));

}

}

return att;

}

}

求一個(gè)簡(jiǎn)單的java代碼:(圖形界面)

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class vv extends JDialog {

private static final long serialVersionUID = 1L;

private JLabel l_Id = new JLabel("登陸賬戶", JLabel.CENTER);

private JLabel l_pw = new JLabel("登陸密碼", JLabel.CENTER);

private JTextField t_Id = new JTextField(10);

private JPasswordField t_pw = new JPasswordField(10);

private JButton btnLogin;

private JButton btnClose;

public vv() {

super();

setResizable(false);

getContentPane().setBackground(new Color(225, 225, 225));

getContentPane().setLayout(null);

initialize();

}

protected void initialize() {

setTitle("系統(tǒng)登錄");

l_Id.setBounds(48, 43, 53, 25);

t_Id.setBounds(110, 43, 150, 25);

l_pw.setBounds(48, 93, 53, 25);

t_pw.setBounds(110, 93, 150, 25);

getContentPane().add(l_Id);

getContentPane().add(l_pw);

getContentPane().add(t_Id);

getContentPane().add(t_pw);

btnLogin = new JButton();

btnLogin.setText("登 錄");

btnLogin.setBounds(70, 142, 85, 28);

btnLogin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

addBtnLoginActionListener();

}

});

getContentPane().add(btnLogin);

btnClose = new JButton();

btnClose.setText("關(guān) 閉");

btnClose.setBounds(175, 142, 85, 28);

btnClose.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

dispose();

System.exit(-1);

}

});

getContentPane().add(btnClose);

}

private void addBtnLoginActionListener() {

String user = t_Id.getText();

String password = new String(t_pw.getPassword());

if (user.equals("")) {

JOptionPane.showMessageDialog(this, "帳號(hào)不可為空", "Caution",

JOptionPane.WARNING_MESSAGE);

return;

}

if (password.equals("")) {

JOptionPane.showMessageDialog(this, "密碼不可為空", "Caution",

JOptionPane.WARNING_MESSAGE);

return;

}

String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"

+ password + "'";

boolean success = false;

// TODO:數(shù)據(jù)校驗(yàn) success = executeQuery(sql);

if (success) {

// TODO: 如果數(shù)據(jù)校驗(yàn)成功 顯示主界面 并關(guān)閉登錄界面

JOptionPane.showMessageDialog(this, "成功登錄", "提示",

JOptionPane.INFORMATION_MESSAGE);

this.dispose();

} else {

JOptionPane.showMessageDialog(this, "帳號(hào)或密碼錯(cuò)誤!", "警告",

JOptionPane.WARNING_MESSAGE);

t_pw.requestFocus(); // 密碼框選中

}

}

public Dimension getPreferredSize() {

return new Dimension(320, 170);

}

public void show() {

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension screen = tk.getScreenSize();

Dimension d = getSize();

this.setLocation((screen.width - d.width) / 2,

(screen.height - d.height) / 2);

// 輸入密碼后回車相當(dāng)于點(diǎn)擊了登錄按鈕

getRootPane().setDefaultButton(btnLogin);

t_pw.requestFocus();

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setSize(300, 220);

super.show();

}

public static void main(String[] args) {

vv loginFrame = new vv();

loginFrame.setVisible(true);

}

}

希望對(duì)你有幫助

java圖形界面代碼

import?java.awt.*;

import?java.awt.event.*;

import?java.io.*;

import?javax.swing.*;

public?class?ReadBook?extends?JFrame?{

JTextArea?jta;

JTextField?jtf;

JButton?jb;

public?ReadBook()?{

jta?=?new?JTextArea();

jtf?=?new?JTextField(30);

jtf.setText("文件保存路徑如c:\\ab.txt");

jb?=?new?JButton("保存文字");

JPanel?jp?=?new?JPanel();

jp.add(jtf);

jp.add(jb);

add(jta);

add(jp,?BorderLayout.SOUTH);

setBounds(500,?100,?500,?380);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

jb.addActionListener(new?ActionListener()?{

@Override

public?void?actionPerformed(ActionEvent?e)?{

//-------------核心代碼---------

String?path?=?jtf.getText();

File?f?=?new?File(path);

String?txt?=?jta.getText().replaceAll("\n",?"\r\n");

try?{

BufferedWriter?bw?=?new?BufferedWriter(new?FileWriter(f));

bw.write(txt);//寫入文件中

bw.close();

}?catch?(Exception?e1)?{

e1.printStackTrace();

}

//-------------核心代碼---------

}

});

}

public?static?void?main(String[]?args)?{

new?ReadBook();

}

}

Java圖形類模擬

package?com.baidu;

public?class?Circle?{

private?int?radius; //圓半徑

private?double?area; //圓面積

public?final?double?PI?=?3.14; //常量

public?Circle(){

}

public?Circle(int?radius,?double?area)?{

this.radius?=?radius;

this.area?=?area;

}

public?double?getArea(int?radius){

return?PI*radius*radius;

}

public?int?getRadius()?{

return?radius;

}

public?void?setRadius(int?radius)?{

this.radius?=?radius;

}

}

package com.zhidao;

import com.baidu.Circle;

public class Cylinder extends Circle{

protected double h; //高

public double getH() { //獲取高

return h;

}

public void setH(double h) { //設(shè)置高

this.h = h;

}

public double getArea(int radius){

return h*PI*radius*radius;

}

}

package?com.test;

import?com.baidu.Circle;

import?com.zhidao.Cylinder;

public?class?Test?{

public?static?void?main(String[]?args)?{

Circle?c?=?new?Circle();

c.setRadius(3);

System.out.println("圓面積"+c.getArea(c.getRadius()));

Cylinder?cy?=?new?Cylinder();

cy.setRadius(6);

cy.setH(3);

System.out.println(cy.getRadius());

double?carea?=?cy.getArea(cy.getRadius());

System.out.println("圓柱體體積:"+carea);

}

}

當(dāng)前題目:java圖型代碼練習(xí) java繪圖程序代碼
標(biāo)題網(wǎng)址:http://bm7419.com/article12/ddcocgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站改版、虛擬主機(jī)、響應(yīng)式網(wǎng)站、網(wǎng)站收錄、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)