自己寫的石頭剪子布代碼,有圖形界面,可以把自己選中的圖片命名為.jpg放到工程目錄下做背景圖片。//////////////////////////// package HomeWork;
創新互聯公司服務項目包括惠城網站建設、惠城網站制作、惠城網頁制作以及惠城網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,惠城網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到惠城省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class StoneShearsSack { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new StoneWindow("Sample Game");
}
}
final class StoneWindow extends JFrame implements ActionListener,ItemListener{
JLabel win,draw,lose,score,count;
JTextArea text;
JComboBox list;
JButton fight,clear;
int swin,sdraw,slose,sscore;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
String str1,str2,str3;
Random rdm = new Random();
int flag,random ,flag1, count1;
//final ImageIcon con = new ImageIcon("a.jpg") ;
public StoneWindow(String s){
setTitle("Sample Game");
fight = new JButton("出招");
clear = new JButton("清除分數");
//final ImageIcon con = new ImageIcon("b.jpg") ;
text = new JTextArea(){
final ImageIcon con = new ImageIcon("a.jpg");
Image mg = con.getImage();
{setOpaque(false);}
public void paintComponent(Graphics g){
g.drawImage(mg, 0, 0, this);
super.paintComponent(g);
}
};
text.setForeground(Color.black);
Font f = new Font("華文行楷",Font.BOLD,50);
text.setFont(f);
list = new JComboBox();
list.addItem("石頭");
list.addItem("剪子");
list.addItem("布");
panel2.setBackground(Color.blue);
panel2.add(list);
panel2.add(fight);
panel2.add(clear);
panel2.setPreferredSize(new Dimension(900,50));
list.addItemListener(this);
fight.addActionListener(this);
clear.addActionListener(this);
win = new JLabel("贏:" + swin);
draw = new JLabel(" 和:" + sdraw);
lose = new JLabel(" 輸:" + slose);
score = new JLabel(" 得分:" + sscore);
count = new JLabel(" 總局:" + count1);
Font f1 = new Font("宋體",1,30);
win.setFont(f1);
draw.setFont(f1);
lose.setFont(f1);
score.setFont(f1);
count.setFont(f1);
win.setForeground(Color.cyan);
draw.setForeground(Color.green);
lose.setForeground(Color.blue);
score.setForeground(Color.black);
count.setForeground(Color.pink);
panel1.setBackground(Color.red);
panel1.add(win);
panel1.add(draw);
panel1.add(lose);
panel1.add(score);
panel1.add(count);
panel1.setPreferredSize(new Dimension(900,50));
add(panel1,BorderLayout.NORTH);
add(text,BorderLayout.CENTER);
add(panel2,BorderLayout.SOUTH);
setBounds(100,100,900,450);
validate();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == fight){
if(list.getSelectedItem() == "石頭"){
flag = 0;
}
else if(list.getSelectedItem() == "剪子"){
flag = 1;
}
else if(list.getSelectedItem() == "布"){
flag = 2;
}
str1 = (String)list.getSelectedItem();
text.setText("");
text.append("\n");
text.append(" 你 出: " + str1 + "\n");
text.append(" 電腦出: " + this.random() + "\n");
text.append(" 結 果: " + this.result() + "\n");
this.finalresult();
}
if(e.getSource() == clear){
swin = 0;
sdraw = 0;
slose = 0;
sscore = 0;
count1 = 0;
text.setText("");
win.setText(" 贏:" + swin);
draw.setText(" 和:" + sdraw);
lose.setText(" 輸:" + slose);
score.setText(" 得分:" + sscore);
count.setText(" 總局:" + count1);
}
}
public String random(){
random = rdm.nextInt(3);
if(random == 0)
str2 = "石頭";
else if(random == 1)
str2 = "剪子";
else if(random == 2)
str2 = "布";
return str2;
}
public String result(){
if(random == flag){
str3 ="打平";
flag1 = 0;
}
else if(random flag flag == 1){
str3 = "你輸了";
flag1 = 1;
}
else if(random flag random ==1){
str3 = "你輸了";
flag1 = 1;
}
else if(random ==2 flag == 0){
str3 = "你輸了";
flag1 = 1;
}
else{
str3 = "你贏了";
flag1 = 2;
}
return str3;
}
public void finalresult(){
if(flag1 == 0){
sdraw ++;
draw.setText(" 平:" + sdraw);
score.setText(" 得分:" + sscore);
}
if(flag1 == 1){
slose ++;
sscore = sscore - 10;
lose.setText(" 輸:" + slose);
score.setText(" 得分:" + sscore);
}
if(flag1 == 2) {
swin ++;
sscore = sscore + 10;
win.setText(" 贏:" + swin);
score.setText(" 得分:" + sscore);
}
count1 = swin + slose + sdraw;
count.setText(" 總局:" + count1);
}
public void itemStateChanged(ItemEvent e){
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class GUI extends JFrame implements ActionListener {//實現監聽接口
JTextField field = new JTextField(15);
double num1=0,num2;
char op;
public GUI(){
setLayout(new BorderLayout(10,10));
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p1.add(new JLabel("計算框:"));
p1.add(field);
add(p1,BorderLayout.NORTH);
JPanel p2 = new JPanel(new GridLayout(4,1));
//添加按鈕
JButton bt1 = new JButton("1");
JButton bt2 = new JButton("2");
JButton bt3 = new JButton("3");
JButton bt4 = new JButton("4");
JButton bt5 = new JButton("5");
JButton bt6 = new JButton("6");
JButton bt7 = new JButton("7");
JButton bt8 = new JButton("8");
JButton bt9 = new JButton("9");
JButton bt0 = new JButton("0");
JButton btDel = new JButton("Del");
JButton btEqual = new JButton("=");
p2.add(bt1);p2.add(bt2);
p2.add(bt3); p2.add(bt4);
p2.add(bt5); p2.add(bt6);
p2.add(bt7);p2.add(bt8);
p2.add(bt9);p2.add(bt0);
p2.add(btDel);p2.add(btEqual);
add(p2,BorderLayout.CENTER);
JPanel p3 = new JPanel(new GridLayout(0,1));
JButton bta = new JButton("+");
JButton btb = new JButton("-");
JButton btc = new JButton("*");
JButton btd = new JButton("/");
p3.add(bta);p3.add(btb);
p3.add(btc);p3.add(btd);
add(p3,BorderLayout.EAST);
//添加監聽器
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt0.addActionListener(this);
bta.addActionListener(this);
btb.addActionListener(this);
btc.addActionListener(this);
btd.addActionListener(this);
btDel.addActionListener(this);
btEqual.addActionListener(this);
}
//事件的響應
public void actionPerformed(ActionEvent e){
String str =e.getActionCommand();
String s1 = field.getText();
if(str!="+"str!="-"str!="*"str!="/"str!="="str!="Del"){//為數字按鈕時
field.setText(s1+str);
}
else if(str=="Del"){//刪除鍵
field.setText("");
str=null;
}
else if(str=="="){//等號按鈕
s1 =field.getText();
String t2 ="";
int flag=0;//標記操作符的位置
if(s1.indexOf("+")!=-1)
flag = s1.indexOf("+");
if(s1.indexOf("-")!=-1)
flag = s1.indexOf("-");
if(s1.indexOf("*")!=-1)
flag = s1.indexOf("*");
if(s1.indexOf("/")!=-1)
flag = s1.indexOf("/");
for(int i=flag+1;is1.length();i++)
{
t2+=s1.charAt(i);//獲得第二個操作數
}
num2 =Double.parseDouble(t2);//將第二個操作數轉換為double
switch(op){
case '+': num1+=num2;break;
case '-': num1-=num2;break;
case '*': num1*=num2;break;
case '/': num1/=num2;break;
}
field.setText(s1+"="+num1+"");//顯示在文本框里
}
else{//加減乘除符號
num1 = Double.parseDouble(field.getText());
field.setText(s1+str);
op = str.charAt(0);//獲得操作符
}
}
public static void main(String[] args){
GUI frame = new GUI();
frame.setTitle("簡易計算器");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
//frame.pack();
frame.setVisible(true);
}
}
不懂再問
1、網頁:
(1)、用frameset加iframe
(2)、直接寫好div+css,左側導入菜單的頁面(import/link),右側導入不同的頁面
2、PC端:
用東南西北中的那個布局 BorderLayout
新聞名稱:java界面轉換源代碼 java源代碼怎么變成軟件
本文來源:http://vcdvsql.cn/article26/ddsepcg.html
成都網站建設公司_創新互聯,為您提供外貿網站建設、自適應網站、品牌網站制作、虛擬主機、服務器托管、靜態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯