以下是代码部分
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.*;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
public class Update_Question extends JFrame{
JFrame jf;
JPanel jp;
JLabel jl;
JButton jb, jb1;
JTextField jt;
JComboBox jc,jc2;
ArrayList<String>qlist = new ArrayList();
String a[],s1;
Update_Question()
{
jp = new JPanel();
jp.setBackground(Color.pink);
jp.setSize(800, 600);
jp.setLayout(null);
add(jp);
jl = new JLabel();
jl.setText("Update Question");
jl.setFont(new Font("宋体",Font.BOLD,40));
jl.setBounds(250, 40, 450, 100);
jl.setForeground(Color.darkGray);
jp.add(jl);
String []cc ={ "Course Name","Advanced mathematics","Data structure", "Discrete mathematics", "Java programming","College English", "College physics","Database","Computer composition principle "};
jc = new JComboBox(cc);
jc.setBounds(150, 150, 150, 50);
jc.setFont(new Font("宋体",Font.BOLD,20));
jc.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String course = jc.getSelectedItem().toString();
DB_Connect2 obj = new DB_Connect2();
Connection con = obj.connect();
qlist.clear();
try{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from question where course='"+course+"'");
while(rs.next())
{
qlist.add(rs.getString(3));
}
for(int i=0;i<=qlist.size();i++){
jc2.addItem(qlist.get(i));
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
});
jp.add(jc);
// String []cc1={"Question",""};
jc2 = new JComboBox();
// jc2.addItem("Select Question");
jc2.setBounds(310, 150, 340, 50);
jc2.setFont(new Font("宋体",Font.BOLD,20));
jc2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
s1= jc2.getSelectedItem().toString();
jt.setText(jc2.getSelectedItem().toString());
}
});
jp.add(jc2);
jt = new JTextField();
jt.setBounds(150, 270, 500, 50);
jp.add(jt);
jb1 = new JButton();
jb1.setText("OK");
jb1.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<=qlist.size();i++){
jc2.removeAllItems();
}
}
}
);
jb1.setBounds(400, 400, 200, 50);
jb1.setFont(new Font("宋体",Font.BOLD,30));
jb = new JButton();
jb.setText("OK");
jb.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String course= jc.getSelectedItem().toString();
String question = jc2.getSelectedItem().toString();
int i=1;
String s =""+i;
i++;
DB_Connect2 obj = new DB_Connect2();
obj.update(course,s , question);
}
}
);
jb.setBounds(150, 400, 200, 50);
jb.setFont(new Font("宋体",Font.BOLD,30));
/*jb.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Delect_Question Delect_Question = new Delect_Question();
dispose();
}
}
);*/
jp.add(jb);
jp.add(jb1);
this.setSize(800,600);
this.setLayout(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public static void main(String[] args) {
Update_Question obj = new Update_Question();
}
}
我需要在选中第一个下拉列表的选项后,在第二个下拉列表同步第一个选中项在数据库里的所有数据