Taidw 2020-12-17 13:43 采纳率: 0%
浏览 437

java JComboBox 下拉列表怎么随着事件动态刷新?

以下是代码部分

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();
    }
}

我需要在选中第一个下拉列表的选项后,在第二个下拉列表同步第一个选中项在数据库里的所有数据

  • 写回答

1条回答 默认 最新

  • 林林林林七 2020-12-17 14:35
    关注

    swing许久没用 我记得应该是onchange

    评论

报告相同问题?

悬赏问题

  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding