【发布时间】:2018-04-16 04:29:48
【问题描述】:
你好朋友我正在做一个项目但是当我从JComboBox中选择dealer name时JComboBox出现问题,它的credit amount将显示在JTextField上。但问题是JComboBox 中只有一个经销商名称显示。请帮忙 !。这是我的源代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Demo2 {
private JFrame frmDemo;
private JTextField textField;
private static Connection con;
private String query,CAmt;
private PreparedStatement PStat;
private ResultSet res;
private JComboBox comboBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Demo2 window = new Demo2();
window.frmDemo.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Demo2() {
initialize();
Database();
Dealer();
}
//********* Database ************//
/* CREATE TABLE Dealer (
DealerName VARCHAR (45),
CreditAmount DOUBLE (10, 4)
);*/
//************************************
public static Connection Database() {
try
{
Class.forName("org.sqlite.JDBC");
con=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Azaz\\workspace\\SalesDesk\\Database\\SalesDesk.db");
return con;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public void Dealer()
{
try
{
query="Select DealerName from Dealer";
PStat=con.prepareStatement(query);
res=PStat.executeQuery();
while(res.next())
{
String dealer=res.getString("DealerName");
comboBox.addItem(dealer);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
PStat.close();
res.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmDemo = new JFrame();
frmDemo.setTitle("Demo");
frmDemo.setBounds(100, 100, 450, 300);
frmDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("Dealer");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
comboBox = new JComboBox();
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try
{
query="Select CreditAmount from Dealer Where DealerName=? ";
PStat=con.prepareStatement(query);
PStat.setString(1, comboBox.getSelectedItem().toString());
res=PStat.executeQuery();
while(res.next())
{
double cdt=res.getDouble("CreditAmount");
CAmt=Double.toString(cdt);
textField.setText(CAmt);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
PStat.close();
res.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
});
textField = new JTextField();
textField.setColumns(10);
JLabel lblCreditAmount = new JLabel("Credit Amount");
lblCreditAmount.setFont(new Font("Tahoma", Font.BOLD, 11));
GroupLayout groupLayout = new GroupLayout(frmDemo.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(44)
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblCreditAmount)))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 314, GroupLayout.PREFERRED_SIZE))
.addGap(21))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(52)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE))
.addGap(37)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
.addComponent(lblCreditAmount))
.addContainerGap(114, Short.MAX_VALUE))
);
frmDemo.getContentPane().setLayout(groupLayout);
}
}
【问题讨论】:
-
您能提供更多minimal reproducible example 吗?据我所知,你有一个
textField.setText(CAmt);,所以你只设置了最后一个值。如果你想要更多,你可能想要连接你的价值观。
标签: java swing jtextfield jcombobox