【问题标题】:I'm trying to make a button to count characters in a text field我正在尝试制作一个按钮来计算文本字段中的字符
【发布时间】:2016-02-28 09:45:00
【问题描述】:

我正在尝试使用JButton count 来计算输入到JTextField t 中的字符数。我是 Java 和 GUI 的新手,但这是我的代码:

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

public class GUI1 extends Frame implements ActionListener{

    TextField t; 
    Button count;
    int a;
    Choice choice;

    public GUI1(){

        this.t = new TextField("", 30);

        this.count = new Button("count");
        this.count.addActionListener(this);

        JTextField x = new JTextField();
        x.setEditable(false);

        this.setTitle("Character count");
        this.setLayout(new FlowLayout());

        this.add(x);
        this.add(t);
        this.add(count);

        this.pack();
        this.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()== this.count) 


       t.setText(choice.getSelectedItem()+ " " +a);
    }

我也在尝试在另一个不可编辑的JTextField x 中输入值。任何帮助表示赞赏。

【问题讨论】:

    标签: java user-interface


    【解决方案1】:

    首先,我建议您不要使用AWT 元素,因为它会带来大量问题并且几乎没有支持,相反您可以尝试使用Swing 组件,它们是AWT 的替代/修复。您可以阅读有关here 的更多信息。您可能还想阅读AWT vs Swing (Pros and Cons)

    现在进入你的问题:

    您应该避免从JFrame 扩展,我可能会建议您创建一个新的JFrame 对象。 Here's the reason to why。话虽如此,您还可以删除所有this.t 和其他使用this 的呼叫。

    很高兴您使用了布局管理器!

    现在要计算JTextField 上的字符数并将文本设置为其他JTextField,您应该使用以下代码:

    count.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int count = t.getText().length();
            System.out.println(count);
            x.setText(t.getText());
        }
    });
    

    我还修复了您的代码,将 AWT 元素更改为 Swing 元素,并将列数添加到您的第二个 JTextField 以便它出现。

    所以,这是我根据您的代码制作的一个运行示例(并删除了 Choice choice 行,因为您没有发布该代码):

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GUI1 {
    
        JTextField t; 
        JButton count;
        int a;
        JFrame frame;
    
        public GUI1(){
            frame = new JFrame();
            t = new JTextField("", 15);
            count = new JButton("count");
            JTextField x = new JTextField("", 15);
            x.setEditable(false);
    
            count.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int count = t.getText().length();
                    System.out.println(count);
                    x.setText(t.getText());
                }
            });
    
            frame.setTitle("Character count");
            frame.setLayout(new FlowLayout());
    
            frame.add(x);
            frame.add(t);
            frame.add(count);
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
    
        }
    
        public static void main (String args[]) {
            new GUI1();
        }
    }
    

    【讨论】:

      【解决方案2】:

      将此添加到您的代码中

      count.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          a = t.getText().length();
       }
      });
      

      你可以像这样使用 lambda 表达式

      count.addActionListener(e -> a = t.getText().length());
      

      更多 http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

      【讨论】:

        【解决方案3】:

        当你点击一个按钮时,你应该

        String str = t.getText(); // get string from jtextfield
        

        从 texfield 保存文本。然后你可以使用类似的东西:

        a = str..length(); // get length of string
        x.setText(str + " " + a); //set it to the field
        

        将其设置为 JTextField。

        【讨论】:

          【解决方案4】:

          你需要添加一个监听器

          TextField t = new TextField();
          
          Button b = new Button("Count");
          b.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  int count = t.getText().length();
               }
          });
          

          您可以在此处阅读更多信息

          http://www.tutorialspoint.com/awt/awt_button.htm

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-11-18
            • 2011-03-04
            • 1970-01-01
            • 2021-10-06
            • 2022-11-22
            • 2022-07-12
            • 1970-01-01
            相关资源
            最近更新 更多