WindowBuilder源代码布局和数据库实现

时间:2015-04-21 18:06:01

标签: java database layout windowbuilder

您好我是一名新手java程序员,我目前正在eclipse中使用WindowBuilder创建一个java gui应用程序。

我有我喜欢的布局,源代码都在一个类中,我似乎无法正确浏览面板。 WindowBuilder中面板布局的图像链接 - http://postimg.org/image/xmnm9ilm1/

我想知道如何才能最好地布局代码,以及如何最好地浏览程序。 请记住,我不知道很多技术术语。

实现微软访问后端数据库的最佳方法是什么,可以在程序中编辑登录用户名,密码以及添加和删除项目?我目前有一个单独的数据库类。

谢谢

程序源代码如下所示

import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;

import java.awt.CardLayout;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;
import javax.swing.JList;
import javax.swing.Box;
import java.awt.Panel;
import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class UnivEPOS {

private JFrame frmUniveposV;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;

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

/**
 * Create the application.
 */
public UnivEPOS() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmUniveposV = new JFrame();
    frmUniveposV.setTitle("UnivEPOS v1.0");
    frmUniveposV.setSize(Toolkit.getDefaultToolkit().getScreenSize());
    frmUniveposV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmUniveposV.getContentPane().setLayout(new CardLayout(0, 0));

    JPanel Login = new JPanel();
    Login.setBackground(new Color(128, 0, 0));
    frmUniveposV.getContentPane().add(Login, "name_244778779455709");
    Login.setLayout(null);

    JLabel lblLogin = new JLabel("LOGIN");
    lblLogin.setForeground(new Color(255, 255, 255));
    lblLogin.setFont(new Font("Tahoma", Font.BOLD, 18));
    lblLogin.setBounds(10, 0, 60, 50);
    Login.add(lblLogin);

    textField = new JTextField();
    textField.setBounds(518, 282, 262, 20);
    Login.add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setColumns(10);
    textField_1.setBounds(518, 319, 262, 20);
    Login.add(textField_1);

    JLabel lblNewLabel = new JLabel("Username");
    lblNewLabel.setForeground(new Color(255, 255, 255));
    lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
    lblNewLabel.setBounds(416, 283, 92, 14);
    Login.add(lblNewLabel);

    JLabel lblPassword = new JLabel("Password");
    lblPassword.setForeground(new Color(255, 255, 255));
    lblPassword.setFont(new Font("Tahoma", Font.BOLD, 16));
    lblPassword.setBounds(416, 320, 92, 14);
    Login.add(lblPassword);

    JButton btnNewButton = new JButton("ENTER");
    btnNewButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {


    }

    });
    btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnNewButton.setBounds(691, 396, 89, 23);
    Login.add(btnNewButton);

    JButton btnNewButton_1 = new JButton("EXIT");
    btnNewButton_1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {

        }

    });
    btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnNewButton_1.setBounds(518, 396, 89, 23);
    Login.add(btnNewButton_1);

    JPanel Home = new JPanel();
    Home.setBackground(new Color(128, 0, 0));
    frmUniveposV.getContentPane().add(Home, "name_244789265918213");
    Home.setLayout(null);

    JLabel lblCurrentOrders = new JLabel("CURRENT ORDERS");
    lblCurrentOrders.setForeground(new Color(255, 255, 255));
    lblCurrentOrders.setBounds(535, 0, 346, 31);
    lblCurrentOrders.setFont(new Font("Tahoma", Font.BOLD, 35));
    Home.add(lblCurrentOrders);

    JButton btnNewButton_2 = new JButton("Previous");
    btnNewButton_2.setBounds(614, 598, 95, 31);
    Home.add(btnNewButton_2);

    JButton btnNewButton_3 = new JButton("Next");
    btnNewButton_3.setBounds(731, 598, 95, 31);
    Home.add(btnNewButton_3);

    JButton btnNewButton_6 = new JButton("NEW ORDER");
    btnNewButton_6.setBounds(10, 598, 244, 112);
    btnNewButton_6.setFont(new Font("Tahoma", Font.BOLD, 18));
    Home.add(btnNewButton_6);

    JButton btnNewButton_4 = new JButton("Show Paid");
    btnNewButton_4.setBounds(614, 632, 95, 31);
    Home.add(btnNewButton_4);

    JButton btnNewButton_5 = new JButton("Show Unpaid");
    btnNewButton_5.setBounds(731, 632, 95, 31);
    Home.add(btnNewButton_5);

    JButton btnShowAll = new JButton("SHOW ALL");
    btnShowAll.setBounds(658, 663, 131, 31);
    Home.add(btnShowAll);

    JButton btnAdmin = new JButton("ADMIN");
    btnAdmin.setBounds(1245, 682, 95, 37);
    Home.add(btnAdmin);

    JLabel lblCurrentUser = new JLabel("Current User:");
    lblCurrentUser.setForeground(new Color(255, 255, 255));
    lblCurrentUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblCurrentUser.setBounds(1081, 12, 83, 14);
    Home.add(lblCurrentUser);

    JLabel lblNewLabel_1 = new JLabel("Date & Time:");
    lblNewLabel_1.setForeground(new Color(255, 255, 255));
    lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblNewLabel_1.setBounds(10, 14, 80, 14);
    Home.add(lblNewLabel_1);

    JPanel Order = new JPanel();
    Order.setBackground(new Color(128, 0, 0));
    frmUniveposV.getContentPane().add(Order, "name_244796279334447");
    Order.setLayout(null);

    JLabel lblOrder = new JLabel("ORDER:");
    lblOrder.setForeground(new Color(255, 255, 255));
    lblOrder.setFont(new Font("Tahoma", Font.BOLD, 40));
    lblOrder.setBounds(10, 11, 166, 43);
    Order.add(lblOrder);

    JLabel lblDateTime = new JLabel("DATE & TIME:");
    lblDateTime.setForeground(new Color(255, 255, 255));
    lblDateTime.setFont(new Font("Tahoma", Font.BOLD, 40));
    lblDateTime.setBounds(771, 11, 282, 43);
    Order.add(lblDateTime);

    JLabel lblTable = new JLabel("TABLE:");
    lblTable.setForeground(new Color(255, 255, 255));
    lblTable.setFont(new Font("Tahoma", Font.BOLD, 40));
    lblTable.setBounds(391, 11, 146, 43);
    Order.add(lblTable);

    Panel panel = new Panel();
    panel.setBounds(10, 75, 650, 489);
    Order.add(panel);
    panel.setLayout(null);

    JLabel lblNewLabel_2 = new JLabel("TOTAL:");
    lblNewLabel_2.setForeground(new Color(255, 255, 255));
    lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 40));
    lblNewLabel_2.setBounds(10, 421, 157, 57);
    panel.add(lblNewLabel_2);

    JLabel lblCurrentOrder = new JLabel("CURRENT ORDER");
    lblCurrentOrder.setForeground(new Color(255, 255, 255));
    lblCurrentOrder.setBounds(251, 5, 198, 31);
    lblCurrentOrder.setFont(new Font("Tahoma", Font.PLAIN, 25));
    panel.add(lblCurrentOrder);

    Panel panel_1 = new Panel();
    panel_1.setBounds(690, 75, 650, 489);
    Order.add(panel_1);
    panel_1.setLayout(null);

    JLabel lblCategories = new JLabel("CATEGORIES");
    lblCategories.setForeground(new Color(255, 255, 255));
    lblCategories.setFont(new Font("Tahoma", Font.PLAIN, 25));
    lblCategories.setBounds(254, 11, 153, 31);
    panel_1.add(lblCategories);

    JButton btnPrevious = new JButton("PREVIOUS");
    btnPrevious.setBounds(462, 455, 89, 23);
    panel_1.add(btnPrevious);

    JButton btnNext = new JButton("NEXT");
    btnNext.setBounds(551, 455, 89, 23);
    panel_1.add(btnNext);

    JButton btnNewButton_7 = new JButton("SAVE");
    btnNewButton_7.setFont(new Font("Tahoma", Font.BOLD, 30));
    btnNewButton_7.setBounds(10, 582, 254, 137);
    Order.add(btnNewButton_7);

    JButton btnSavePay = new JButton("SAVE & PAY");
    btnSavePay.setFont(new Font("Tahoma", Font.BOLD, 30));
    btnSavePay.setBounds(274, 582, 274, 137);
    Order.add(btnSavePay);

    JButton btnFinish = new JButton("FINISH");
    btnFinish.setFont(new Font("Tahoma", Font.BOLD, 30));
    btnFinish.setBounds(558, 582, 253, 137);
    Order.add(btnFinish);

    JButton button = new JButton("ADMIN");
    button.setFont(new Font("Tahoma", Font.BOLD, 11));
    button.setBounds(1245, 682, 95, 37);
    Order.add(button);

    JButton btnExit = new JButton("EXIT");
    btnExit.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnExit.setBounds(1114, 682, 95, 37);
    Order.add(btnExit);

    JPanel Reports = new JPanel();
    Reports.setBackground(new Color(128, 0, 0));
    frmUniveposV.getContentPane().add(Reports, "name_244798869260892");
    Reports.setLayout(null);

    Panel panel_2 = new Panel();
    panel_2.setBounds(0, 180, 1350, 459);
    Reports.add(panel_2);
    panel_2.setLayout(null);

    JLabel lblSales = new JLabel("SALES");
    lblSales.setForeground(new Color(255, 255, 255));
    lblSales.setFont(new Font("Tahoma", Font.PLAIN, 25));
    lblSales.setBounds(600, 0, 78, 52);
    panel_2.add(lblSales);

    JLabel lblReports = new JLabel("REPORTS");
    lblReports.setForeground(new Color(255, 255, 255));
    lblReports.setFont(new Font("Tahoma", Font.BOLD, 35));
    lblReports.setBounds(556, 0, 174, 52);
    Reports.add(lblReports);

    JButton btnNewButton_8 = new JButton("SALES");
    btnNewButton_8.setFont(new Font("Tahoma", Font.PLAIN, 25));
    btnNewButton_8.setBounds(10, 97, 174, 77);
    Reports.add(btnNewButton_8);

    JButton btnCancellations = new JButton("CANCELLATIONS");
    btnCancellations.setFont(new Font("Tahoma", Font.PLAIN, 25));
    btnCancellations.setBounds(194, 97, 221, 77);
    Reports.add(btnCancellations);

    JButton btnItemsSold = new JButton("ITEMS SOLD");
    btnItemsSold.setFont(new Font("Tahoma", Font.PLAIN, 25));
    btnItemsSold.setBounds(425, 97, 221, 77);
    Reports.add(btnItemsSold);

    JLabel lblNewLabel_3 = new JLabel("PAGE");
    lblNewLabel_3.setForeground(new Color(255, 255, 255));
    lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 40));
    lblNewLabel_3.setBounds(610, 645, 106, 74);
    Reports.add(lblNewLabel_3);

    JButton btnNewButton_9 = new JButton("PREVIOUS");
    btnNewButton_9.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnNewButton_9.setBounds(860, 645, 163, 74);
    Reports.add(btnNewButton_9);

    JButton btnNext_1 = new JButton("NEXT");
    btnNext_1.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnNext_1.setBounds(1025, 645, 164, 74);
    Reports.add(btnNext_1);

    JButton btnNewButton_10 = new JButton("EXIT");
    btnNewButton_10.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnNewButton_10.setBounds(1199, 645, 141, 74);
    Reports.add(btnNewButton_10);

    JPanel Settings = new JPanel();
    Settings.setBackground(new Color(128, 0, 0));
    frmUniveposV.getContentPane().add(Settings, "name_244813941995636");
    Settings.setLayout(null);

    JLabel lblNewLabel_4 = new JLabel("SYSTEM SETTINGS");
    lblNewLabel_4.setForeground(new Color(255, 255, 255));
    lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 35));
    lblNewLabel_4.setBounds(500, 0, 339, 54);
    Settings.add(lblNewLabel_4);

    JList list = new JList();
    list.setForeground(new Color(255, 255, 255));
    list.setBackground(new Color(128, 0, 0));
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setFont(new Font("Tahoma", Font.PLAIN, 40));
    list.setModel(new AbstractListModel() {
        String[] values = new String[] {"USER 1", "USER 2", "USER 3", "USER 4", "USER 5"};
        public int getSize() {
            return values.length;
        }
        public Object getElementAt(int index) {
            return values[index];
        }
    });
    list.setBounds(10, 82, 634, 481);
    Settings.add(list);

    JButton btnNewButton_11 = new JButton("ADD USER");
    btnNewButton_11.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnNewButton_11.setBounds(10, 617, 196, 102);
    Settings.add(btnNewButton_11);

    JButton btnChangeSuperuserPassword = new JButton("CHANGE SUPERUSER PASSWORD");
    btnChangeSuperuserPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnChangeSuperuserPassword.setBounds(433, 617, 211, 102);
    Settings.add(btnChangeSuperuserPassword);

    JButton button_1 = new JButton("EXIT");
    button_1.setFont(new Font("Tahoma", Font.BOLD, 11));
    button_1.setBounds(1245, 682, 95, 37);
    Settings.add(button_1);

    Panel panel_3 = new Panel();
    panel_3.setBounds(650, 84, 690, 479);
    Settings.add(panel_3);
    panel_3.setLayout(null);

    JLabel lblNewLabel_5 = new JLabel("RECEIPT OF SALE");
    lblNewLabel_5.setForeground(new Color(255, 255, 255));
    lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblNewLabel_5.setBounds(260, 11, 178, 44);
    panel_3.add(lblNewLabel_5);

    JLabel lblNewLabel_6 = new JLabel("COMPANY");
    lblNewLabel_6.setForeground(new Color(255, 255, 255));
    lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 22));
    lblNewLabel_6.setBounds(24, 136, 125, 44);
    panel_3.add(lblNewLabel_6);

    JLabel lblSalesRepresentative = new JLabel("SALES REP");
    lblSalesRepresentative.setForeground(new Color(255, 255, 255));
    lblSalesRepresentative.setFont(new Font("Tahoma", Font.PLAIN, 22));
    lblSalesRepresentative.setBounds(24, 213, 125, 44);
    panel_3.add(lblSalesRepresentative);

    JLabel lblAddress = new JLabel("ADDRESS");
    lblAddress.setForeground(new Color(255, 255, 255));
    lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 22));
    lblAddress.setBounds(24, 171, 125, 48);
    panel_3.add(lblAddress);

    JLabel lblMessage = new JLabel("MESSAGE");
    lblMessage.setForeground(new Color(255, 255, 255));
    lblMessage.setFont(new Font("Tahoma", Font.PLAIN, 22));
    lblMessage.setBounds(24, 256, 125, 38);
    panel_3.add(lblMessage);

    textField_2 = new JTextField();
    textField_2.setBounds(154, 151, 404, 26);
    panel_3.add(textField_2);
    textField_2.setColumns(10);

    textField_3 = new JTextField();
    textField_3.setColumns(10);
    textField_3.setBounds(154, 188, 404, 26);
    panel_3.add(textField_3);

    textField_4 = new JTextField();
    textField_4.setColumns(10);
    textField_4.setBounds(154, 228, 404, 26);
    panel_3.add(textField_4);

    textField_5 = new JTextField();
    textField_5.setColumns(10);
    textField_5.setBounds(154, 268, 404, 26);
    panel_3.add(textField_5);

    JPanel Administration = new JPanel();
    Administration.setBackground(new Color(128, 0, 0));
    frmUniveposV.getContentPane().add(Administration, "name_244816080502214");
    Administration.setLayout(null);

    JLabel lblNewLabel_7 = new JLabel("ADMINISTRATION");
    lblNewLabel_7.setForeground(new Color(255, 255, 255));
    lblNewLabel_7.setFont(new Font("Tahoma", Font.BOLD, 35));
    lblNewLabel_7.setBounds(506, 0, 335, 43);
    Administration.add(lblNewLabel_7);

    JLabel lblCategories_1 = new JLabel("CATEGORIES");
    lblCategories_1.setForeground(new Color(255, 255, 255));
    lblCategories_1.setFont(new Font("Tahoma", Font.PLAIN, 35));
    lblCategories_1.setBounds(90, 88, 214, 43);
    Administration.add(lblCategories_1);

    JLabel lblSubcategories = new JLabel("SUB-CATEGORIES");
    lblSubcategories.setForeground(new Color(255, 255, 255));
    lblSubcategories.setFont(new Font("Tahoma", Font.PLAIN, 35));
    lblSubcategories.setBounds(528, 88, 292, 43);
    Administration.add(lblSubcategories);

    JLabel label = new JLabel("SUB-CATEGORIES");
    label.setForeground(new Color(255, 255, 255));
    label.setFont(new Font("Tahoma", Font.PLAIN, 35));
    label.setBounds(1008, 88, 292, 43);
    Administration.add(label);

    JList list_1 = new JList();
    list_1.setBounds(55, 142, 292, 404);
    Administration.add(list_1);

    JList list_2 = new JList();
    list_2.setBounds(528, 142, 292, 404);
    Administration.add(list_2);

    JList list_3 = new JList();
    list_3.setBounds(1008, 142, 292, 404);
    Administration.add(list_3);

    JButton btnAdd = new JButton("ADD");
    btnAdd.setBounds(55, 557, 89, 23);
    Administration.add(btnAdd);

    JButton button_2 = new JButton("ADD");
    button_2.setBounds(528, 557, 89, 23);
    Administration.add(button_2);

    JButton button_3 = new JButton("ADD");
    button_3.setBounds(1008, 557, 89, 23);
    Administration.add(button_3);

    JButton btnRemove = new JButton("REMOVE");
    btnRemove.setBounds(258, 557, 89, 23);
    Administration.add(btnRemove);

    JButton button_4 = new JButton("REMOVE");
    button_4.setBounds(731, 557, 89, 23);
    Administration.add(button_4);

    JButton button_5 = new JButton("REMOVE");
    button_5.setBounds(1211, 557, 89, 23);
    Administration.add(button_5);

    JButton btnReports = new JButton("REPORTS");
    btnReports.setFont(new Font("Tahoma", Font.BOLD, 30));
    btnReports.setBounds(10, 638, 180, 81);
    Administration.add(btnReports);

    JButton btnSettings = new JButton("SETTINGS");
    btnSettings.setFont(new Font("Tahoma", Font.BOLD, 30));
    btnSettings.setBounds(200, 638, 185, 81);
    Administration.add(btnSettings);

    JButton button_6 = new JButton("EXIT");
    button_6.setFont(new Font("Tahoma", Font.BOLD, 11));
    button_6.setBounds(1245, 682, 95, 37);
    Administration.add(button_6);
}
}

数据库类的源代码如下所示

import java.awt.event.*;
import java.sql.*;

public class database {

Connection con;
Statement st;
ResultSet rs;

public database()
{
    connect();
    frame();

}
public void connect()
{

    try{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db = "jdbc:odbc:db";
con = DriverManager.getConnection(db);
st = con.createStatement();

    }

    catch(Exception ex){

    }

}



public void frame()
{

new UnivEPOS(); 
}


}

0 个答案:

没有答案