2013-04-21 48 views
0

我認爲我的錯誤是我的showGreeting()方法。具體在嘗試。當用戶按下Get Greeting按鈕時,我想讓它顯示一個對話框什麼是「Greetings,FirstName + MI + LastName」而當用戶離開字段空白時,我創建出現錯誤,工作正常,但我確實不明白爲什麼我的正常問候不起作用。爲什麼我得到這個錯誤:java.lang.OutOfMemoryError:無法創建離屏表面

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


public class GreetingApp extends JFrame { 
    private static final long serialVersionUID = 1L; 
    private final JTextField firstNameField,middleNameField,lastNameField; 
    private final JButton greetingButton; 
    public GreetingApp() { 
     super("Greetings"); 
     this.firstNameField = new JTextField(8); 
     this.middleNameField = new JTextField(8); 
     this.lastNameField = new JTextField(8); 
     this.greetingButton = new JButton("Get Greeting"); 
     greetingButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(final ActionEvent ae){ 
       showGreeting(); 
      } 
     }); 
     final Container mainPanel = getContentPane(); 
     mainPanel.setLayout(new BorderLayout()); 
     final JPanel inputPanel = new JPanel(); 
     final JPanel buttonPanel = new JPanel(); 
     inputPanel.setLayout(new GridLayout(3,3,2,2)); 
     buttonPanel.setLayout(new BorderLayout()); 
     JSeparator sep = new JSeparator(); 
     inputPanel.add(new JLabel("First Name: "),JLabel.LEFT_ALIGNMENT); 
     inputPanel.add(firstNameField); 
     inputPanel.add(new JLabel("MI: "),JLabel.LEFT_ALIGNMENT); 
     inputPanel.add(middleNameField); 
     inputPanel.add(new JLabel("Last Name: "),JLabel.LEFT_ALIGNMENT); 
     inputPanel.add(lastNameField); 
     mainPanel.add(inputPanel,BorderLayout.PAGE_START); 
     buttonPanel.add(sep,BorderLayout.PAGE_START); 
     buttonPanel.add(greetingButton); 
     mainPanel.add(buttonPanel,BorderLayout.PAGE_END); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 
    private String getFullName() throws IllegalStateException{ 
     if(firstNameField.getText().trim().length() == 0){ 
      throw new IllegalArgumentException("First name cannot be blank"); 
     } 
     if(middleNameField.getText().trim().length() == 0){ 
      throw new IllegalArgumentException("Middle name cannot be blank"); 
     } 
     if(lastNameField.getText().trim().length() == 0){ 
      throw new IllegalArgumentException("Last name cannot be blank"); 
     } 
     return "Greetings, "+this.firstNameField+" "+ this.middleNameField +". "+this.lastNameField+"!"; 
    } 
    private void showGreeting(){ 

     try{ 
      String message = getFullName(); 
      JOptionPane.showMessageDialog(this, message, "Greetings",JOptionPane.PLAIN_MESSAGE); 
     }catch(final IllegalArgumentException iae){ 
      JOptionPane.showMessageDialog(this, 
        iae.getMessage(), 
        "Error", 
        JOptionPane.ERROR_MESSAGE); 
     } 

    } 
    public static void main(String[] args) { 
     GreetingApp g = new GreetingApp(); 
    } 

} 

編輯: 沒關係傢伙得到了它,我不得不添加.getText() 歸來「的問候, 「+ this.firstNameField +」 「+ this.middleNameField +」 「+ this.lastNameField +」! 「; 每個字段後實際從中獲取文本

+5

請張貼您的解答作爲答案並接受它,因此它顯示您的問題正在解決。 – Clark 2013-04-21 16:55:29

回答

0

沒關係球員得到它我不得不添加一個.getText()返回「Greetings,」+ this.firstNameField +「」+ this.middleNameField +「。」+ this.lastNameField + 「!」;在每個字段實際上從他們獲得文本後

相關問題