数值即时改变
比如我要在TEXT1中输入50, 在 TEXT2 中输入100,那么text3的值会自动变为150这要如何写代码.是什么事件.
搜索更多相关的解决方案:
数值
----------------解决方案--------------------------------------------------------
呵,本人菜鸟.不会!
帮你顶
----------------解决方案--------------------------------------------------------
程序代码:
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
/*
* Main.java
*
* Created on 2007年12月7日, 下午1:14
*/
/**
*
* @author Eastsun
*/
public class Main extends javax.swing.JFrame {
/** Creates new form Main */
public Main() {
initComponents();
}
private void initComponents() {
bField = new javax.swing.JTextField();
aField = new javax.swing.JTextField();
summerField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("加法计算器");
setResizable(false);
summerField.setEditable(false);
summerField.setFont(new java.awt.Font("宋体", 1, 12));
jLabel1.setText(" + ");
jLabel2.setText(" = ");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(bField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(aField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(summerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {aField, bField, summerField});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(aField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(summerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(bField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
bField.getDocument().addDocumentListener(listener);
aField.getDocument().addDocumentListener(listener);
pack();
}// </editor-fold>//GEN-END:initComponents
private void updateSummer() {
try {
double a = Double.parseDouble(aField.getText());
double b = Double.parseDouble(bField.getText());
summerField.setText(String.valueOf(a + b));
} catch (NumberFormatException ex) {
summerField.setText("");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception ex){
//do nothing
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField aField;
private javax.swing.JTextField bField;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField summerField;
// End of variables declaration//GEN-END:variables
private DocumentListener listener = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
updateSummer();
}
public void removeUpdate(DocumentEvent e) {
updateSummer();
}
public void changedUpdate(DocumentEvent e) {
updateSummer();
}
};
}
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
/*
* Main.java
*
* Created on 2007年12月7日, 下午1:14
*/
/**
*
* @author Eastsun
*/
public class Main extends javax.swing.JFrame {
/** Creates new form Main */
public Main() {
initComponents();
}
private void initComponents() {
bField = new javax.swing.JTextField();
aField = new javax.swing.JTextField();
summerField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("加法计算器");
setResizable(false);
summerField.setEditable(false);
summerField.setFont(new java.awt.Font("宋体", 1, 12));
jLabel1.setText(" + ");
jLabel2.setText(" = ");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(bField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(aField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(summerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {aField, bField, summerField});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(aField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(summerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(bField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
bField.getDocument().addDocumentListener(listener);
aField.getDocument().addDocumentListener(listener);
pack();
}// </editor-fold>//GEN-END:initComponents
private void updateSummer() {
try {
double a = Double.parseDouble(aField.getText());
double b = Double.parseDouble(bField.getText());
summerField.setText(String.valueOf(a + b));
} catch (NumberFormatException ex) {
summerField.setText("");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception ex){
//do nothing
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField aField;
private javax.swing.JTextField bField;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField summerField;
// End of variables declaration//GEN-END:variables
private DocumentListener listener = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
updateSummer();
}
public void removeUpdate(DocumentEvent e) {
updateSummer();
}
public void changedUpdate(DocumentEvent e) {
updateSummer();
}
};
}
----------------解决方案--------------------------------------------------------
用JDK1.6编译运行.
----------------解决方案--------------------------------------------------------
谢谢!
没想到这么复杂
----------------解决方案--------------------------------------------------------
为什么我把代码复制到我那边就出错了呢?
搞一个让我下载.
[[italic] 本帖最后由 jiasen2007 于 2007-12-7 16:51 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
原帖由 [bold][underline]jiasen2007[/underline][/bold] 于 2007-12-7 15:46 发表 [url=http://bbs.bc-cn.net/redirect.php?goto=findpost&pid=1136197&ptid=190946][/url]
谢谢!
没想到这么复杂
谢谢!
没想到这么复杂
代码是自动生成的,有点乱,来个手工修改过的:
程序代码:
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
/**
*
* @author Eastsun
*/
public class Main extends javax.swing.JFrame {
public Main() {
bField = new javax.swing.JTextField(16);
aField = new javax.swing.JTextField(16);
summerField = new javax.swing.JTextField(16);
javax.swing.JLabel jLabel1 = new javax.swing.JLabel(" + ");
javax.swing.JLabel jLabel2 = new javax.swing.JLabel(" = ");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("加法计算器");
summerField.setEditable(false);
bField.getDocument().addDocumentListener(listener);
aField.getDocument().addDocumentListener(listener);
javax.swing.JPanel panel =new javax.swing.JPanel(new java.awt.BorderLayout());
panel.add(aField,java.awt.BorderLayout.WEST);
panel.add(jLabel1,java.awt.BorderLayout.CENTER);
panel.add(bField,java.awt.BorderLayout.EAST);
javax.swing.JPanel content =new javax.swing.JPanel(new java.awt.BorderLayout());
content.add(panel,java.awt.BorderLayout.WEST);
content.add(jLabel2,java.awt.BorderLayout.CENTER);
content.add(summerField,java.awt.BorderLayout.EAST);
content.setBorder(new javax.swing.border.EmptyBorder(4,16,4,16));
getContentPane().add(content);
pack();
setResizable(false);
}
private void updateSummer() {
try {
double a = Double.parseDouble(aField.getText());
double b = Double.parseDouble(bField.getText());
summerField.setText(String.valueOf(a + b));
} catch (NumberFormatException ex) {
summerField.setText("");
}
}
public static void main(String args[]) {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception ex){
//do nothing
}
new Main().setVisible(true);
}
private javax.swing.JTextField aField;
private javax.swing.JTextField bField;
private javax.swing.JTextField summerField;
private DocumentListener listener = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
updateSummer();
}
public void removeUpdate(DocumentEvent e) {
updateSummer();
}
public void changedUpdate(DocumentEvent e){
}
};
}
----------------解决方案--------------------------------------------------------
谢谢版主
对了,能不能把你做的那个jpx一起发给我。
我主要是要看事件和做法。
private DocumentListener listener = new DocumentListener()
这句话是什么意思
[[italic] 本帖最后由 jiasen2007 于 2007-12-7 19:22 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
jpx是嘛?
private DocumentListener listener = new DocumentListener() {}
就是得到一个DocumentListener
----------------解决方案--------------------------------------------------------
对,我要看一下事件。
对了,你QQ多少呀!我加你。好不好。谢谢了。
Q:67035999
[[italic] 本帖最后由 jiasen2007 于 2007-12-7 19:43 编辑 [/italic]]
----------------解决方案--------------------------------------------------------