选择JComboBox时在JTextField中显示值

时间:2017-11-03 12:26:07

标签: java swing jtextfield jcombobox

您好我正在处理项目的朋友但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); } } 。请帮忙 !。这是我的源代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.extern.com/extern/v/fragebogenweg.php?file=412954715521130");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

var_dump($result);

1 个答案:

答案 0 :(得分:0)

您需要连接值以在组件中设置它们,您可以恢复当前值以添加每个值,如

textfield.setText(textfield.getText() + "\n" +CAmt)

但最好使用StringBuilder来连接值,然后在textfield中将其设置为String

StringBuilder sb = new StringBuilder();
while(res.next())
{
    double cdt=res.getDouble("CreditAmount");
    CAmt=Double.toString(cdt);
    sb.append(CAmt).append("\n");
}
textField.setText(sb.toString();