将两个JTextField与ActionListener进行比较

时间:2016-02-03 20:36:46

标签: java swing user-interface compare

我要做的是使用ActionListener比较JFrame中TextFields的两个输入。如果两个输入相等并且用户点击按钮,则会弹出一个MessageDialog并说出" equal"。如果它们不相等,将弹出MessageDialog并说“不等于”#34;。我有框架和ActionListener运行,我只是不知道如何从TextFields获取输入并比较它们。

例如,如果用户输入这样的内容, Equal TextFields,会弹出,Equal Message

这是我的主要课程:

public class LabFiveOne
{

public static void main(String[] args)
    {   
    JFrame frame = new JFrame("String Equality Program");

    JTextField tf1 = new JTextField(10);
    tf1.setActionCommand(tf1.toString());
    tfListener tfListen = new tfListener(tf1);
    JTextField tf2 = new JTextField(10);
    tf2.setActionCommand(tf2.toString());
    JButton chEq = new JButton("Check Equality");
    chEq.addActionListener(tfListen);

    JPanel nPanel = new JPanel();
    nPanel.add(tf1);
    nPanel.add(tf2);
    frame.add(nPanel, BorderLayout.NORTH);
    JPanel sPanel = new JPanel();
    sPanel.add(chEq);
    frame.add(sPanel, BorderLayout.SOUTH);
    nPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    frame.pack();

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

这是我的ActionListener类:

class tfListener implements ActionListener
{
    private final JTextField tf3;

    public tfListener(JTextField nameTF)
    {
        tf3 = nameTF;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("abc"))
        {
            JOptionPane.showMessageDialog(null, "equal");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "not equal");
        }
    }   
}

3 个答案:

答案 0 :(得分:1)

编辑:还可以尝试将ActionListener类中的构造函数更改为

public tfListener(JTextField tf1,JTextField tf2){

{

嗨:)只是不要过度思考,你应该没事。简单的方法是将ActionListener直接实现到Main Class,如下所示:

public class LabFiveOne
{

public static void main(String[] args)
    {   
    JFrame frame = new JFrame("String Equality Program");

    final JTextField tf1 = new JTextField(10);
    tf1.setActionCommand(tf1.toString());
    tfListener tfListen = new tfListener(tf1);
    final JTextField tf2 = new JTextField(10);
    tf2.setActionCommand(tf2.toString());
    JButton chEq = new JButton("Check Equality");  
    chEq.addActionListener(tfListen);


    JPanel nPanel = new JPanel();
    nPanel.add(tf1);
    nPanel.add(tf2);
    frame.add(nPanel, BorderLayout.NORTH);
    JPanel sPanel = new JPanel();
    sPanel.add(chEq);
    frame.add(sPanel, BorderLayout.SOUTH);
    nPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    frame.pack();

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

{

class tfListener implements ActionListener
{
private final String tf1text;
private final String tf2text;


public tfListener(JTextField tf1, JTextField tf2)
{
    tf1text = new String(tf1.getText());
    tf1text = new String(tf2.getText());

}

@Override
public void actionPerformed(ActionEvent e)
{
    if(tf1text.equal(tf2text))
    {
        JOptionPane.showMessageDialog(null, "equal");
    }
    else
    {
        JOptionPane.showMessageDialog(null, "not equal");
    }
}   

} }

答案 1 :(得分:0)

tf1.toString();

显示JTextField中的一些信息。 使用其他方法从字段中获取输入。我的意思是它的方法:

tfi.getText();

最好查看JTextField javadoc

答案 2 :(得分:0)

老实说,我认为你不需要两节课;一个用于实现GUI,一个用于处理ActionListener,当你可以在一个类中拥有所有内容,如下面的类

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;

public class LabFiveOne implements ActionListener
{

private JFrame frame;
private JPanel nPanel, sPanel;
private JTextField tf1, tf2;
private JButton chEq;

public static void main(String[] args)
{   
    new LabFiveOne();
}

public LabFiveOne(){
    frame = new JFrame("String Equality Program");

    tf1 = new JTextField(10);
    tf2 = new JTextField(10);
    chEq = new JButton("Check Equality");
    chEq.addActionListener(this);

    nPanel = new JPanel();
    nPanel.add(tf1);
    nPanel.add(tf2);
    frame.add(nPanel, BorderLayout.NORTH);
    sPanel = new JPanel();
    sPanel.add(chEq);
    frame.add(sPanel, BorderLayout.SOUTH);
    nPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    frame.pack();

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String action = e.getActionCommand();

    if(action.equals("Check Equality")){
        String number1 = tf1.getText();
        String number2 = tf2.getText();

        int num1 = Integer.valueOf(number1);
        int num2 = Integer.valueOf(number2);

        if(num1 == num2){
            JOptionPane.showMessageDialog(null, "Equal");
        }
        else{
            JOptionPane.showMessageDialog(null, "Not Equal");
        }
    }
}
}

我将全局声明为全局,以便ActionPerformed方法可以访问Textfields中的值。