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才对~不好意思~没有考虑清楚~