如何在单击或选中时消失的JTextArea上添加消息?

时间:2017-06-01 19:07:08

标签: java jtextarea

我的问题很简单,但我不知道它是什么

我有一个程序,在java中,就像这样

enter image description here

我希望在JTextArea上添加一条短信,在点击或选中时会消失,就像你在facebook上登录一样

enter image description here

我想要的JTextArea例如是" Startind Date:" JTextArea称为textAreaStartindDate

到目前为止这是我的代码。我使用了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                   

}

没有使用明显的

2 个答案:

答案 0 :(得分:0)

通常,您可以使用ActionListener或MouseListener。 因为你没有发布任何代码,我们无法帮助你。但也许这篇文章有助于: Onclick for TextField

答案 1 :(得分:0)

初始化JTextArea时添加默认文字。然后添加FocusListener。如下所示:

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");
    }
});