如何使用Java

时间:2015-07-30 19:59:03

标签: java eclipse

此链接将带您查看我的代码图片,程序正在运行,以及控制台中显示的内容。

http://imgur.com/OXI7W4x

我已经在控制台上写了一个测试,告诉我变量是非常正确的并加载正确的东西。哪个显然有效。唯一不能正常工作并且我100%确定代码是正确的是标签。改变标签的文字似乎不起作用,我无法弄清楚为什么。我已经尝试将它们改为静态,尝试获取和设置,我已经尝试了几个小时找到其他解决方案并且问这个问题是我的最后一招。洛尔。

编辑:希望这是足以让yall了解的代码。这是3节课。我从CoPrjGUI启动,我在文本字段中键入我要搜索的内容,当我单击“查找”时,它会填充组合框,然后单击“下载”以使用找到的一个查询填充我的主页面。然后我单击“开始”以在JobFrame的“摘要”页面上加载数据库摘要。当我这样做时,我在我的数据库中调用名为PopulateSummary的方法。现在,我知道我的代码是100%正确的原因..是因为我在JobFrame中做了同样的事情验证,以确保Aplha的代码也不是数字...如果它不正确我会改变标签和工作得很好。为什么现在不行呢?

public class Database 


           public void populateSummary()
{
    JobFrame info = new JobFrame();
    CoPrjGUI list = new CoPrjGUI();
    try
    {
    String text = list.txtField.getText();
    String query = "Select * from flooring where CustomerName like '" + text + "%'"; // Just need to adjust this to query only for search
    rs = stmt.executeQuery(query);
    if (rs.next())
    {
        String name = rs.getString("CustomerName");
        String address = rs.getString("CustomerAddress");
        String floorType = rs.getString("FlooringType");
        String floorArea = String.valueOf(rs.getDouble("FloorArea"));
        String floorCost = String.valueOf(rs.getDouble("FloorCost"));

        info.lblCustNameResult.setText(name); // Not writing to label. Why?
        info.lblAddressResult.setText(address);
        info.lblFlooringTypeResult.setText(floorType);
        info.lblSQFTResult.setText(floorArea);
        info.lblCostResult.setText(floorCost);
        String test = "Name: "+ name + "\n Address: "+ address + "\n Floor Type: "+ floorType + "\n floorArea: "
                + floorArea + "\n floorCost: " + floorCost;
        System.out.println(test);
    }


    }
    catch (Exception ex)
    {
        System.out.println(ex);
    }
}

公共类JobFrame

static JLabel lblCustNameResult = new JLabel("New label");
static JLabel lblAddressResult = new JLabel("New label");
static JLabel lblPhoneResult = new JLabel("New label");
static JLabel lblDateResult = new JLabel("New label");
static JLabel lblFlooringTypeResult = new JLabel("New label");
static JLabel lblSQFTResult = new JLabel("New label");
static JLabel lblCostResult = new JLabel("New label");
JButton btnSaveInfo = new JButton("Save Info");
static JButton btnExport = new JButton("Export");
JTabbedPane TabHolder = new JTabbedPane(JTabbedPane.TOP);




       btnSaveInfo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) 
        {
            String fullName = txtFirstName.getText() + " " + txtLastName.getText();
            String regexName = "[a-zA-Z]+";
            lblCustNameResult.setText(fullName);
            if (txtFirstName.getText().matches(regexName) || txtLastName.getText().matches(regexName))
            {
                lblCustNameResult.setText(fullName);
                EnableButton(); 
            }
            else
            {
                TabHolder.setSelectedIndex(0);
                txtFirstName.setText("Letters Only");
                txtLastName.setText("Letters Only");
                DisableButton();
            }

            String Address = txtStreetAddress.getText() + " " + txtCity.getText()
            + "\n" + " " + txtZip.getText() + " " + txtState.getText();
            lblAddressResult.setText(Address);
            /* //Need Individual Errors for each Text Box
            String regexAdress = "^[a-zA-Z0-9]+$";
            if (Address.equals(Address))
            {
                lblAddressResult.setText(Address);
                EnableButton();
            }
            else
            {
                TabHolder.setSelectedIndex(0);
                txtStreetAddress.setText("Example: 123 John Street ");
                txtCity.setText("Example: Dallas");
                txtZip.setText("Example: 75063");
                txtState.setText("Example: Texas");
                DisableButton();
            }
            */
            if (txtPhone.getText().matches("\\d{3}-\\d{3}-\\d{4}"))
            {
                lblPhoneResult.setText(txtPhone.getText());
                EnableButton();
            }
            else 
            {
                txtPhone.setText("Valid Phone Format: 123-456-7890");
                TabHolder.setSelectedIndex(0);
                DisableButton();
            }

            String date = txtDate.getText();
            if (date.matches("\\d{4}/\\d{2}/\\d{2}"))
            {
                lblDateResult.setText(txtDate.getText());
                EnableButton();
            }
            else
            {
                TabHolder.setSelectedIndex(0);
                txtDate.setText("yyyy/dd/mm");
                DisableButton();
            }

            if (fullName != "New Label" && fullName != "Letters Only")
            {
                if (Address != "New LabeL")
                {
                    if (txtPhone.getText() !=  "New Label" && txtPhone.getText().matches("\\d{3}-\\d{3}-\\d{4}"))
                    {
                        if (date != "New Label" && date != "yyyy/dd/mm")
                        {
                            EnableButton();
                        }
                        else
                            DisableButton();
                    }
                }
            }
        }
    });

}

公共类CoPrjGUI

    JButton btnFind = new JButton("Find");
    btnFind.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0) 
        {
            try {
                connect.ConnectToMyDB();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
            }
            connect.getData();
            for (Object item : connect.listInfo)
            {
                lstSearchResults.addItem(item);
            }
        }
    });

    JButton btnDownload = new JButton("Download");
    btnDownload.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {

            for (Object item : connect.listInfo)
            {
                cmbDownloads.addItem(item);
            }
            tabbedPane.setSelectedIndex(0);
        }
    });


  JButton btnStart = new JButton("Start");
    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) 
        {
            HomeFrame.setVisible(false);
            JobFrame CustFrame = new JobFrame();
            CustFrame.CustomerFrame.setVisible(true);
            CustFrame.setIndex();
            CustFrame.DisableButton(); 
            connect.populateSummary();
            txtField.setText("");
        }
    });

1 个答案:

答案 0 :(得分:0)

我的问题的解决方案:必须再次调用ConnectToMyDB()并且它工作正常..