import?java.awt.BorderLayout;
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供丹徒網(wǎng)站建設(shè)、丹徒做網(wǎng)站、丹徒網(wǎng)站設(shè)計(jì)、丹徒網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、丹徒企業(yè)網(wǎng)站模板建站服務(wù),10余年丹徒做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
import?java.awt.Container;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.WindowEvent;
import?java.awt.event.WindowListener;
import?java.io.*;
import?java.util.*;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JOptionPane;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
import?javax.swing.plaf.OptionPaneUI;
public?class?Demo?{
static?boolean?isRuning=false;
static?boolean?isFirst=true;
@SuppressWarnings("unchecked")
public?static?void?main(String[]?args)?throws?Exception?{
JFrame?form1?=?new?JFrame("Form1");
JTextField?jTextField?=?new?JTextField(10);
jTextField.setSize(10,?10);
jTextField.setText("0");
jTextField.setEditable(false);
JButton?jButton?=?new?JButton("開(kāi)始");
jButton.setSize(10,?10);
Thread?thread?=?new?Thread(new?Runnable()?{
@Override
public?void?run()?{
while?(true)?{
while(isRuning){
Integer?counter?=?Integer.parseInt(jTextField.getText().trim());
counter++;
jTextField.setText(counter.toString());
try?{
Thread.sleep(1000);
}?catch?(Exception?e2)?{
}
}
}
}
});
jButton.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
???String?text=jButton.getText().equals("開(kāi)始")?"暫停":"開(kāi)始";
???jButton.setText(text);
???isRuning=!isRuning;
???if(isFirst){
???thread.start();
???isFirst=false;
???}
}
});
JPanel?panel?=?new?JPanel();
panel.setSize(200,?200);
panel.add(jTextField,?BorderLayout.NORTH);
panel.add(jButton,?BorderLayout.CENTER);
form1.add(panel);
form1.setBounds(200,?100,?250,?150);
form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form1.addWindowListener(new?WindowListener()?{
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowIconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowDeiconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowDeactivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?窗口關(guān)閉前取出文本框的數(shù)字保存到外部文件,代碼在此處寫
JOptionPane.showMessageDialog(null,?"Are?you?sure?closing?");
}
@Override
public?void?windowClosed(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?windowActivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
}
});
form1.setVisible(true);
}
}
html
head
title New Document /title
script
function btn(){
var first = document.getElementById("first").value;
document.getElementById("result").value = eval(first);;
}
/script
/head
body
輸入運(yùn)算公式:input type="text" id="first" /
結(jié)果:input type="text" id="result" /
input type="button" value="計(jì)算" onclick="btn()" /
/body
/html
1、首先,設(shè)置在網(wǎng)頁(yè)中顯示文本的格式,文本的高度為100像素,寬度為280像素。
2、定義顯示文本的字體大小為22像素,離左側(cè)和右側(cè)的距離設(shè)置為自動(dòng),離頂部的距離設(shè)置為15像素。
3、用font標(biāo)簽在網(wǎng)頁(yè)中插入要顯示的文本。
4、由于顯示的文本內(nèi)容是動(dòng)態(tài)控制的,所以設(shè)置一個(gè)標(biāo)識(shí)符poemsi,用來(lái)動(dòng)態(tài)表示顯示的詩(shī)句。
5、然后,在網(wǎng)頁(yè)中插入一個(gè)按鈕。
6、按鈕的高度為33像素,寬度為100像素,離左側(cè)和右側(cè)的距離為自動(dòng),離頂部的距離為20像素。
7、用type屬性,定義按鈕的類型為button。
8、定義按鈕的標(biāo)簽為顯示,單擊后執(zhí)行的函數(shù)為show。
9、按鈕的背景色值為#aaeded,字體大小為20像素。
import?java.awt.Color;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
public?class?Test?extends?JFrame{
public?Test(){
this.setSize(400,300);
this.setLocationRelativeTo(null);//設(shè)置居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel?jp?=new?JPanel();
final?JButton?jb1?=?new?JButton("彈出藍(lán)色");
JButton?jb2?=?new?JButton("彈出青色");
jp.add(jb1);
jp.add(jb2);
//給第一個(gè)按鈕添加監(jiān)聽(tīng)
jb1.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
JFrame?jf=new?JFrame();
JPanel?jp=new?JPanel();
jf.setSize(200,?200);
jf.add(jp);
jp.setBackground(Color.blue);
jf.setLocationRelativeTo(jb1);
jf.setVisible(true);
}
});
//給第二個(gè)按鈕添加監(jiān)聽(tīng)
jb2.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
JFrame?jf=new?JFrame();
JPanel?jp=new?JPanel();
jf.setSize(200,?200);
jf.add(jp);
jp.setBackground(Color.cyan);
jf.setLocationRelativeTo(jb1);
jf.setVisible(true);
}
});
this.add(jp);
}
public?static?void?main(String?arg[]){
new?Test().setVisible(true);
}
}
需要使用HttpSession對(duì)象將當(dāng)前會(huì)話過(guò)程中的變量的中間結(jié)果進(jìn)行保存;也就是說(shuō),每次計(jì)算前從HttpSession對(duì)象中取出上次的結(jié)果并加一,然后將該結(jié)果再保存到HttpSession對(duì)象中去。代碼樣例(部分代碼,需要嵌入到JSP文件中)如下:
...
HttpSession sess=request.getHttpSession();
String counter=sess.getParameter("COUNTER");
if(counter==null) counter="0";
int value=Integer.parseInt(counter);
value++;
sess.setParameter("COUNTER",""+value);
...
我沒(méi)有用到題目里給的計(jì)算時(shí)間差來(lái)計(jì)算,想不明白有啥用。
另外我就沒(méi)布局了,隨便做了個(gè)界面美化你自己想辦法吧。
代碼為:
import javax.swing.*;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
public class T {
private static int click = 0;//點(diǎn)擊多少次
private static int time = 60;//剩余多少秒
public static void main(String[] args) {
JFrame jFrame = new JFrame();
//設(shè)置默認(rèn)布局
jFrame.setLayout(new FlowLayout());
//設(shè)置窗口的屬性 窗口位置以及窗口的大小
jFrame.setBounds(200, 200, 300, 200);
//設(shè)置窗口可見(jiàn)
jFrame.setVisible(true);
//設(shè)置關(guān)閉方式 如果不設(shè)置的話 似乎關(guān)閉窗口之后不會(huì)退出程序
jFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//得到窗口的容器
Container conn = jFrame.getContentPane();
JLabel jLabel1 = new JLabel("點(diǎn)擊次數(shù):" + click + "");
JLabel jLabel2 = new JLabel("剩余時(shí)間:" + time + "秒");
JButton jButton = new JButton("點(diǎn)我點(diǎn)我");
jButton.setBounds(50, 50, 100, 20);
jButton.setBackground(Color.CYAN);
Timer timer = new Timer();//利用計(jì)時(shí)器計(jì)時(shí)
jButton.addActionListener(e - {//為按鈕添加事件
if (time 0) {//剩余時(shí)間大于0才觸發(fā)事件
jLabel1.setText("點(diǎn)擊次數(shù):" + ++click);
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
Thread.sleep(1000);
jLabel2.setText("剩余時(shí)間:" + --time + "秒");
if (time == 0) {
timer.cancel();//停止計(jì)時(shí)
}
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}, 0);
}
});
conn.add(jLabel1);
conn.add(jButton);
conn.add(jLabel2);
}
}
運(yùn)行結(jié)果:
沒(méi)開(kāi)始點(diǎn)的時(shí)候:
開(kāi)始點(diǎn):
到時(shí)間后還是能點(diǎn),但是點(diǎn)擊次數(shù)就不會(huì)增加了,計(jì)時(shí)器也停止了。
修改部分代碼:
Timer timer = new Timer();//利用計(jì)時(shí)器計(jì)時(shí)
final boolean[] onClock = {false};//因?yàn)橄喈?dāng)于監(jiān)聽(tīng)事件用了匿名類,這里必須這么寫
jButton.addActionListener(e - {//為按鈕添加事件
if (!onClock[0]) {//第一次點(diǎn)擊時(shí)才觸發(fā)
timer.schedule(new TimerTask() {
@Override
public void run() {
jLabel2.setText("剩余時(shí)間:" + --time + "秒");
if (time == 0) {
timer.cancel();//停止計(jì)時(shí)
}
}
}, 1000, 1000);//延遲1秒,并每隔1秒執(zhí)行一次
onClock[0] = true;
}
if (time 0) {//剩余時(shí)間大于0才觸發(fā)事件
jLabel1.setText("點(diǎn)擊次數(shù):" + ++click);
}
});
這樣才正確,確保只啟用一個(gè)計(jì)時(shí)器Timer,也只有1個(gè)計(jì)時(shí)器任務(wù)TimerTask