2011-03-21 54 views
0

我有兩個文本框和我不知道如何或在哪裏插入行代碼時,它的點擊在清除文本框。文本框時獲得焦點

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

public class OfficeAreaCalculator extends JFrame implements ActionListener{ 
    private JTabbedPane jtabbedPane; 
    private JPanel calculator; 


    JTextField lengthText, widthText, areaText; 

    public OfficeAreaCalculator(){ 
    setTitle("Office Area Calculator"); 


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

    createcalculator(); 

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


    public void createcalculator(){ 
    calculator = new JPanel(); 
    calculator.setLayout(null); 
    JLabel lengthLabel = new JLabel("Enter the length of the office:"); 
    lengthLabel.setBounds(12, 15, 260, 20); 
    calculator.add(lengthLabel); 
    lengthText = new JTextField(); 
    lengthText.setBounds(180, 15, 80, 20); 
    calculator.add(lengthText); 
    JLabel widthLabel = new JLabel("Enter the width of the office:"); 
    widthLabel.setBounds(15, 40, 260, 20); 
    calculator.add(widthLabel); 
    widthText = new JTextField(); 
    widthText.setBounds(180, 40, 80, 20); 
    calculator.add(widthText); 

    JLabel areaLabel = new JLabel("Area of the Office is:"); 
    areaLabel.setBounds(30, 70, 260, 20); 
    calculator.add(areaLabel); 
    areaText = new JTextField(); 
    areaText.setBounds(150, 70, 120, 20); 
    areaText.setEditable(false); 
    calculator.add(areaText); 

    JButton calcarea = new JButton("Calculate"); 
    calcarea.setBounds(20,110,110,20); 
    calcarea.addActionListener(this); 
    calcarea.setBackground(Color.white); 
    calculator.add(calcarea); 

    JButton Close = new JButton("Close"); 
    Close.setBounds(150,110,80,20); 
    Close.addActionListener(this); 
    Close.setBackground(Color.white); 
    calculator.add(Close); 
    } 


    public void actionPerformed(ActionEvent event){ 
    JButton button = (JButton)event.getSource(); 
    String buttonLabel = button.getText(); 
    if ("Close".equalsIgnoreCase(buttonLabel)){ 
     Exit_pressed(); return; 
    } 
    if ("Calculate".equalsIgnoreCase(buttonLabel)){ 
     Calculate_area(); return; 
    } 

    } 

    private void Exit_pressed(){ 
    System.exit(0); 
    } 
    private void Calculate_area(){ 
    String lengthString, widthString; 
    int length=0; 
    int width=0; 
    lengthString = lengthText.getText(); 
    widthString = widthText.getText(); 
    if (lengthString.length() < 1 || widthString.length() < 1){ 
     areaText.setText("Enter All Numbers"); return; 
    } 
    length = Integer.parseInt(lengthString); 
    width = Integer.parseInt(widthString); 
    if (length != 0 || width != 0){ 
     areaText.setText((length * width) + ""); 
    } else{ 
     areaText.setText("Enter All Numbers"); return; 
    } 
    } 

    public static void main(String[] args){ 
    JFrame frame = new OfficeAreaCalculator(); 
    frame.setSize(300, 200); 
    frame.setVisible(true); 
    } 
} 

回答

2

每個文本框,你可以像這樣創建的FocusListener:

JTextField textfield = new JTextField(); 
     textfield.addFocusListener(new FocusListener() { 

      @Override 
      public void focusLost(FocusEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void focusGained(FocusEvent arg0) { 
       // TODO Auto-generated method stub 
       textfield.setText(""); 
      } 
     }); 
1

用途:

lengthText = new JTextField(); 
lengthText.setBounds(180, 15, 80, 20); 
lengthText.addFocusListener(new FocusListener(){ 
    public void focusGained(FocusEvent e) { 
     lengthText.setText(""); 
    } 
    public void focusLost(FocusEvent e) { 
    } 
}); 
1

我認爲這是性感向全選,而不是將文本「」,因爲用戶不鬆開之前的值直到她按下一個新數字。

lengthText = new JTextField(); 
    lengthText.setBounds(180, 15, 80, 20); 
    lengthText.addFocusListener(new FocusListener() { 

     @Override 
     public void focusLost(FocusEvent arg0) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void focusGained(FocusEvent arg0) { 
      // TODO Auto-generated method stub 
      lengthText.selectAll(); 
     } 
    }); 
相關問題