按钮可以保存用户输入并在以后访问它吗?用java

时间:2014-11-22 19:41:30

标签: java netbeans input save

你认为这个问题很愚蠢,但这对我来说很难,因为我只是java的初学者而且我从那里开始搜索。 请问我如何做到这一点,我正在使用netbeans 8 谢谢!

2 个答案:

答案 0 :(得分:0)

有很多方法,你可以使用 JOptionPane 来输入Message Dialog

JOptionPane 始终返回字符串

然后您可以将输入数据保存在变量中。

答案 1 :(得分:0)

如果你取一个字段的值,你必须先将信息保存在一个全局变量中,然后再访问它。

import javax.swing.JOptionPane;

public class example extends javax.swing.JFrame {

String nameStr = ""; //Here I save the name entered.

public example() {
    initComponents();
}

private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {                                      
    nameStr = nameTxt.getText();
    nameTxt.setText("");
}                                     

private void showBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    JOptionPane.showMessageDialog(null, "You entered this name: " + nameStr);
}                                       

public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new example().setVisible(true);
        }
    });
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    aboutLbl = new javax.swing.JLabel();
    inputLbl = new javax.swing.JLabel();
    nameTxt = new javax.swing.JTextField();
    okBtn = new javax.swing.JButton();
    showBtn = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    aboutLbl.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
    aboutLbl.setText("Example by José Peña");

    inputLbl.setText("Input your name:");

    okBtn.setText("OK");
    okBtn.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            okBtnActionPerformed(evt);
        }
    });

    showBtn.setText("Show my name entered");
    showBtn.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            showBtnActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(showBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
                .addComponent(aboutLbl)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(inputLbl)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(nameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(okBtn))))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(aboutLbl)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(inputLbl)
                .addComponent(nameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(okBtn))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(showBtn)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

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


// Variables declaration - do not modify                     
private javax.swing.JLabel aboutLbl;
private javax.swing.JLabel inputLbl;
private javax.swing.JTextField nameTxt;
private javax.swing.JButton okBtn;
private javax.swing.JButton showBtn;
// End of variables declaration                   
}