Java Gui秋千通过另一堂课传递参数

发布于 2025-02-03 14:58:35 字数 6671 浏览 2 评论 0原文

IM使用Eclipse插件WindowsBuilder构成GUI,我试图从initialize()方法传递变量“ Combobox Kombo”。我无法调用该方法运行我的传递变量。该方法应该将数据从我的数据库获取到ComboBox。 下面我附上了我的编码,任何帮助将不胜感激,谢谢!

”错误“

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Label;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JTextField;

public class book extends JFrame {
     Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;


    private JFrame mdetail;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    book window = new book();
                    window.mdetail.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public book() {
        initialize();
        comboBox(kombo);
    }
    
    private void comboBox(JComboBox kombo) {
         try
            {
                con= DriverManager.getConnection("jdbc:mysql://localhost/oop_project", "root","");
                String sql = "select movieName from movie ";
                pst = con.prepareStatement(sql);
                rs = pst.executeQuery();
                while(rs.next())
                {
                    String name = rs.getString("movieName");
                    kombo.addItem(name);
                }
                
            }catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, ex);
            }
    }
    
    
    
    

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        mdetail = new JFrame();
        mdetail.setTitle("Book Ticket");
        mdetail.setBounds(100, 100, 450, 336);
        mdetail.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mdetail.getContentPane().setLayout(null);
        
        Label label = new Label("Movie Details");
        label.setForeground(Color.BLACK);
        label.setFont(new Font("Dialog", Font.PLAIN, 21));
        label.setBounds(135, 10, 184, 54);
        mdetail.getContentPane().add(label);
        
        JComboBox kombo = new JComboBox();
        kombo.setBounds(155, 70, 226, 22);
        mdetail.getContentPane().add(kombo);
        
        Label label_1 = new Label("Select Movie");
        label_1.setBounds(74, 70, 75, 22);
        mdetail.getContentPane().add(label_1);
        
        Label label_1_1 = new Label("Select Date");
        label_1_1.setBounds(74, 103, 75, 22);
        mdetail.getContentPane().add(label_1_1);
        
        JComboBox comboBox_1 = new JComboBox();
        comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"11-12-2022", "12-12-2022", "13-12-2022"}));
        comboBox_1.setBounds(155, 103, 226, 22);
        mdetail.getContentPane().add(comboBox_1);
        
        Label label_1_1_1 = new Label("Select Time");
        label_1_1_1.setBounds(74, 136, 75, 22);
        mdetail.getContentPane().add(label_1_1_1);
        
        JComboBox comboBox_1_1 = new JComboBox();
        comboBox_1_1.setModel(new DefaultComboBoxModel(new String[] {"7.00am", "10.00am", "1.00pm", "5.00pm", "8.00pm"}));
        comboBox_1_1.setBounds(155, 136, 226, 22);
        mdetail.getContentPane().add(comboBox_1_1);
        
        Label label_1_1_2 = new Label("Select Seat");
        label_1_1_2.setBounds(74, 169, 75, 22);
        mdetail.getContentPane().add(label_1_1_2);
        
        JComboBox comboBox_1_2 = new JComboBox();
        comboBox_1_2.setModel(new DefaultComboBoxModel(new String[] {"A(01)", "B(02)", "B(03)"}));
        comboBox_1_2.setBounds(155, 169, 226, 22);
        mdetail.getContentPane().add(comboBox_1_2);
        
        JButton btnNewButton = new JButton("SUBMIT");
        btnNewButton.setBounds(155, 241, 89, 23);
        mdetail.getContentPane().add(btnNewButton);
        
        JLabel lblNewLabel = new JLabel("View Seat Position");
        lblNewLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                seatposition.main(null);
                mdetail.setVisible(false);
            }
        });
        lblNewLabel.setBounds(282, 245, 112, 14);
        mdetail.getContentPane().add(lblNewLabel);
        
        Label label_1_2 = new Label("Welcome \"user\"");
        label_1_2.setBounds(10, 10, 89, 22);
        mdetail.getContentPane().add(label_1_2);
        
        Label label_1_1_2_1_1 = new Label("Number of ticket");
        label_1_1_2_1_1.setBounds(53, 202, 101, 22);
        mdetail.getContentPane().add(label_1_1_2_1_1);
        
        JComboBox comboBox_1_2_1 = new JComboBox();
        comboBox_1_2_1.setBounds(155, 202, 226, 22);
        mdetail.getContentPane().add(comboBox_1_2_1);
        
        JMenuBar menuBar = new JMenuBar();
        mdetail.setJMenuBar(menuBar);
        
        JMenu mnNewMenu = new JMenu("Option");
        menuBar.add(mnNewMenu);
        
        JMenuItem mntmNewMenuItem = new JMenuItem("Logout");
        mnNewMenu.add(mntmNewMenuItem);
        
        JMenuItem exitmenu = new JMenuItem("Exit");
        exitmenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int exit = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit", "Exit", JOptionPane.YES_NO_OPTION);
                if(exit==JOptionPane.YES_OPTION) {
                    System.exit(0);
                }else {
                    mdetail.setVisible(true);
                }
                
            }
        });
        mnNewMenu.add(exitmenu);
    }
    
}

Im bulding a GUI using eclipse plugin WindowsBuilder, im trying to pass a variable "comboBox kombo" from the initialize() method. I cant call the method to run my passed variable. The method supposed to get the data from my database to the comboBox.
Below i have attached my coding, any help will be appreciated ,Thank you!

enter image description here

error

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Label;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JTextField;

public class book extends JFrame {
     Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;


    private JFrame mdetail;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    book window = new book();
                    window.mdetail.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public book() {
        initialize();
        comboBox(kombo);
    }
    
    private void comboBox(JComboBox kombo) {
         try
            {
                con= DriverManager.getConnection("jdbc:mysql://localhost/oop_project", "root","");
                String sql = "select movieName from movie ";
                pst = con.prepareStatement(sql);
                rs = pst.executeQuery();
                while(rs.next())
                {
                    String name = rs.getString("movieName");
                    kombo.addItem(name);
                }
                
            }catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, ex);
            }
    }
    
    
    
    

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        mdetail = new JFrame();
        mdetail.setTitle("Book Ticket");
        mdetail.setBounds(100, 100, 450, 336);
        mdetail.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mdetail.getContentPane().setLayout(null);
        
        Label label = new Label("Movie Details");
        label.setForeground(Color.BLACK);
        label.setFont(new Font("Dialog", Font.PLAIN, 21));
        label.setBounds(135, 10, 184, 54);
        mdetail.getContentPane().add(label);
        
        JComboBox kombo = new JComboBox();
        kombo.setBounds(155, 70, 226, 22);
        mdetail.getContentPane().add(kombo);
        
        Label label_1 = new Label("Select Movie");
        label_1.setBounds(74, 70, 75, 22);
        mdetail.getContentPane().add(label_1);
        
        Label label_1_1 = new Label("Select Date");
        label_1_1.setBounds(74, 103, 75, 22);
        mdetail.getContentPane().add(label_1_1);
        
        JComboBox comboBox_1 = new JComboBox();
        comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"11-12-2022", "12-12-2022", "13-12-2022"}));
        comboBox_1.setBounds(155, 103, 226, 22);
        mdetail.getContentPane().add(comboBox_1);
        
        Label label_1_1_1 = new Label("Select Time");
        label_1_1_1.setBounds(74, 136, 75, 22);
        mdetail.getContentPane().add(label_1_1_1);
        
        JComboBox comboBox_1_1 = new JComboBox();
        comboBox_1_1.setModel(new DefaultComboBoxModel(new String[] {"7.00am", "10.00am", "1.00pm", "5.00pm", "8.00pm"}));
        comboBox_1_1.setBounds(155, 136, 226, 22);
        mdetail.getContentPane().add(comboBox_1_1);
        
        Label label_1_1_2 = new Label("Select Seat");
        label_1_1_2.setBounds(74, 169, 75, 22);
        mdetail.getContentPane().add(label_1_1_2);
        
        JComboBox comboBox_1_2 = new JComboBox();
        comboBox_1_2.setModel(new DefaultComboBoxModel(new String[] {"A(01)", "B(02)", "B(03)"}));
        comboBox_1_2.setBounds(155, 169, 226, 22);
        mdetail.getContentPane().add(comboBox_1_2);
        
        JButton btnNewButton = new JButton("SUBMIT");
        btnNewButton.setBounds(155, 241, 89, 23);
        mdetail.getContentPane().add(btnNewButton);
        
        JLabel lblNewLabel = new JLabel("View Seat Position");
        lblNewLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                seatposition.main(null);
                mdetail.setVisible(false);
            }
        });
        lblNewLabel.setBounds(282, 245, 112, 14);
        mdetail.getContentPane().add(lblNewLabel);
        
        Label label_1_2 = new Label("Welcome \"user\"");
        label_1_2.setBounds(10, 10, 89, 22);
        mdetail.getContentPane().add(label_1_2);
        
        Label label_1_1_2_1_1 = new Label("Number of ticket");
        label_1_1_2_1_1.setBounds(53, 202, 101, 22);
        mdetail.getContentPane().add(label_1_1_2_1_1);
        
        JComboBox comboBox_1_2_1 = new JComboBox();
        comboBox_1_2_1.setBounds(155, 202, 226, 22);
        mdetail.getContentPane().add(comboBox_1_2_1);
        
        JMenuBar menuBar = new JMenuBar();
        mdetail.setJMenuBar(menuBar);
        
        JMenu mnNewMenu = new JMenu("Option");
        menuBar.add(mnNewMenu);
        
        JMenuItem mntmNewMenuItem = new JMenuItem("Logout");
        mnNewMenu.add(mntmNewMenuItem);
        
        JMenuItem exitmenu = new JMenuItem("Exit");
        exitmenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int exit = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit", "Exit", JOptionPane.YES_NO_OPTION);
                if(exit==JOptionPane.YES_OPTION) {
                    System.exit(0);
                }else {
                    mdetail.setVisible(true);
                }
                
            }
        });
        mnNewMenu.add(exitmenu);
    }
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文