java的JTextfield怎么更改值后怎么显示出来
importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.e...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class myFrame {
private JFrame jf;
private JTextField first,second,jieguo;
private JButton dengyu;
private JComboBox box;
private int a,c;
private String d;
private double b;
public myFrame(){
jf=new JFrame();
first=new JTextField();
second=new JTextField();
dengyu=new JButton("=");
jieguo=new JTextField("0");
box=new JComboBox(new String[]{"","+","-","*","/"});
}
public void init(){
jf.setLayout(null);
jf.add(first);
jf.add(second);
jf.add(dengyu);
jf.add(box);
jf.add(jieguo);
first.setBounds(50, 50, 100, 30);
box.setBounds(160, 50, 50, 30);
second.setBounds(230,50,100,30);
dengyu.setBounds(200,100,50,30);
jieguo.setBounds(200,200,50,30);
box.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
int number =box.getSelectedIndex();
switch (number) {
case 1:
c=(Integer.parseInt(first.getText()))+(Integer.parseInt(second.getText()));
d=String.valueOf(c);
case 2:
c=(Integer.parseInt(first.getText()))-(Integer.parseInt(second.getText()));
d=String.valueOf(c);
case 3:
c=(Integer.parseInt(first.getText()))*(Integer.parseInt(second.getText()));
d=String.valueOf(c);
case 4:
c=(Integer.parseInt(first.getText()))/(Integer.parseInt(second.getText()));
d=String.valueOf(c);
}
}
});
dengyu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jieguo.setText(d);
jf.remove(jieguo);
jf.add(jieguo);
}
});
}
public void showme(){
init();
jf.setSize(500, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
jf.setVisible(true);
}
}
就是这个,简单的计算器,就是弄不出来,输出的值就是不对,帮忙看看 展开
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class myFrame {
private JFrame jf;
private JTextField first,second,jieguo;
private JButton dengyu;
private JComboBox box;
private int a,c;
private String d;
private double b;
public myFrame(){
jf=new JFrame();
first=new JTextField();
second=new JTextField();
dengyu=new JButton("=");
jieguo=new JTextField("0");
box=new JComboBox(new String[]{"","+","-","*","/"});
}
public void init(){
jf.setLayout(null);
jf.add(first);
jf.add(second);
jf.add(dengyu);
jf.add(box);
jf.add(jieguo);
first.setBounds(50, 50, 100, 30);
box.setBounds(160, 50, 50, 30);
second.setBounds(230,50,100,30);
dengyu.setBounds(200,100,50,30);
jieguo.setBounds(200,200,50,30);
box.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
int number =box.getSelectedIndex();
switch (number) {
case 1:
c=(Integer.parseInt(first.getText()))+(Integer.parseInt(second.getText()));
d=String.valueOf(c);
case 2:
c=(Integer.parseInt(first.getText()))-(Integer.parseInt(second.getText()));
d=String.valueOf(c);
case 3:
c=(Integer.parseInt(first.getText()))*(Integer.parseInt(second.getText()));
d=String.valueOf(c);
case 4:
c=(Integer.parseInt(first.getText()))/(Integer.parseInt(second.getText()));
d=String.valueOf(c);
}
}
});
dengyu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jieguo.setText(d);
jf.remove(jieguo);
jf.add(jieguo);
}
});
}
public void showme(){
init();
jf.setSize(500, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
jf.setVisible(true);
}
}
就是这个,简单的计算器,就是弄不出来,输出的值就是不对,帮忙看看 展开
2个回答
展开全部
你的程序不能在box上加监听,因为那样要求你先输入两个JTextField框的值,然后再选加减乘除的
JComboBox才能不出错,不符合输入习惯(一般都是从左到右一次输入值和计算法则)
第二你的程序输出不对是因为switch语句没在分支上加break;语句.
你的程序我帮你改好了,改动的地方见注释,你看看吧。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class myFrame {
private JFrame jf;
private JTextField first,second,jieguo;
private JButton dengyu;
private JComboBox box;
private int a,c;
private String d;
private double b;
public myFrame(){
jf=new JFrame();
first=new JTextField();
second=new JTextField();
dengyu=new JButton("=");
jieguo=new JTextField("0");
box=new JComboBox(new String[]{"","+","-","*","/"});
}
public void init(){
jf.setLayout(null);
jf.add(first);
jf.add(second);
jf.add(dengyu);
jf.add(box);
jf.add(jieguo);
first.setBounds(50, 50, 100, 30);
box.setBounds(160, 50, 50, 30);
second.setBounds(230,50,100,30);
dengyu.setBounds(200,100,50,30);
jieguo.setBounds(200,200,50,30);
//box.addActionListener(new ActionListener() { //这里去掉box的监听程序
//@Override
//public void actionPerformed(ActionEvent e) {
//这里的程序移到dengyu的actionPerformed函数中
//}
//});
dengyu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int number =box.getSelectedIndex();
switch (number) {
case 1:
c=(Integer.parseInt(first.getText()))+(Integer.parseInt(second.getText()));
d=String.valueOf(c);
break;//这里加一句
case 2:
c=(Integer.parseInt(first.getText()))-(Integer.parseInt(second.getText()));
d=String.valueOf(c);
break;//这里加一句
case 3:
c=(Integer.parseInt(first.getText()))*(Integer.parseInt(second.getText()));
d=String.valueOf(c);
break;//这里加一句
case 4:
c=(Integer.parseInt(first.getText()))/(Integer.parseInt(second.getText()));
d=String.valueOf(c);
break;//这里加一句
}
jieguo.setText(d);
//jf.remove(jieguo);//这里去掉一句
//jf.add(jieguo);//这里去掉一句
}
});
}
public void showme(){
init();
jf.setSize(500, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
jf.setVisible(true);
}
public static void main(String[] args) {//这里加主程序
myFrame f=new myFrame();
f.showme();
}
}
展开全部
你的程序问题非常多= =先解决了这些问题吧。
jf.setSize(500, 300);会导致程序丧失跨平台功能,推荐使用jf.pack();直接自动调整窗口大小。
你创建事件监听器时使用的都是老旧的匿名内部类语法,为什么不用Java8的Lambda表达式呢?
排版很丑= =一看就知道是Eclipse做的。
至于显示不出来的问题,可以尝试在第二个事件监听器下方加入如下代码:
jitguo.updateUI();
如果还是不行,你可以把源代码给我,我帮你看看。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询