butOk.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {
if(jtf1.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用水量不能为空");
}
else if(jtf2.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用电量不能为空");
}
else{
int index1=jcb1.getSelectedIndex(); 想得到第一个JComboBox里面的数字
int index2=jcb2.getSelectedIndex(); 这个是第二个JComboBoxint index3=jcb3.getSelectedIndex(); 三
int index4=jcb4.getSelectedIndex(); 四
int index5=new Integer(jtf1.getText()).intValue(); 第一个JTextField
int index6=new Integer(jtf2.getText()).intValue(); 二
int index7=jcb5.getSelectedIndex(); 五个JComboBoxint a=index1*index5+index2*index6+index3*index7+index4; 计算
jtf3.setText(String.valueOf(a)); 想将上面的计算结果通过按钮触发到第三个JTextField 中
}
}
});
但是不能成功,请大家指教,本人菜鸟。。谢谢了
if(jtf1.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用水量不能为空");
}
else if(jtf2.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用电量不能为空");
}
else{
int index1=jcb1.getSelectedIndex(); 想得到第一个JComboBox里面的数字
int index2=jcb2.getSelectedIndex(); 这个是第二个JComboBoxint index3=jcb3.getSelectedIndex(); 三
int index4=jcb4.getSelectedIndex(); 四
int index5=new Integer(jtf1.getText()).intValue(); 第一个JTextField
int index6=new Integer(jtf2.getText()).intValue(); 二
int index7=jcb5.getSelectedIndex(); 五个JComboBoxint a=index1*index5+index2*index6+index3*index7+index4; 计算
jtf3.setText(String.valueOf(a)); 想将上面的计算结果通过按钮触发到第三个JTextField 中
}
}
});
但是不能成功,请大家指教,本人菜鸟。。谢谢了
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;public class TestAction extends JFrame { private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
private JComboBox jComboBox = null;
private JComboBox jComboBox1 = null;
private JComboBox jComboBox2 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JTextField jTextField2 = null;
private JComboBox jComboBox3 = null;
private JComboBox jComboBox4 = null;
private JComboBox jComboBox5 = null; /**
* This is the default constructor
*/
public TestAction() {
super();
initialize();
} /**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(481, 464);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
} /**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJComboBox(), null);
jContentPane.add(getJComboBox1(), null);
jContentPane.add(getJComboBox2(), null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(getJTextField2(), null);
jContentPane.add(getJComboBox3(), null);
jContentPane.add(getJComboBox4(), null);
jContentPane.add(getJComboBox5(), null);
}
return jContentPane;
} /**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton("求和");
jButton.setBounds(new Rectangle(102, 404, 139, 23));
jButton.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (jTextField.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用水量不能为空");
} else if (jTextField1.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用电量不能为空");
} else {
int index1 = ((Integer) jComboBox.getSelectedItem())
.intValue();
int index2 = ((Integer) jComboBox1.getSelectedItem())
.intValue();
int index3 = ((Integer) jComboBox2.getSelectedItem())
.intValue();
int index4 = ((Integer) jComboBox3.getSelectedItem())
.intValue();
int index5 = new Integer(jTextField.getText())
.intValue();
int index6 = new Integer(jTextField1.getText())
.intValue();
int index7 = ((Integer) jComboBox4.getSelectedItem())
.intValue();
int a = index1 * index5 + index2 * index6 + index3
* index7 + index4;
jTextField2.setText(String.valueOf(a));
}
}
});
}
return jButton;
} /**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox(new Integer[] { 1, 2, 3, 4 });
jComboBox.setBounds(new Rectangle(87, 12, 155, 31));
}
return jComboBox;
} /**
* This method initializes jComboBox1
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox1() {
if (jComboBox1 == null) {
jComboBox1 = new JComboBox(new Integer[] { 1, 2, 3, 4 });
jComboBox1.setBounds(new Rectangle(87, 55, 162, 35));
}
return jComboBox1;
} /**
* This method initializes jComboBox2
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox2() {
if (jComboBox2 == null) {
jComboBox2 = new JComboBox(new Integer[] { 1, 2, 3, 4 });
jComboBox2.setBounds(new Rectangle(87, 102, 167, 39));
}
return jComboBox2;
} /**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(88, 273, 172, 30));
}
return jTextField;
} /**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(88, 315, 169, 28));
}
return jTextField1;
} /**
* This method initializes jTextField2
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField2() {
if (jTextField2 == null) {
jTextField2 = new JTextField();
jTextField2.setBounds(new Rectangle(88, 355, 179, 34));
}
return jTextField2;
} /**
* This method initializes jComboBox3
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox3() {
if (jComboBox3 == null) {
jComboBox3 = new JComboBox(new Integer[] { 1, 2, 3, 4 });
jComboBox3.setBounds(new Rectangle(86, 149, 184, 36));
}
return jComboBox3;
} /**
* This method initializes jComboBox4
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox4() {
if (jComboBox4 == null) {
jComboBox4 = new JComboBox(new Integer[] { 1, 2, 3, 4 });
jComboBox4.setBounds(new Rectangle(81, 194, 188, 37));
}
return jComboBox4;
} /**
* This method initializes jComboBox5
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox5() {
if (jComboBox5 == null) {
jComboBox5 = new JComboBox(new Integer[] { 1, 2, 3, 4 });
jComboBox5.setBounds(new Rectangle(79, 236, 194, 32));
}
return jComboBox5;
}}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cost extends JFrame{
private JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8;
private JTextField jtf1,jtf2,jtf3,jtf4;
private JButton butOk;
private JComboBox jcb1,jcb2,jcb3,jcb4;
public cost(){
super("费用管理");
initComponent();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(600,500);
pack();
}
public void initComponent(){
jl1=new JLabel("平均水费");
jl2=new JLabel("平均电费");
jl3=new JLabel("物业管理费");
jl4=new JLabel("停车费");
jl5=new JLabel("用水量");
jl6=new JLabel("用电量");
jl7=new JLabel("有效面积");
jl8=new JLabel("总费用");
jcb1=new JComboBox();
jcb2=new JComboBox();
jcb3=new JComboBox();
jcb4=new JComboBox();
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
jtf4=new JTextField(10);
butOk=new JButton("计算");
getContentPane().setLayout(null);
getContentPane().add(jl1);
jl1.setBounds(30,30,70,30);
getContentPane().add(jl2);
jl2.setBounds(30,80,70,30);
getContentPane().add(jl3);
jl3.setBounds(30,130,70,30);
getContentPane().add(jl4);
jl4.setBounds(30,180,70,30);
getContentPane().add(jl5);
jl5.setBounds(250,30,70,30);
getContentPane().add(jl6);
jl6.setBounds(250,80,70,30);
getContentPane().add(jl7);
jl7.setBounds(250,130,70,30);
getContentPane().add(jl8);
jl8.setBounds(280,250,70,30);
getContentPane().add(jcb1);
jcb1.setBounds(120,30,100,30);
getContentPane().add(jcb2);
jcb2.setBounds(120,80,100,30);
getContentPane().add(jcb3);
jcb3.setBounds(120,130,100,30);
getContentPane().add(jcb4);
jcb4.setBounds(120,180,100,30);
getContentPane().add(jtf1);
jtf1.setBounds(340,30,100,30);
getContentPane().add(jtf2);
jtf2.setBounds(340,80,100,30);
getContentPane().add(jtf3);
jtf3.setBounds(340,130,100,30);
getContentPane().add(jtf4);
jtf4.setBounds(370,250,100,30);
getContentPane().add(butOk);
butOk.setBounds(100,250,70,30);
database.joinDB();
String sql="select 平均水费,平均电费,物业费,停车费 from 费用表";
try{
if(database.query(sql)){
while(database.rs.next()){
String shuifei=database.rs.getString("平均水费");
String dianfei=database.rs.getString("平均电费");
String wuyefei=database.rs.getString("物业费");
String tingchefei=database.rs.getString("停车费");
jcb1.addItem(shuifei);
jcb2.addItem(dianfei);
jcb3.addItem(wuyefei);
jcb4.addItem(tingchefei);
}
}
} catch(Exception ea){}
butOk.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
if(jtf1.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用水量不能为空");
}
else if(jtf2.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用电量不能为空");
}
else{
int index1=jcb1.getSelectedIndex();
int index2=jcb2.getSelectedIndex();
int index3=jcb3.getSelectedIndex();
int index4=jcb4.getSelectedIndex();
int index5=new Integer(jtf1.getText()).intValue();
int index6=new Integer(jtf2.getText()).intValue();
int index7=new Integer(jtf3.getText()).intValue();
int a=index1*index5+index2*index6+index3*index7+index4;
jtf4.setText(String.valueOf(a));
}
}
});
}
public static void main(String args[]){
cost newone= new cost();
}
}
计算结果就是出不来,一直计算是0