有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用SQL填充JComboBox

我目前正在做一个Java文凭课程,对于我们的项目,我们必须创建一个餐厅计算器(见截图),我已经完成了大部分GUI,但是我在SQL方面遇到了很大的困难。我以前从未使用过SQL,所以我不能100%确定该怎么做。我知道它是如何工作的,但我甚至不能运行学院用MySQL提供给我们的SQL文件。如果有任何帮助,我将不胜感激,因为我真的很难做到这一点。在过去的两周里,我已经完成了这个图形用户界面,并且一直在网上搜索帮助,但是我的大脑无法理解它

enter image description here

代码如下

import java.awt.Container;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RestaurantCalculator {

private static JTextField TotalText;
private static JTextField TaxText;
private static JTextField Table;
private static JTextField Sub;
private static JTextField WName;
private static JComboBox BevMenu;
private static JComboBox AppMenu;
private static JComboBox MainMenu;
private static JComboBox DesMenu;



public static void testFun(String myName){
    TotalText.setText(myName);
}

public static void addComponentsToPane(Container pane){
    pane.setLayout(null);

    Border border = BorderFactory.createLoweredBevelBorder();
    Border button = BorderFactory.createRaisedBevelBorder();

   JLabel Welcome = new JLabel("Restaurant");
          Welcome.setBounds(250, 5, 150, 100);
          Welcome.setFont(new Font("Arial Black", Font.BOLD, 20));


   JLabel Waiter = new JLabel("Waiter Information");
          Waiter.setBounds(100, 100, 200, 100);
          Waiter.setFont(new Font("Arial Black", Font.BOLD, 14));

   JLabel Tableno = new JLabel("Table number:");
          Tableno.setBounds(120, 130, 200, 100);
          Tableno.setFont(new Font("Arial Black", Font.BOLD, 12));


   JLabel WaiterName = new JLabel("Waiter Name:");
          WaiterName.setBounds(120, 160, 200, 100);
          WaiterName.setFont(new Font("Arial Black", Font.BOLD, 12));

    JLabel MenuItems = new JLabel("Menu Items");
           MenuItems.setBounds(100, 220, 150, 100);
           MenuItems.setFont(new Font("Arial Black", Font.BOLD, 14));

    JLabel Beverage = new JLabel("Beverage:");
           Beverage.setBounds(120, 250, 200, 100);
           Beverage.setFont(new Font("Arial Black", Font.BOLD, 12));

    JLabel Appetizer = new JLabel("Appetizer:");
           Appetizer.setBounds(120, 280, 200, 100);
           Appetizer.setFont(new Font("Arial Black", Font.BOLD, 12));

    JLabel MainCourse = new JLabel("Main Course:");
           MainCourse.setBounds(120, 310, 200, 100);
           MainCourse.setFont(new Font("Arial Black", Font.BOLD, 12));

    JLabel Dessert = new JLabel("Dessert:");
           Dessert.setBounds(120, 340, 200, 100);
           Dessert.setFont(new Font("Arial Black", Font.BOLD, 12));

    JLabel SubTotal = new JLabel("Sub Total:");
           SubTotal.setBounds(120, 440, 200, 100);
           SubTotal.setFont(new Font("Arial Black", Font.BOLD, 12));

    JLabel Tax = new JLabel("Tax:");
           Tax.setBounds(120, 470, 200, 100);
           Tax.setFont(new Font("Arial Black", Font.BOLD, 12));

    JLabel Total = new JLabel("Total:");
           Total.setBounds(120, 500, 200, 100);
           Total.setFont(new Font("Arial Black", Font.BOLD, 12));


     Table = new JTextField(10);
                 Table.setBounds(290, 171, 150, 20);
                 Table.setBorder(border);

     WName = new JTextField(11);
               WName.setBounds(290, 201, 150, 20);
               WName.setBorder(border);             

     Sub = new JTextField(10);
               Sub.setBounds(260,482,150, 20);
               Sub.setBorder(border);
               Sub.setEditable(false);

     TotalText = new JTextField(10);
               TotalText.setBounds(260,545,150, 20);
               TotalText.setBorder(border);
               TotalText.setEditable(false);

     TaxText = new JTextField(10);
               TaxText.setBounds(260,515,150, 20);
               TaxText.setBorder(border);
               TaxText.setEditable(false);

     BevMenu = new JComboBox();
               BevMenu.setBounds(290, 295, 180, 20);

     AppMenu = new JComboBox();
               AppMenu.setBounds(290, 325, 180, 20);

     MainMenu = new JComboBox();
               MainMenu.setBounds(290, 355, 180, 20);

     DesMenu = new JComboBox();
               DesMenu.setBounds(290, 385, 180, 20);


        JButton     calculateJButton;
               calculateJButton = new JButton( "Calculate Bill" );
               calculateJButton.setBounds(250,425, 170, 30);
               calculateJButton.setBorder(button);









   pane.add(Welcome);       
   pane.add(Waiter);
   pane.add(Tableno);
   pane.add(WaiterName);
   pane.add(MenuItems);
   pane.add(Beverage);
   pane.add(Appetizer);
   pane.add(MainCourse);
   pane.add(Dessert);
   pane.add(SubTotal);
   pane.add(Tax);
   pane.add(Total);
   pane.add(Table);
   pane.add(WName);
   pane.add(Sub);
   pane.add(TaxText);
   pane.add(TotalText);
   pane.add(calculateJButton);
   pane.add(BevMenu);
   pane.add(AppMenu);
   pane.add(MainMenu);
   pane.add(DesMenu);



}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Restaurant Bill Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    //Size and display the window.
    Insets insets = frame.getInsets();
    frame.setSize(600 + insets.left + insets.right,
                  700 + insets.top + insets.bottom);
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
            String theBest = "test";
            testFun(theBest);

        }
    });
}

共 (0) 个答案