如何从许多按钮添加文本到单个文本字段? - Java

时间:2015-04-14 21:37:55

标签: java swing user-interface jbutton jtextfield

正如你可以从标题中读到的那样,我需要通过按下按钮来设置JTextField的文本..而且点击时按钮也会消失(我使用方法' setVisible') ,但是当你删除一个字母时,按钮必须再次出现..

但主要关注"如何从多个按钮添加文本到单个文本字段"按下时每个按钮必须添加一个字母,

我到目前为止所做的代码// 注意:我正在使用NetBeans IDE

public class Test extends javax.swing.JPanel {

 /**
 * Creates new form Test
 */
public Test() { 
    initComponents();  
}
/**
 * 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() {

    jPanel1 = new javax.swing.JPanel();
    k = new javax.swing.JButton();
    o = new javax.swing.JButton();
    a1 = new javax.swing.JButton();
    a = new javax.swing.JButton();
    n = new javax.swing.JButton();
    d = new javax.swing.JButton();
    r = new javax.swing.JButton();
    b = new javax.swing.JButton();
    u = new javax.swing.JButton();
    s = new javax.swing.JButton();
    l = new javax.swing.JButton();
    l1 = new javax.swing.JButton();
    txtField = new javax.swing.JTextField();
    c = new javax.swing.JButton();

    k.setText("K");
    k.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            kActionPerformed(evt);
        }
    });

    o.setText("O");
    o.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            oActionPerformed(evt);
        }
    });

    a1.setText("A");
    a1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            a1ActionPerformed(evt);
        }
    });

    a.setText("A");
    a.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            aActionPerformed(evt);
        }
    });

    n.setText("N");
    n.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nActionPerformed(evt);
        }
    });

    d.setText("D");
    d.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            dActionPerformed(evt);
        }
    });

    r.setText("R");
    r.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            rActionPerformed(evt);
        }
    });

    b.setText("B");
    b.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            bActionPerformed(evt);
        }
    });

    u.setText("U");
    u.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            uActionPerformed(evt);
        }
    });

    s.setText("S");
    s.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            sActionPerformed(evt);
        }
    });

    l.setText("L");
    l.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            lActionPerformed(evt);
        }
    });

    l1.setText("L");
    l1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            l1ActionPerformed(evt);
        }
    });

    txtField.setFont(new java.awt.Font("Serif", 1, 12)); // NOI18N
    txtField.setFocusable(false);
    txtField.setMaximumSize(new java.awt.Dimension(202, 31));
    txtField.setMinimumSize(new java.awt.Dimension(202, 31));
    txtField.setOpaque(false);
    txtField.setPreferredSize(new java.awt.Dimension(202, 31));
    txtField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtFieldActionPerformed(evt);
        }
    });

    c.setText("C");
    c.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cActionPerformed(evt);
        }
    });



    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(b)
                        .addComponent(s))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(u)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(l)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(l1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(o)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(c)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(k))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(a)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(n)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(d)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(r)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(a1)))
                    .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(txtField, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addd)))
            .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(txtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(34, 34, 34)
                    .addComponent(addd)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(n)
                .addComponent(d)
                .addComponent(r)
                .addComponent(s)
                .addComponent(a)
                .addComponent(a1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(b)
                .addComponent(u)
                .addComponent(l)
                .addComponent(l1)
                .addComponent(o)
                .addComponent(k)
                .addComponent(c))
            .addGap(19, 19, 19))
    );

    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/test3/fotot/sanda.jpg"))); // NOI18N
    jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 292, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
 private void sActionPerformed(java.awt.event.ActionEvent evt) { 
 s.setVisible(false);
 }                                 

private void aActionPerformed(java.awt.event.ActionEvent evt) {                                  
    a.setVisible(false);
}                                 

private void nActionPerformed(java.awt.event.ActionEvent evt) {                                  
    n.setVisible(false);
}                                 

private void dActionPerformed(java.awt.event.ActionEvent evt) {                                  
    d.setVisible(false);
}                                 

private void rActionPerformed(java.awt.event.ActionEvent evt) {                                  
    r.setVisible(false);
}                                 

private void a1ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    a1.setVisible(false);
}                                  

private void bActionPerformed(java.awt.event.ActionEvent evt) {                                  
    b.setVisible(false);
}                                 

private void uActionPerformed(java.awt.event.ActionEvent evt) {                                  
    u.setVisible(false);
}                                 

private void lActionPerformed(java.awt.event.ActionEvent evt) {                                  
    l.setVisible(false);
}                                 

private void l1ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    l1.setVisible(false);
}                                  

private void oActionPerformed(java.awt.event.ActionEvent evt) {                                  
    o.setVisible(false);
}                                 

private void kActionPerformed(java.awt.event.ActionEvent evt) {                                  
    k.setVisible(false);
}                                 

private void cActionPerformed(java.awt.event.ActionEvent evt) {                                  
    c.setVisible(false);
}                                 

// Variables declaration - do not modify                     
private javax.swing.JButton a;
private javax.swing.JButton a1;
private javax.swing.JButton b;
private javax.swing.JButton c;
private javax.swing.JButton d;
private javax.swing.JButton k;
private javax.swing.JButton l;
private javax.swing.JButton l1;
private javax.swing.JButton n;
private javax.swing.JButton o;
private javax.swing.JButton r;
private javax.swing.JButton s;
public javax.swing.JTextField txtField;
private javax.swing.JButton u;
// End of variables declaration                   
}

任何帮助或建议都非常受欢迎! 谢谢。

1 个答案:

答案 0 :(得分:2)

你的意思是:?

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class TestTextField {
    private JTextField textField;

    private Map<Character, JButton> buttons = new HashMap<>();

    private void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textField = new JTextField(30);
        frame.add(textField, BorderLayout.NORTH);
        JPanel panel = new JPanel(new GridLayout(0, 8, 15, 15));
        for (int i = 0; i < 26; i++) {
            final char c = (char) ('A' + i);
            final JButton button = new JButton(String.valueOf(c));
            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    textField.setText(textField.getText() + c);
                }
            });
            buttons.put(c, button);
            panel.add(button);
        }
        textField.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateButtonVisibility();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateButtonVisibility();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateButtonVisibility();
            }
        });
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    protected void updateButtonVisibility() {
        String text = textField.getText();
        for (Entry<Character, JButton> e : buttons.entrySet()) {
            e.getValue().setVisible(!text.toUpperCase().contains(String.valueOf(e.getKey())));
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    TestTextField test = new TestTextField();
                    test.initUI();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}