導航:首頁 > 編程語言 > java點擊按鈕text框數字自增

java點擊按鈕text框數字自增

發布時間:2021-10-10 00:32:14

1. 如何用java語言實現點擊等號按鈕把文本框中的數據相加

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.util.regex.Pattern;

public class Example {
public static void main(final java.lang.String[] args) {
java.awt.EventQueue.invokeLater(new java.lang.Runnable(){
@Override public void run(){
final JFrame frame = new JFrame("Example");
final JTextField input1 = new JTextField(8);
final JTextField input2 = new JTextField(8);
final JButton compute = new JButton(" = ");
final JTextField result = new JTextField(10);
final InputVerifier verifier = new InputVerifier(){
private Pattern pattern = Pattern.compile("\\d+");
@Override public boolean verify(javax.swing.JComponent c){
JTextField input = (JTextField) c;
return pattern.matcher(input.getText()).matches();
}
};
input1.setInputVerifier(verifier);
input2.setInputVerifier(verifier);
result.setEditable(false);
compute.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
try {
result.setText(Integer.toString(Integer.valueOf(input1.getText()) + Integer.valueOf(input2.getText())));
} catch (Exception xe) {
assert false:"it should not reach here";
}
}
});
final JPanel container = new JPanel();
container.add(input1);
container.add(new JLabel(" + "));
container.add(input2);
container.add(compute);
container.add(result);
frame.setContentPane(container);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
});
}
}

2. 編一個簡單的程序。 界面幾個按鈕,每點擊一次,數字加1,例如點擊了4次,則顯示4。用C++或JAVA學什麼

我這個是JAVA的,要學習

1.swing圖形編程。主要是JFrame,JButton

2.swing事件編程。主要是JButton的ActionListener

3.Integer類,用於String到int的轉換。

4.語法知識:匿名內部類,介面

5.必要工具:JDK的文檔(API)

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ButtonActionFrame extends JFrame {
public static void main(String[] args) {
new ButtonActionFrame();
}
public ButtonActionFrame() {
this.setSize(600, 450);
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
add(createButton());
add(createButton());
add(createButton());
add(createButton());
this.setVisible(true);
}
private JButton createButton() {
final JButton button = new JButton("0");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(button.getText());
button.setText(String.valueOf(i + 1));
}
});
return button;
}
}

3. 用JAVA寫一個按鈕程序,單擊按鈕按鈕上就自動把兩個自己輸入的數字相加並顯示結果

<html>
<head>
<title> New Document </title>

<script>
function btn(){
var first = document.getElementById("first").value;
document.getElementById("result").value = eval(first);;
}

</script>

</head>
<body>
輸入運算公式:<input type="text" id="first" />
結果:<input type="text" id="result" />
<input type="button" value="計算" onclick="btn()" />
</body>
</html>

4. Java 怎麼點擊一個按鈕 讓文本框里顯示輸出的結果

思路:定義一個按鈕,然後對按鈕進行監聽,監聽後,在裡面將結果放置在需要顯示結果的文本框中即可。

5. 在java中,怎樣實現點擊一個添加按鈕,下面的表單自動添加一行

你是類型於jsp這樣的頁面嗎?這樣的話你可以這樣,新建一個網頁,可以調用 window.open()。方法具體的參數你去找一下,比我說的清楚。刷新調用方法 window.reload()同樣參數你自己去看看。希望是你要的

6. java 點擊添加按鈕, 在文本框內添加一個字母

添加一個按鈕監聽啊,每點一次就添加一個字。刪除同理!稍後給你代碼

importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;

importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextArea;

{

=6244133622077153034L;

privateJTextAreatext=null;
privateJButtonsave=null;
privateJButtoncancel=null;
privateJPanelpanel=null;

publicDemo(){
text=newJTextArea();
save=newJButton("增加");
cancel=newJButton("刪除");
panel=newJPanel();

save.addActionListener(this);
save.setActionCommand("save");

cancel.addActionListener(this);
cancel.setActionCommand("cancel");
panel.setLayout(null);
text.setBounds(5,5,200,30);
panel.add(text);
save.setBounds(10,60,80,40);
panel.add(save);
cancel.setBounds(100,60,80,40);
panel.add(cancel);
add(panel);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


setVisible(true);

}

@Override
publicvoidactionPerformed(ActionEvente){

if(e.getActionCommand().equals("save")){
text.append("好");

}elseif(e.getActionCommand().equals("cancel")){
Stringstr=text.getText();
if(str.length()!=0){
text.setText(str.substring(0,str.length()-1));
}
}


}

publicstaticvoidmain(String[]args){
newDemo();
}

}

7. java點擊不同按鈕給不同文本框賦值

//已經符合你的要求了
//多給點財富吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame implements ActionListener {
public static void main(String[] args) {
test();
}
test() {
JPanel p=new JPanel(),fsp=new JPanel(),bsp=new JPanel();
JTextField tf=new JTextField(10);
JPasswordField pf=new JPasswordField(10);
FocusAdapter focus=new FocusAdapter() {
public void focusGained(FocusEvent e) {
dsttf=(JTextField)e.getSource();
}
};

tf.addFocusListener(focus);
pf.addFocusListener(focus);
fsp.add(tf);
fsp.add(pf);
bsp.setLayout(new GridLayout(3,3));
JButton b=null;
for(int x=1;x<=9;x++) {
b=new JButton(""+x);
b.addActionListener(this);
bsp.add(b);
}
p.setLayout(new GridLayout(2,1));
p.add(fsp);
p.add(bsp);
add(p);
setResizable(false);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton b=(JButton)e.getSource();

if(null!=dsttf)
{
if(dsttf instanceof JPasswordField)
{
JPasswordField pw=(JPasswordField)dsttf;
pw.setText(new String(pw.getPassword())+b.getText());
}
else
{
dsttf.setText(dsttf.getText()+b.getText());
}
}
}
JTextField dsttf=null;
}

8. 用JAVA寫一個按鈕程序,單擊按鈕按鈕上的數字就自動加1,點一次加一次。 然後,在同一個頁面上再弄一個按

import javax.swing.*;
import java.awt.event.*;
public class UITest extends JFrame implements ActionListener{
JButton button;
int count;
UITest(){
count = 0;
button = new JButton("有效按鈕:"+count);
button.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(button);
this.setSize(300,300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
// TODO 自動生成方法存根
count++;
button.setText(""+count);
this.add(new JButton(""+count));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成方法
new UITest();

}

}

9. java中如何實現點擊數字按鈕在文本框中顯示相應的數字

很多方法可以實現,例如常見的 前後台請求響應servlet

10. 怎麼樣做一個JAVA程序:單擊按鈕,在文本框產生一個200到400 的隨機數

這樣有點問題~
在給 i 賦處值的時候,應該是給200才對~不好意思~沒有考慮清楚~

閱讀全文

與java點擊按鈕text框數字自增相關的資料

熱點內容
網路中常用的傳輸介質 瀏覽:518
文件如何使用 瀏覽:322
同步推密碼找回 瀏覽:865
樂高怎麼才能用電腦編程序 瀏覽:65
本機qq文件為什麼找不到 瀏覽:264
安卓qq空間免升級 瀏覽:490
linux如何刪除模塊驅動程序 瀏覽:193
at89c51c程序 瀏覽:329
怎麼創建word大綱文件 瀏覽:622
裊裊朗誦文件生成器 瀏覽:626
1054件文件是多少gb 瀏覽:371
高州禁養區內能養豬多少頭的文件 瀏覽:927
win8ico文件 瀏覽:949
仁和數控怎麼編程 瀏覽:381
項目文件夾圖片 瀏覽:87
怎麼在東芝電視安裝app 瀏覽:954
plc顯示數字怎麼編程 瀏覽:439
如何辨別假網站 瀏覽:711
寬頻用別人的賬號密碼 瀏覽:556
新app如何佔有市場 瀏覽:42

友情鏈接