我不知道发生了什么。即使条件为false,如果块正在运行,代码也在内部。我在netbeans ide 7.4中设计了我的界面,只是在eclipse中复制粘贴设计。这是我的代码
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AddSymbol extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
Connection c;
public AddSymbol() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
String x = "hello";
if(x.equals("hii"))
{
System.out.println("Inside hi");
}
jinstrument = new javax.swing.JComboBox();
jsymbol = new javax.swing.JComboBox();
jexpiry = new javax.swing.JComboBox();
joption = new javax.swing.JComboBox();
jstrikeprice = new javax.swing.JComboBox();
instrument = new javax.swing.JLabel();
symbol = new javax.swing.JLabel();
expiry = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
instrumentLabel = new javax.swing.JLabel();
symbolLabel = new javax.swing.JLabel();
expiryLabel = new javax.swing.JLabel();
optionLabel = new javax.swing.JLabel();
strikeLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jinstrument.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Equity", "OPTIDX", "OPTSTK", "FUTIDX", "FUTSTK", " " }));
getContentPane().add(jinstrument);
jinstrument.setBounds(10, 38, 83, 20);
jsymbol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jsymbol);
jsymbol.setBounds(120, 38, 210, 20);
jexpiry.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jexpiry);
jexpiry.setBounds(380, 38, 180, 20);
joption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PE", "CE" }));
getContentPane().add(joption);
joption.setBounds(10, 85, 66, 20);
jstrikeprice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jstrikeprice);
jstrikeprice.setBounds(120, 85, 56, 20);
instrument.setText("Instrument");
getContentPane().add(instrument);
instrument.setBounds(10, 13, 83, 14);
symbol.setText("Symbol");
getContentPane().add(symbol);
symbol.setBounds(120, 13, 218, 14);
expiry.setText("Expiry");
getContentPane().add(expiry);
expiry.setBounds(380, 13, 180, 14);
jLabel4.setText("Option Type");
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 69, 83, 14);
jLabel5.setText("Strike Price");
getContentPane().add(jLabel5);
jLabel5.setBounds(120, 69, 56, 14);
jPanel2.setLayout(null);
jinstrument.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
jsymbol.removeAllItems();
int enterloop=0;
if(jinstrument.getSelectedItem().equals("FUTSTK"))
{
enterloop =1;
}
if(enterloop == 1);
{ System.out.println("inside if");
}
}
});
getContentPane().add(jPanel2);
jPanel2.setBounds(10, 135, 577, 338);
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AddSymbol().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel expiry;
private javax.swing.JLabel expiryLabel;
private javax.swing.JLabel instrument;
private javax.swing.JLabel instrumentLabel;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel2;
private javax.swing.JComboBox jexpiry;
private javax.swing.JComboBox jinstrument;
private javax.swing.JComboBox joption;
private javax.swing.JComboBox jstrikeprice;
private javax.swing.JComboBox jsymbol;
private javax.swing.JLabel optionLabel;
private javax.swing.JLabel strikeLabel;
private javax.swing.JLabel symbol;
private javax.swing.JLabel symbolLabel;
// End of variables declaration
}
无论我在jinstrument jcombobox中选择什么,我得到“内部if”作为输出。我已经检查了print语句但是enterloop的值为0并且它仍然进入if语句块。请帮我解决这个问题
答案 0 :(得分:6)
此行;
if(enterloop == 1);
;
表示语句结束。甚至if语句返回false,因此下一行将被执行
答案 1 :(得分:2)
删除第semicolon [;]
行中的if(enterloop == 1);
答案 2 :(得分:0)
尝试这些更新的代码::
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AddSymbol extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
Connection c;
public AddSymbol() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
String x = "hello";
if(x.equals("hii"))
{
System.out.println("Inside hi");
}
jinstrument = new javax.swing.JComboBox();
jsymbol = new javax.swing.JComboBox();
jexpiry = new javax.swing.JComboBox();
joption = new javax.swing.JComboBox();
jstrikeprice = new javax.swing.JComboBox();
instrument = new javax.swing.JLabel();
symbol = new javax.swing.JLabel();
expiry = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
instrumentLabel = new javax.swing.JLabel();
symbolLabel = new javax.swing.JLabel();
expiryLabel = new javax.swing.JLabel();
optionLabel = new javax.swing.JLabel();
strikeLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jinstrument.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Equity", "OPTIDX", "OPTSTK", "FUTIDX", "FUTSTK", " " }));
getContentPane().add(jinstrument);
jinstrument.setBounds(10, 38, 83, 20);
jsymbol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jsymbol);
jsymbol.setBounds(120, 38, 210, 20);
jexpiry.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jexpiry);
jexpiry.setBounds(380, 38, 180, 20);
joption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PE", "CE" }));
getContentPane().add(joption);
joption.setBounds(10, 85, 66, 20);
jstrikeprice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
getContentPane().add(jstrikeprice);
jstrikeprice.setBounds(120, 85, 56, 20);
instrument.setText("Instrument");
getContentPane().add(instrument);
instrument.setBounds(10, 13, 83, 14);
symbol.setText("Symbol");
getContentPane().add(symbol);
symbol.setBounds(120, 13, 218, 14);
expiry.setText("Expiry");
getContentPane().add(expiry);
expiry.setBounds(380, 13, 180, 14);
jLabel4.setText("Option Type");
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 69, 83, 14);
jLabel5.setText("Strike Price");
getContentPane().add(jLabel5);
jLabel5.setBounds(120, 69, 56, 14);
jPanel2.setLayout(null);
jinstrument.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
jsymbol.removeAllItems();
int enterloop=0;
if(jinstrument.getSelectedItem().equals("FUTSTK"))
{
enterloop =1;
}
if(enterloop == 1);
{ System.out.println("inside if")
}
}
});
instrumentLabel.setText("Instrument");
jPanel2.add(instrumentLabel);
instrumentLabel.setBounds(21, 11, 102, 14);
symbolLabel.setText("Symbol");
jPanel2.add(symbolLabel);
symbolLabel.setBounds(129, 11, 126, 14);
expiryLabel.setText("Expiry");
jPanel2.add(expiryLabel);
expiryLabel.setBounds(261, 11, 64, 14);
optionLabel.setText("OptionType");
jPanel2.add(optionLabel);
optionLabel.setBounds(377, 11, 56, 14);
strikeLabel.setText("Strike Price");
jPanel2.add(strikeLabel);
strikeLabel.setBounds(462, 11, 53, 14);
getContentPane().add(jPanel2);
jPanel2.setBounds(10, 135, 577, 338);
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AddSymbol().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel expiry;
private javax.swing.JLabel expiryLabel;
private javax.swing.JLabel instrument;
private javax.swing.JLabel instrumentLabel;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel2;
private javax.swing.JComboBox jexpiry;
private javax.swing.JComboBox jinstrument;
private javax.swing.JComboBox joption;
private javax.swing.JComboBox jstrikeprice;
private javax.swing.JComboBox jsymbol;
private javax.swing.JLabel optionLabel;
private javax.swing.JLabel strikeLabel;
private javax.swing.JLabel symbol;
private javax.swing.JLabel symbolLabel;
// End of variables declaration
}
您的代码出错,因为您有“;”如
if(enterloop == 1);
{ System.out.println("inside if")
}
在if条件之后不应该终止。 如果在你的条件之后有一个分号(终止),那么你在{//花括号}内写的任何代码将永远不会被执行。
答案 3 :(得分:0)
您的代码存在致命缺陷,即一个
if(enterloop == 1) // ; bad semi-colon. Which, made this a single line if.
{ // And, as a result this was "just" an anonymous code block.
System.out.println("inside if");
}