2017-08-13 60 views
1

這個格式化JTextField的是GUI設計類的代碼和下面是提供功能給程序的類。我試圖從文本字段獲取用戶輸入,所以我可以使用clearAll方法刪除文本,並使用saveit方法保存用戶輸入。我嘗試使用nameEntry.setText(「」);在clearAll方法,但它不會工作有人可以幫助我!使用另一個類

//Import Statements 
import javax.swing.*; 
import java.awt.*; 
import javax.swing.JOptionPane; 
import java.awt.event.*; 


//Class Name 
public class Customer extends JFrame { 
    Function fun = new Function(); 

    public static void main(String[]args){ 
     Customer.setLookAndFeel(); 
     Customer cust = new Customer(); 
    } 


    public Customer(){ 
     super("Resident Details"); 
     setSize(500,500); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     FlowLayout two = new FlowLayout(FlowLayout.LEFT); 
     setLayout(two); 


     JPanel row1 = new JPanel(); 
     JLabel name = new JLabel("First Name",JLabel.LEFT); 
     JTextField nameEntry = new JTextField("",20); 
     row1.add(name); 
     row1.add(nameEntry); 
     add(row1); 

     JPanel row2 = new JPanel(); 
     JLabel surname = new JLabel("Surname ",JLabel.LEFT); 
     JTextField surnameEntry = new JTextField("",20); 
     row2.add(surname); 
     row2.add(surnameEntry); 
     add(row2); 

     JPanel row3 = new JPanel(); 
     JLabel contact1 = new JLabel("Contact Details : Email     ",JLabel.LEFT); 
     JTextField contact1Entry = new JTextField("",10); 
     FlowLayout newflow = new FlowLayout(FlowLayout.LEFT,10,30); 
     setLayout(newflow); 
     row3.add(contact1); 
     row3.add(contact1Entry); 
     add(row3); 

     JPanel row4 = new JPanel(); 
     JLabel contact2 = new JLabel("Contact Details : Phone Number",JLabel.LEFT); 
     JTextField contact2Entry = new JTextField("",10); 
     row4.add(contact2); 
     row4.add(contact2Entry); 
     add(row4); 

     JPanel row5 = new JPanel(); 
     JLabel time = new JLabel("Duration Of Stay        ",JLabel.LEFT); 
     JTextField timeEntry = new JTextField("",10); 
     row5.add(time); 
     row5.add(timeEntry); 
     add(row5); 


     JPanel row6 = new JPanel(); 
     JComboBox<String> type = new JComboBox<String>(); 
     type.addItem("Type Of Room"); 
     type.addItem("Single Room"); 
     type.addItem("Double Room"); 
     type.addItem("VIP Room"); 
     row6.add(type); 
     add(row6); 

     JPanel row7 = new JPanel(); 
     FlowLayout amt = new FlowLayout(FlowLayout.LEFT,100,10); 
     setLayout(amt); 
     JLabel amount = new JLabel("Amount Per Day        "); 
     JTextField AmountField = new JTextField("\u20ac ",10); 
     row7.add(amount); 
     row7.add(AmountField); 
     add(row7); 

     JPanel row8 = new JPanel(); 
     FlowLayout prc = new FlowLayout(FlowLayout.LEFT,100,10); 
     setLayout(prc); 
     JLabel price = new JLabel("Total Price           "); 
     JTextField priceField = new JTextField("\u20ac ",10); 
     row8.add(price); 
     row8.add(priceField); 
     add(row8); 

     JPanel row9 = new JPanel(); 
     JButton clear = new JButton("Clear"); 
     row9.add(clear); 
     add(row9); 

     JPanel row10 = new JPanel(); 
     JButton save = new JButton("Save"); 
     save.addActionListener(fun); 
     row10.add(save); 
     add(row10); 

     //Adding ActionListners 
     nameEntry.addActionListener(fun); 
     clear.addActionListener(fun); 
     save.addActionListener(fun); 

     setVisible(true); 
    } 

    private static void setLookAndFeel() { 
      try { 
       UIManager.setLookAndFeel(
        "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" 
       ); 
      } catch (Exception exc) { 
      // ignore error 
       } 
    } 

} 

//Import Statements 
import javax.swing.*; 
import java.awt.*; 
import java.awt.Color; 
import javax.swing.JOptionPane; 
import java.awt.event.*; 


//Class Name 
public class Function implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 
     String command = e.getActionCommand(); 
     if(command.equals("Add Customer")) { 
      Login(); 
     } 
     else if(command.equals("Register")){ 
      Registration(); 
     } 
     else if(command.equals("Exit")){ 
      System.exit(0); 
     }  
     else if(command.equals("Clear")){ 
      ClearAllFields(); 
     } 
     else if(command.equals("Save")){ 
      SaveIt(); 
     } 
    } 

    public static void Login(){ 
     Customer cust = new Customer(); 
    } 

    public static void Registration(){ 
     //Do Nothing 

    } 
    //This clears all the text from the JTextFields 
    public static void ClearAllFields(){ 

    } 

    //This will save Info on to another Class 
    public static void SaveIt(){ 


    } 


} 
+1

請詳細說明您的意思是'什麼「但它不會工作」。它怎麼不起作用?它是否編譯?跑?它是否會拋出異常?請給我們任何和所有的信息,這將有助於我們更好地理解你的代碼和你的問題。 –

+0

請關注創建[mcve]。另外,你可以包含clearAll方法和你從哪個類中調用它? –

+0

它不會編譯,它給出了一個錯誤,說「找不到符號名稱。實體」 –

回答

1

或者,您也可以使nameEntry已知功能類通過調用函數的構造之前定義它,然後傳遞到構造函數,如:

JTextField nameEntry = new JTextField("",20); 

Function fun = new Function(nameEntry); 

然後,在功能,添加nameEntry作爲函數的成員變量,使一個構造,它接受nameEntry,功能(右後「公共類功能...」線),如:

JTextField nameEntry; 

public Function(JTextField nameEntry) { 
    this.nameEntry = nameEntry; 
} 

現在,下面將編譯:

public void ClearAllFields(){ 
    nameEntry.setText(""); 
} 

而且,清除按鈕將清除名稱字段。

+0

感謝查爾斯它最後的作品我很欣賞的幫助下,氣墊船全鱔感謝你的幫助太 –

1

再次按註釋,一個簡單的方法來解決,這是給控制器(收聽者)可以調用GUI公共方法,然後通過當前顯示的實例將GUI導入偵聽器,允許它調用GUI可能具有的任何公共方法。下面的代碼是比你更簡單的,僅具有一個JTextField的,但它足以說明這一點:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import javax.swing.*; 

public class GUI extends JPanel { 
    private JTextField textField = new JTextField(10); 
    private JButton clearButton = new JButton("Clear"); 

    public GUI() { 
     // pass **this** into the listener class 
     MyListener myListener = new MyListener(this); 
     clearButton.addActionListener(myListener); 
     clearButton.setMnemonic(KeyEvent.VK_C); 

     add(textField); 
     add(clearButton); 
    } 

    // public method in GUI that will do the dirty work 
    public void clearAll() { 
     textField.setText(""); 
    } 

    // other public methods here to get text from the JTextFields 
    // to set text, and do whatever else needs to be done 

    private static void createAndShowGui() { 
     GUI mainPanel = new GUI(); 

     JFrame frame = new JFrame("GUI"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

class MyListener implements ActionListener { 
    private GUI gui; 

    // use constructor parameter to set a field 
    public MyListener(GUI gui) { 
     this.gui = gui; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     gui.clearAll(); // call public method in field 
    } 
} 

一個更好和更可靠的解決方案是一個模型 - 來構建程序視圖 - 控制器的時尚(看這個),但這可能是這個簡單的學術活動矯枉過正,你正在做。