2017-06-01 3 views
1

meine Frage ist sehr einfach, aber ich, aber ich weiß nicht, was esWie füge ich eine Nachricht zu einer JTextArea hinzu, die verschwindet, wenn geklickt oder ausgewählt wird?

ist ich ein Programm haben, in Java, wie so

enter image description here

und ich möchte auf der JTextArea ist ein hinzufügen Kurznachricht, die disappeares wenn sie angeklickt oder ausgewählt, wie wenn Sie sich einloggen auf Facebook

enter image description here

die JTextArea i wollen zum Beispiel die eine unter dem „Start ind Datum: "Die JTextArea heißt textAreaStartindDate

das ist mein Code so weit. i verwendet, um die in Design-Option eingebaut Netbeans

public class CreateEventUI extends javax.swing.JFrame { 

private static final long serialVersionUID = 7526472295622776147L; 

//GUARDAR EVENTOS EM REGISTOEVENTOS 

/** 
* Creates new form CriarEventoUI 
*/ 
public CreateEventUI() { 
    super("Create an Event"); 
    setResizable(false); 
    initComponents(); 
    setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    setVisible(true); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    labelChooseTypeOfEvent = new javax.swing.JLabel(); 
    comboBoxTypeOfEvent = new javax.swing.JComboBox<>(); 
    labelInsertTypeOfData = new javax.swing.JLabel(); 
    labelTitle = new javax.swing.JLabel(); 
    labelDescription = new javax.swing.JLabel(); 
    jScrollPane1 = new javax.swing.JScrollPane(); 
    textAreaDescription = new javax.swing.JTextArea(); 
    textFieldTitle = new javax.swing.JTextField(); 
    labelStartingDate = new javax.swing.JLabel(); 
    labelStartSubmissionDate = new javax.swing.JLabel(); 
    labelEndDate = new javax.swing.JLabel(); 
    labelEndSubmissionDate = new javax.swing.JLabel(); 
    labelPlace = new javax.swing.JLabel(); 
    textFieldPlace = new javax.swing.JTextField(); 
    buttonConfirm = new javax.swing.JButton(); 
    buttonCancel = new javax.swing.JButton(); 
    textFieldStartingDate = new javax.swing.JTextField(); 
    jTextField3 = new javax.swing.JTextField(); 
    jTextField2 = new javax.swing.JTextField(); 
    jTextField4 = new javax.swing.JTextField(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    labelChooseTypeOfEvent.setText("Choose the type of event"); 

    comboBoxTypeOfEvent.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Congress" , "Exposition"})); 

    labelInsertTypeOfData.setText("insert the data"); 

    labelTitle.setText("Title"); 

    labelDescription.setText("Description"); 

    textAreaDescription.setColumns(20); 
    textAreaDescription.setRows(5); 
    jScrollPane1.setViewportView(textAreaDescription); 

    labelStartingDate.setText("Starting date:"); 

    labelStartSubmissionDate.setText("starting date for submissions:"); 

    labelEndDate.setText("End date:"); 

    labelEndSubmissionDate.setText("Ending date for submissions:"); 

    labelPlace.setText("place"); 

    buttonConfirm.setText("Confirm"); 
    buttonConfirm.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      buttonConfirmActionPerformed(evt); 
     } 
    }); 

    buttonCancel.setText("Cancel"); 
    buttonCancel.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      buttonCancelActionPerformed(evt); 
     } 
    }); 

    textFieldStartingDate.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      textFieldStartingDateActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(25, 25, 25) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(labelInsertTypeOfData) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(labelChooseTypeOfEvent) 
        .addGap(18, 18, 18) 
        .addComponent(comboBoxTypeOfEvent, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
         .addGroup(layout.createSequentialGroup() 
          .addComponent(labelTitle) 
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
          .addComponent(textFieldTitle)) 
         .addComponent(labelDescription) 
         .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE) 
         .addGroup(layout.createSequentialGroup() 
          .addComponent(labelPlace) 
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
          .addComponent(textFieldPlace))) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addGroup(layout.createSequentialGroup() 
          .addGap(18, 18, 18) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
           .addComponent(labelStartSubmissionDate) 
           .addComponent(labelStartingDate) 
           .addComponent(textFieldStartingDate, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)) 
          .addGap(40, 40, 40) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
           .addComponent(labelEndDate) 
           .addComponent(labelEndSubmissionDate) 
           .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))) 
         .addGroup(layout.createSequentialGroup() 
          .addGap(173, 173, 173) 
          .addComponent(buttonConfirm) 
          .addGap(87, 87, 87) 
          .addComponent(buttonCancel))))) 
      .addContainerGap(13, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(28, 28, 28) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(labelChooseTypeOfEvent) 
       .addComponent(comboBoxTypeOfEvent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addGap(23, 23, 23) 
      .addComponent(labelInsertTypeOfData) 
      .addGap(18, 18, 18) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(labelTitle) 
         .addComponent(textFieldTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(labelDescription) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(labelStartingDate) 
         .addComponent(labelEndDate)) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addGroup(layout.createSequentialGroup() 
          .addGap(52, 52, 52) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
           .addComponent(labelStartSubmissionDate) 
           .addComponent(labelEndSubmissionDate)) 
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
           .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
          .addComponent(textFieldStartingDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(labelPlace) 
       .addComponent(textFieldPlace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(buttonConfirm) 
       .addComponent(buttonCancel)) 
      .addContainerGap(17, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold>       

private void buttonConfirmActionPerformed(java.awt.event.ActionEvent evt) {            
    // TODO add your handling code here: 
}            

private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {            
    dispose(); 
}            

private void textFieldStartingDateActionPerformed(java.awt.event.ActionEvent evt) {              
    // TODO add your handling code here: 
}              

// Variables declaration - do not modify      
private javax.swing.JButton buttonCancel; 
private javax.swing.JButton buttonConfirm; 
private javax.swing.JComboBox<String> comboBoxTypeOfEvent; 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JTextField jTextField2; 
private javax.swing.JTextField jTextField3; 
private javax.swing.JTextField jTextField4; 
private javax.swing.JLabel labelChooseTypeOfEvent; 
private javax.swing.JLabel labelDescription; 
private javax.swing.JLabel labelEndDate; 
private javax.swing.JLabel labelEndSubmissionDate; 
private javax.swing.JLabel labelInsertTypeOfData; 
private javax.swing.JLabel labelPlace; 
private javax.swing.JLabel labelStartSubmissionDate; 
private javax.swing.JLabel labelStartingDate; 
private javax.swing.JLabel labelTitle; 
private javax.swing.JTextArea textAreaDescription; 
private javax.swing.JTextField textFieldPlace; 
private javax.swing.JTextField textFieldStartingDate; 
private javax.swing.JTextField textFieldTitle; 
// End of variables declaration     

}

ohne die offensichtliche

+0

Kannst du auch Code bis jetzt hinzufügen? –

+0

Suche nach Wasserzeichen textfield/textarea oder [Textfeld mit Standardnachricht] (https://stackoverflow.com/questions/14916255/how-to-display-a-default-message-in-jtextfield-java) – matoni

+0

Fragen Sie nach JTextArea oder JTextField (oder beides)? – Pshemo

Antwort

0

Im Allgemeinen können Sie eine Action oder Mouselistener verwenden. Für Sie hat keinen Code gepostet, wir können Ihnen nicht genau helfen. Aber vielleicht hilft dieser Beitrag: Onclick for TextField

+0

Ich bereits edited mit Code, sorry Ich möchte die offensichtliche .setText vermeiden ("") wenn ich die JTextArea – skidils

+0

So, wenn .setText ("") ist zu offensichtlich, was genau willst du? –

0

Fügen Sie einen Standardtext bei der Initialisierung der . Fügen Sie dann eine FocusListener hinzu. wie unten:

textAreaDescription = new javax.swing.JTextArea("Description"); 
//jScrollPane1.setViewportView(textAreaDescription); 
textAreaDescription.addFocusListener(new FocusListener() { 
    public void focusGained(FocusEvent e) { 
     textAreaDescription.setText(""); 
    } 

    public void focusLost(FocusEvent e) { 
     textAreaDescription.setText("Description"); 
    } 
}); 
Verwandte Themen