如何使用JComboBox对象作为java方法的参数?
由于我在代码中使用 JComboBox 参数,我收到一条错误消息。 方法是: public void miseAJourComboBox(JComboBox
消息是:
error: cannot find symbol
public void miseAJourComboBox(JComboBox<String> jcb){
symbol: class JComboBox
location: class FrameAchat
代码(简化)是:
public class FrameAchat extends javax.swing.JFrame {
public ConnectionSQL connectionSQL;
/** Creates new form FrameAchat */
public FrameAchat() {
connectionSQL = new ConnectionSQL();
System.out.println("passe par constructeur de achat");
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabelFicheAchat = new javax.swing.JLabel();
jComboIdPdt = new javax.swing.JComboBox<>();
jLabelMarque = new javax.swing.JLabel();
jLabelModele = new javax.swing.JLabel();
jLabelAnnee = new javax.swing.JLabel();
jLabelPrix = new javax.swing.JLabel();
jComboIdFrs = new javax.swing.JComboBox<>();
jLabelNom = new javax.swing.JLabel();
jComboQte = new javax.swing.JComboBox<>();
jLabelQte = new javax.swing.JLabel();
Achat = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabelFicheAchat.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabelFicheAchat.setText("Creation Achat");
jComboIdPdt.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jLabelMarque.setText("jLabel1");
jLabelModele.setText("jLabel1");
jLabelAnnee.setText("jLabel1");
jLabelPrix.setText("jLabel1");
jComboIdFrs.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jLabelNom.setText("jLabel1");
jComboQte.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jLabelQte.setText("Quantité:");
Achat.setText("ACHETER");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jLabelFicheAchat, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 177, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(91, 91, 91))
.add(layout.createSequentialGroup()
.add(24, 24, 24)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jComboIdPdt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(53, 53, 53)
.add(jLabelMarque)
.add(18, 18, 18)
.add(jLabelModele)
.add(28, 28, 28)
.add(jLabelAnnee)
.add(18, 18, 18)
.add(jLabelPrix))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jComboIdFrs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelQte))
.add(53, 53, 53)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jComboQte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelNom)
.add(Achat))))
.addContainerGap(66, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(21, 21, 21)
.add(jLabelFicheAchat)
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jComboIdPdt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelMarque)
.add(jLabelModele)
.add(jLabelAnnee)
.add(jLabelPrix))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jComboIdFrs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelNom))
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jComboQte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelQte))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 76, Short.MAX_VALUE)
.add(Achat)
.add(51, 51, 51))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameAchat().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Achat;
private javax.swing.JComboBox<String> jComboIdFrs;
private javax.swing.JComboBox<String> jComboIdPdt;
private javax.swing.JComboBox<String> jComboQte;
private javax.swing.JLabel jLabelAnnee;
private javax.swing.JLabel jLabelFicheAchat;
private javax.swing.JLabel jLabelMarque;
private javax.swing.JLabel jLabelModele;
private javax.swing.JLabel jLabelNom;
private javax.swing.JLabel jLabelPrix;
private javax.swing.JLabel jLabelQte;
// End of variables declaration
public void miseAJourComboBox(JComboBox<String> jcb){
System.out.println("hi");
}
}
I get an error message due to the JComboBox parameter I am using in my code.
The method is:public void miseAJourComboBox(JComboBox<String> jcb)
The message is:
error: cannot find symbol
public void miseAJourComboBox(JComboBox<String> jcb){
symbol: class JComboBox
location: class FrameAchat
and the code (simplified) is:
public class FrameAchat extends javax.swing.JFrame {
public ConnectionSQL connectionSQL;
/** Creates new form FrameAchat */
public FrameAchat() {
connectionSQL = new ConnectionSQL();
System.out.println("passe par constructeur de achat");
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabelFicheAchat = new javax.swing.JLabel();
jComboIdPdt = new javax.swing.JComboBox<>();
jLabelMarque = new javax.swing.JLabel();
jLabelModele = new javax.swing.JLabel();
jLabelAnnee = new javax.swing.JLabel();
jLabelPrix = new javax.swing.JLabel();
jComboIdFrs = new javax.swing.JComboBox<>();
jLabelNom = new javax.swing.JLabel();
jComboQte = new javax.swing.JComboBox<>();
jLabelQte = new javax.swing.JLabel();
Achat = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabelFicheAchat.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabelFicheAchat.setText("Creation Achat");
jComboIdPdt.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jLabelMarque.setText("jLabel1");
jLabelModele.setText("jLabel1");
jLabelAnnee.setText("jLabel1");
jLabelPrix.setText("jLabel1");
jComboIdFrs.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jLabelNom.setText("jLabel1");
jComboQte.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jLabelQte.setText("Quantité:");
Achat.setText("ACHETER");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jLabelFicheAchat, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 177, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(91, 91, 91))
.add(layout.createSequentialGroup()
.add(24, 24, 24)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jComboIdPdt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(53, 53, 53)
.add(jLabelMarque)
.add(18, 18, 18)
.add(jLabelModele)
.add(28, 28, 28)
.add(jLabelAnnee)
.add(18, 18, 18)
.add(jLabelPrix))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jComboIdFrs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelQte))
.add(53, 53, 53)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jComboQte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelNom)
.add(Achat))))
.addContainerGap(66, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(21, 21, 21)
.add(jLabelFicheAchat)
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jComboIdPdt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelMarque)
.add(jLabelModele)
.add(jLabelAnnee)
.add(jLabelPrix))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jComboIdFrs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelNom))
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jComboQte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabelQte))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 76, Short.MAX_VALUE)
.add(Achat)
.add(51, 51, 51))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameAchat().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Achat;
private javax.swing.JComboBox<String> jComboIdFrs;
private javax.swing.JComboBox<String> jComboIdPdt;
private javax.swing.JComboBox<String> jComboQte;
private javax.swing.JLabel jLabelAnnee;
private javax.swing.JLabel jLabelFicheAchat;
private javax.swing.JLabel jLabelMarque;
private javax.swing.JLabel jLabelModele;
private javax.swing.JLabel jLabelNom;
private javax.swing.JLabel jLabelPrix;
private javax.swing.JLabel jLabelQte;
// End of variables declaration
public void miseAJourComboBox(JComboBox<String> jcb){
System.out.println("hi");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这方面得到了帮助。仅供参考,我错过了导入 JComboBox 或在参数中添加 JComboBox 的路径:
而不是:
,现在它可以工作了
I have been helped on that. FYI, I missed to import the JComboBox or to add in my parameter the path of JComboBox:
instead of:
and now it works