这里的一切都会好起来,但它没有在组合框中获得选定的文字, 我已经阅读了stackoverflow中的文章bt它根本不工作plz.you可以检查cl.setObject(7,mainBox.getSelectedItem())下面的提交条目按钮(在actionevent中);
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
public class Pay implements ActionListener
{
JFrame frame;
JPanel p1,p2,p3,p4,p5,p6,p7,p8,p9;
JTabbedPane t1;
JLabel idLabel,oneidLabel,nameLabel,addressLabel;
JLabel cityLabel,stateLabel,phoneLabel,itemLabel,pincodeLabel;
JTable dataTable;
JTextField idField,oneidField,nameField,addressField,cityField;
JTextField stateField,phoneField,itemField,pincodeField;
JButton entryButton,accessButton,submitentryButton,accessrecordButton;
JComboBox mainBox;
Font onefont,twofont;
String post[]={"Student","Staff"};
//JTable dataTable,staffTable,onedataTable,onestaffTable;
JScrollPane dataScroll,staffScroll,onedataScroll,onestaffScroll;
GridBagLayout gbl,onegbl,twogbl;
GridBagConstraints gbc,onegbc,twogbc;
public Pay()
{
frame=new JFrame("sample");
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
//p7=new JPanel();
onefont=new Font("Rosemary",Font.BOLD,14);
entryButton=new JButton("Entry");
entryButton.setFont(onefont);
accessButton=new JButton("Access");
accessButton.setFont(onefont);
submitentryButton=new JButton("Submit Entry");
submitentryButton.setFont(onefont);
accessrecordButton=new JButton("Access Record");
accessrecordButton.setFont(onefont);
//addstaffButton=new JButton("Add Staff");
idLabel=new JLabel("Consumer ID");
idLabel.setFont(onefont);
nameLabel=new JLabel("Consumer Name");
nameLabel.setFont(onefont);
addressLabel=new JLabel("Address");
addressLabel.setFont(onefont);
cityLabel=new JLabel("City");
cityLabel.setFont(onefont);
stateLabel=new JLabel("State");
stateLabel.setFont(onefont);
phoneLabel=new JLabel("Enter Phone : ");
phoneLabel.setFont(onefont);
itemLabel=new JLabel("Item");
itemLabel.setFont(onefont);
pincodeLabel=new JLabel("PINCODE");
pincodeLabel.setFont(onefont);
oneidLabel=new JLabel("Consumer ID");
oneidLabel.setFont(onefont);
idField=new JTextField(10);
nameField=new JTextField(10);
addressField=new JTextField(10);
cityField=new JTextField(10);
stateField=new JTextField(10);
pincodeField=new JTextField(10);
itemField=new JTextField(10);
//=new JTextField(10);
oneidField=new JTextField(10);
//code separation
gbc=new GridBagConstraints();
gbl=new GridBagLayout();
p1.setLayout(gbl);
gbc.insets=new Insets(4,4,4,4);
gbc.anchor=GridBagConstraints.WEST;
gbc.gridx=0;
gbc.gridy=0;
gbl.setConstraints(idLabel,gbc);
gbc.gridx=1;
gbc.gridy=0;
gbl.setConstraints(idField,gbc);
gbc.gridx=0;
gbc.gridy=1;
gbl.setConstraints(nameLabel,gbc);
gbc.gridx=1;
gbc.gridy=1;
gbl.setConstraints(nameField,gbc);
gbc.gridx=0;
gbc.gridy=2;
gbl.setConstraints(addressLabel,gbc);
gbc.gridx=1;
gbc.gridy=2;
gbl.setConstraints(addressField,gbc);
gbc.gridx=0;
gbc.gridy=3;
gbl.setConstraints(cityLabel,gbc);
gbc.gridx=1;
gbc.gridy=3;
gbl.setConstraints(cityField,gbc);
gbc.gridx=0;
gbc.gridy=4;
gbl.setConstraints(stateLabel,gbc);
gbc.gridx=1;
gbc.gridy=4;
gbl.setConstraints(stateField,gbc);
gbc.gridx=0;
gbc.gridy=5;
gbl.setConstraints(pincodeLabel,gbc);
gbc.gridx=1;
gbc.gridy=5;
gbl.setConstraints(pincodeField,gbc);
//combobox
String intake[]={"Shoes","Flip-Flop","Sandals","Formal"};
JComboBox mainBox=new JComboBox(intake);
mainBox.setFont(onefont);
gbc.gridx=0;
gbc.gridy=6;
gbl.setConstraints(itemLabel,gbc);
gbc.gridx=1;
gbc.gridy=6;
gbl.setConstraints(mainBox,gbc);
gbc.gridx=1;
gbc.gridy=7;
gbl.setConstraints(submitentryButton,gbc);
p2.setLayout(null);
oneidLabel.setBounds(0,0,150,70);
oneidField.setBounds(90,25,150,25);
accessrecordButton.setBounds(240,20,150,30);
dataTable=new JTable(100,7);
dataScroll=new JScrollPane(dataTable);
// column headers were there but i didn't added them because of text
//settings
//dataTable.getTableHeader().getColumnModel().getColumn(0).setHeaderValue
dataScroll.setBounds(10,60,490,400);
p1.add(idLabel);
p1.add(idField);
p1.add(nameLabel);
p1.add(nameField);
p1.add(addressLabel);
p1.add(addressField);
p1.add(cityLabel);
p1.add(cityField);
p1.add(stateLabel);
p1.add(stateField);
p1.add(pincodeLabel);
p1.add(pincodeField);
p1.add(itemLabel);
p1.add(mainBox);
p1.add(submitentryButton);
p2.add(oneidLabel);
p2.add(oneidField);
p2.add(accessrecordButton);
p2.add(dataScroll);
Font font=new Font("Rosemary",Font.BOLD,32);
//headingLabel.setFont(font);
//headingLabel.setForeground(Color.BLUE);
t1=new JTabbedPane();
t1.setBackground(Color.GREEN);
Font tfont=new Font("Rosemary",Font.BOLD,15);
t1.setFont(tfont);
submitentryButton.addActionListener(this);
accessrecordButton.addActionListener(this);
Toolkit t=Toolkit.getDefaultToolkit();
Dimension d=t.getScreenSize();
frame.setLocation((d.width-400)/2,(d.height-400)/2);
t1.addTab("Entry",p1);
t1.addTab("Access", p2);
frame.add(t1);
frame.setSize(520,520);
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String[] args)
{
new Pay();
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==entryButton)
{
//p1.add( p3);
//p1.revalidate();
p1.setVisible(true);
p2.setVisible(false);
}
if(evt.getSource()==accessButton)
{
//String row[]={"","","","","",};
//p5=new JPanel();
//p5.add(dataScroll);
//p2.add(p4);
//p2.revalidate();
p2.setVisible(true);
p1.setVisible(false);
}
if(evt.getSource()==submitentryButton)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:StudentDSN");
CallableStatement cl=con.prepareCall("{call firstone(?,?,?,?,?,?,?,?)}");
cl.setString(1,idField.getText());
cl.setString(2,nameField.getText());
cl.setString(3,addressField.getText());
cl.setString(4,cityField.getText());
cl.setString(5,stateField.getText());
cl.setString(6,pincodeField.getText());
//String x = String.valueOf(mainBox.getSelectedItem());
//cl.setObject(7,mainBox.getSelectedItem());
cl.setObject(7,mainBox.getSelectedItem());
cl.registerOutParameter(8, Types.INTEGER);
cl.executeUpdate();
int i=cl.getInt(8);
if(i==1)
{
JOptionPane.showMessageDialog(frame,"Record successfully stored");
}
else
{
JOptionPane.showMessageDialog(frame,"consumer is already..");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(frame, e+"");
}
}
if(evt.getSource()==accessrecordButton)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connectioncon=DriverManager.getConnection("jdbc:odbc:StudentDSN");
CallableStatement cl=con.prepareCall("{call secondone}");
ResultSet rs=cl.executeQuery();
int i=0;
while(rs.next())
{
dataTable.setValueAt(rs.getString(2),i,0);
dataTable.setValueAt(rs.getString(3),i,1);
dataTable.setValueAt(rs.getString(4),i,2);
dataTable.setValueAt(rs.getString(5),i,3);
dataTable.setValueAt(rs.getString(6),i,4);
dataTable.setValueAt(rs.getString(7),i,5);
dataTable.setValueAt(rs.getString(8),i,6);
i++;
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(frame, e+"");
}
}
}
答案 0 :(得分:0)
问题是,您已将JComboBox
声明为类的成员变量。
JComboBox mainBox;
然后在下面的行中初始化它,您创建了JComboBox
的新实例:
JComboBox mainBox=new JComboBox(intake);
因此,当解决方案改变为以上行时:
mainBox=new JComboBox(intake);
最后一件事,不是将JComboBox mainBox;
保留为raw type,而是JComboBox<String> mainBox;
,然后您还需要将the diamond添加到以下行:
mainBox=new JComboBox<>(intake);
要清楚了解这一点,read about generics。