2011-03-23 84 views
0

下面的第一個代碼工作,由一個添加客戶GUI組成。下面的代碼是從我創建的另一個GUI複製的,但是遇到了一些問題。在下面第一個代碼的第30行,我有一個「CREATE CUSTOMERS」部分,我想添加下面的第二個代碼。首先,我不確定我需要添加它的位置。其次,可能有一些需要添加的代碼,我不確定它在哪裏或哪裏。我已經嘗試過將這兩者結合在一起而沒有運氣。我對Java編碼非常陌生,它讓我永遠想到了這一點。嘗試組合代碼

的作品首先代碼:

import java.awt.*; 
import javax.swing.*; 

import java.awt.event.*; 
import java.util.*; 
import java.io.*; 

public class SwimCalc extends JFrame implements ActionListener{ 
private JTabbedPane jtabbedPane; 
private JPanel Customers; 


JTextArea NameTextCustomers, ExistTextCustomers, NameTextContractors, ExistTextContractors; 

public SwimCalc(){ 
setTitle("Volume Calculator"); 
setSize(300, 200); 

JPanel topPanel = new JPanel(); 
topPanel.setLayout(new BorderLayout()); 
getContentPane().add(topPanel); 

createCustomers(); 

jtabbedPane = new JTabbedPane(); 
jtabbedPane.addTab("Customers", Customers); 
topPanel.add(jtabbedPane, BorderLayout.CENTER); 
       } 

/*  CREATE CUSTOMERS  */ 

public void createCustomers(){ 
    Customers = new JPanel(); 
    Customers.setLayout(null); 

    NameTextCustomers = new JTextArea(); 
    NameTextCustomers.setBounds(10, 10, 350, 150); 
    NameTextCustomers.setLineWrap(true); 
    Customers.add(NameTextCustomers); 

    JButton Exit = new JButton("Exit"); 
    Exit.setBounds(30,170,80,20); 
    Exit.addActionListener(this); 
    Exit.setBackground(Color.white); 
    Customers.add(Exit); 

    JButton AddCustomers = new JButton("Add Customer"); 
    AddCustomers.setBounds(130,170,120,20); 
    AddCustomers.setBackground(Color.white); 
    Customers.add(AddCustomers); 

    JButton Refresh = new JButton("Refresh"); 
    Refresh.setBounds(260,170,80,20); 
    Refresh.setBackground(Color.white); 
    Customers.add(Refresh); 

    ExistTextCustomers = new JTextArea(); 
    ExistTextCustomers.setBounds(10, 200, 350, 60); 
    ExistTextCustomers.setLineWrap(true); 
    Customers.add(ExistTextCustomers); 

} 

public void actionPerformed(ActionEvent event){ 

              } 
private void Exit_pressed(){ 
System.exit(0); 
          } 

public static void main(String[] args){ 
JFrame frame = new SwimCalc(); 
frame.setSize(380, 350); 
frame.setVisible(true); 
} 
} 

現在這裏是代碼,我想添加到上面的代碼:

addCustomer.setMnemonic('a'); 

addCustomer.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 

new Customer("Customer"); 
} 
}); 
custPanel.add(custArea); 
custPanel.add(addCustomer); 

custPanel.add(custRefButton); 
custPanel.add(custMessage); 
custRefButton.setMnemonic('R'); 

custRefButton.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
custMessage.setText(""); 
try 
{ 
File custOpen = new File("customer.txt"); 
FileReader custAreaIn = new FileReader(custOpen); 
custArea.read(custAreaIn, custOpen.toString()); 
custMessage.setText("The file exists and can be read from."); 
} 
catch (IOException e3){ 
custMessage.setText("The file could not be read. " + e3.getMessage()); 
} 
} 
}); 
return custPanel; 
} 

class Customer extends JFrame 
{ 
private String[] states = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", 
"FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", 
"MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", 
"NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", 
"TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"}; 
private JComboBox StateList = new JComboBox(states); 
private JTextField NameText = new JTextField(25); 
private JTextField AddressText = new JTextField(25); 
private JTextField CityText = new JTextField(25); 
private JTextField ZipText = new JTextField(9); 
private JTextField PhoneText = new JTextField(10); 
private JTextField PopMessageText = new JTextField(30); 
private static final long serialVersionUID = 1L; 

private AddCustButtonHandler addCusHandler = new AddCustButtonHandler(); 

public Customer(String who) 
{ 
popUpWindow(who); 
} 
public void popUpWindow(final String who) { 

final JFrame popWindow; 
popWindow = new JFrame(who); 
popWindow.setSize(425, 350); 
popWindow.setLocation(100, 100); 
popWindow.setVisible(true); 
setDefaultCloseOperation(EXIT_ON_CLOSE); 

Container c = new Container(); 

popWindow.add(c); 

c.setLayout(new FlowLayout()); 

JPanel one = new JPanel(); 
JPanel two = new JPanel(); 
JPanel three = new JPanel(); 
JPanel four = new JPanel(); 
JPanel five = new JPanel(); 
JPanel six = new JPanel(); 

one.add(new JLabel(who + " Name ")); 
one.add(NameText); 
two.add(new JLabel("Address ")); 
two.add(AddressText); 
three.add(new JLabel("City ")); 
three.add(CityText); 
four.add(new JLabel("State ")); 
StateList.setSelectedIndex(0); 
four.add(StateList); 
four.add(new JLabel("ZIP")); 
four.add(ZipText); 
four.add(new JLabel("Phone")); 
four.add(PhoneText); 
JButton addwho = new JButton("Add " + who); 
addwho.setMnemonic('A'); 
JButton close = new JButton("Close"); 
close.setMnemonic('C'); 
JButton deleteFile = new JButton("Delete File"); 
deleteFile.setMnemonic('D'); 
five.add(addwho); 
five.add(close); 
five.add(deleteFile); 
PopMessageText.setEditable(false); 
PopMessageText.setHorizontalAlignment(JTextField.CENTER); 

six.add(PopMessageText); 
c.add(one); 
c.add(two); 
c.add(three); 
c.add(four); 
c.add(five); 
c.add(six); 

deleteFile.setToolTipText("Delete File"); 
addwho.setToolTipText("Add "+ who); 
close.setToolTipText("Close"); 

if (who == "Customer") 
addwho.addActionListener(addCusHandler); 
close.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
NameText.setText(""); 
AddressText.setText(""); 
CityText.setText(""); 
ZipText.setText(""); 
PhoneText.setText(""); 
PopMessageText.setText(""); 
popWindow.dispose(); 
} 
}); 
deleteFile.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
PopMessageText.setText(""); 
if (who == "Customer") { 
File file = new File("Customer.txt"); 
boolean cusFileDeleted = file.delete(); 
if (cusFileDeleted) { 
PopMessageText 
.setText("Customer file has been deleted"); 
} else { 
PopMessageText 
.setText("There was an erron in deleting file"); 
} 
} 
} 
}); 
} 
class AddCustButtonHandler implements ActionListener { 
public void actionPerformed(ActionEvent addCusHandler) { 
int StateIndex; 
try { 
File file = new File("Customer.txt"); 

boolean success = file.createNewFile(); 

if (success) { 
PopMessageText 
.setText("Customer.txt file created file added"); 
} else if (file.canWrite()) { 
PopMessageText 
.setText("Writing data to Customer.txt, file added"); 
} else { 
PopMessageText.setText("Cannot create file: Customer.txt"); 
} 
try { 
FileWriter fileW = new FileWriter("Customer.txt", true); 
fileW.write(NameText.getText()); 
fileW.write(","); 
fileW.write(AddressText.getText()); 
fileW.write(","); 
fileW.write(CityText.getText()); 
fileW.write(","); 
StateIndex = StateList.getSelectedIndex(); 
fileW.write(states[StateIndex]); 
fileW.write(","); 
fileW.write(ZipText.getText()); 
fileW.write(","); 
fileW.write(PhoneText.getText()); 
fileW.write("\r\n"); 
fileW.close(); 
PopMessageText.setText("A new Customer has been added!"); 

FileReader fileR = new FileReader("Customer.txt"); 
BufferedReader buffIn = new BufferedReader(fileR); 

String textData = buffIn.readLine(); 
buffIn.close(); 
} 
catch (IOException e1) { 
JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR", 2); 
} 
NameText.setText(""); 
AddressText.setText(""); 
CityText.setText(""); 
ZipText.setText(""); 
PhoneText.setText(""); 
} catch (IOException e1) { 
} 

} 
} 
} 
+3

你的問題歸結爲「請完成我的程序!」 ......也許你可以將你的一般查詢分解成更具體的問題來尋求幫助?你的問題到底是什麼?什麼阻止你結合你的代碼? – Arne 2011-03-23 13:59:57

+0

@Arne,我有一個單獨的圖形用戶界面,其中第二部分工作正常。然而,我在其他選項卡中還有其他問題,所以我創建了一個由客戶選項卡組成的新GUI。我嘗試將原始代碼中的所有代碼插入到新代碼中,並且由於大量錯誤而沒有運行。我相信這只是一個正確的安置和重命名問題,但我不確定。 – Mike 2011-03-23 14:03:39

+3

如果您有大量錯誤,請執行遞增操作...從正在運行的程序開始,然後逐行添加/編輯以獲取您的目標。只要參與一個程序的一部分,並將其複製到另一個程序中,而不需要太多考慮就是一個圓頂的方法。 – Arne 2011-03-23 14:07:25

回答

0

你真的需要了解Model View Controller separation。現在編寫的代碼可怕地混入商業邏輯(例如美國國家陣列)與視圖邏輯(即所有UI元素)。此外,這些類是錯誤的 - 客戶顯然不是JFrame,所以您的類不應該被稱爲客戶。它應該被命名爲CustomerFrame。其次,你的用戶界面本身需要工作;不要像這樣調用「setBounds」,而應該使用LayoutManager來處理元素的大小和位置。我建議學習MigLayout,但首先看看Layout Manager tutorials online

最後,你真的需要開始掌握你正在做的事情,並真正理解你所複製的代碼在做什麼。否則,你的行爲就像是一個Cargo cult programmer,他們只是在沒有真正理解任何代碼的情況下複製和粘貼。

做那些事情,再試一次,然後如果你仍然堅持發佈一個新的問題。