附带按钮点击的事件

时间:2009-07-16 07:05:38

标签: java swing jbutton

我有一个JFrame,其中有2个文本字段和1个JButton。用户需要在文本框中输入一些字符串。当用户单击JFrame中的按钮时,这些字符串将显示在JTable中。我没有得到用按钮点击编写的代码。请帮我。

4 个答案:

答案 0 :(得分:2)

查看方法void addActionListener(ActionListener l)。特定类的Javadoc通常也有帮助:Java API

答案 1 :(得分:2)

您的描述对于一个好的答案来说有点过于模糊,但我怀疑您缺少的步骤是为该按钮添加一个动作监听器。例如,这描述了在Sun's Swing tutorials。我个人不喜欢他们的风格,而且自己更喜欢匿名的内部课程:

myButton.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e) {
    callMyMethod();
  }
}

无论哪种方式都有效,我更喜欢匿名内部类,因为它们保持本地内容并避免使用大量的切换方法。

答案 2 :(得分:0)

您需要向该按钮添加actionlistener。尝试在谷歌上搜索。 现在您可以查看此链接Jbutton example

答案 3 :(得分:0)

出于学习目的:

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.AbstractTableModel;

public class NameGUI extends JFrame {
    class NamePair {
        String firstName;
        String lastName;
    }

    class NameModel extends AbstractTableModel {
        List<NamePair> names = new ArrayList<NamePair>();

        @Override
        public String getColumnName(int column) {
            switch (column) {
            case 0:
                return "First name";
            case 1:
                return "Last name";
            }
            return "";
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public int getRowCount() {
            return names.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
            case 0:
                return names.get(rowIndex).firstName;
            case 1:
                return names.get(rowIndex).lastName;
            }
            return null;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }

        @Override
        public void setValueAt(Object value, int rowIndex, int columnIndex) {
            switch (columnIndex) {
            case 0:
                names.get(rowIndex).firstName = String.valueOf(value);
                break;
            case 1:
                names.get(rowIndex).lastName = String.valueOf(value);
                break;
            }
        }

    }

    JTextField firstName;
    JTextField lastName;
    JButton addName;
    JTable nameTable;
    NameModel nameModel;

    public NameGUI() {
        super("My GUI");
        Container c = getContentPane();
        GroupLayout gl = new GroupLayout(c);
        c.setLayout(gl);
        gl.setAutoCreateContainerGaps(true);
        gl.setAutoCreateGaps(true);
        firstName = new JTextField(20);
        lastName = new JTextField(20);
        addName = new JButton("Add name");
        // MAGIC STARTS *********************
        addName.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onButtonClick();
            }
        });
        // MAGIC ENDS ***********************
        nameModel = new NameModel();
        nameTable = new JTable(nameModel);
        JScrollPane nameTableScroll = new JScrollPane(nameTable);

        gl.setHorizontalGroup(
            gl.createParallelGroup()
            .addGroup(
                gl.createSequentialGroup()
                .addComponent(firstName)
                .addComponent(lastName)
                .addComponent(addName)
            )
            .addComponent(nameTableScroll)
        );
        gl.setVerticalGroup(
            gl.createSequentialGroup()
            .addGroup(
                gl.createParallelGroup(Alignment.BASELINE)
                .addComponent(firstName)
                .addComponent(lastName)
                .addComponent(addName)
            )
            .addComponent(nameTableScroll)
        );

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
    }

    private void onButtonClick() {
        NamePair np = new NamePair();
        np.firstName = firstName.getText();
        np.lastName = lastName.getText();
        nameModel.names.add(np);
        nameModel.fireTableDataChanged();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NameGUI().setVisible(true);
            }
        });
    }

}