java動態(tài)圖片界面代碼 網(wǎng)頁動圖的代碼

java實(shí)現(xiàn)游戲背景圖片的動態(tài)移動,我想用java做一個小霸王中冒險島這樣的游戲,背景移動怎么實(shí)現(xiàn)?

// 想實(shí)現(xiàn)你的方法,用畫圖是最快的,即在一個組件上直接畫上你的背景圖

成都創(chuàng)新互聯(lián)專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、綏江網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為綏江等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

// 建議你看看swing 畫圖這方面的書籍

// 其它我就不寫

class My extends JComponent {

// 記住這個方法不要直接調(diào)用,如果想讓界面更新,請用repaint();交由UI線程來完成

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

BufferedImage image = null;

try {

image = ImageIO.read(new FileInputStream(""));

} catch (Exception e) {

}

// 這里是圖片從100,200處開始畫

g.drawImage(image, -100, -200, null);

}

}

java中在窗體的創(chuàng)建一個面板中加入一個gif動態(tài)圖

你是說 JPanel 嗎?兩種方法:

添加 JLabel,給 JLabel 添加 ImageIcon,然后放置 JLabel 到 JPanel 上就行了:

JPanel pnl = new JPanel();

......

JLabel lblImage = new JLabel(new ImageIcon("你的文件名")); //創(chuàng)建一個帶圖片的 JLabel

lblImage.setBounds(0, 0, pnl.getWidth(), pnl.getHeight()); //設(shè)置 圖片的橫坐標(biāo)、縱坐標(biāo)、寬、高

pnl.add(lblImage); //放置這個 JLabel 到你的 JPanel 上面

還有一種方法是重寫 JPanel 的 paint() 方法:

class YourPanel extends JPanel {

private Image img = Toolkit.getDefaultToolkit().createImage("你的文件名"); //用 Toolkit 的 createImage 方法來創(chuàng)建一個 Image 對象

@Override public void paint (Graphics g) {

super.paint(g);

g.drawImage(img, 0, 0, getWidth(), getHeight()); //繪制圖片,第一個參數(shù)是 Image 對象,第二個參數(shù)是 橫坐標(biāo),第三個是 縱坐標(biāo),第四個是 寬,第五個是 高

}

}

以上你隱式重寫也可以:

JPanel pnl = new JPanel() {

@Override public void paint (Graphics g) {

...... //但是要注意這樣如果你把 Image 聲明成了局部變量或者參數(shù)形式,你需要聲明成 final

}

}

JAVA的圖形用戶界面代碼

package hao;

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);

//注意下面對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 = { "無色", "灰色", "淡紅", "淡藍(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)建一個可以容納多個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("字號~~"),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("儲存記錄");

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("無色")) {

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;

}

}

求一個簡單的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, "帳號不可為空", "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, "帳號或密碼錯誤!", "警告",

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);

}

}

希望對你有幫助

本文題目:java動態(tài)圖片界面代碼 網(wǎng)頁動圖的代碼
網(wǎng)頁URL:http://www.muchs.cn/article38/doocepp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版移動網(wǎng)站建設(shè)、網(wǎng)站收錄電子商務(wù)、營銷型網(wǎng)站建設(shè)、商城網(wǎng)站

廣告

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

成都網(wǎng)頁設(shè)計公司