当前位置: 动力学知识库 > 问答 > 编程问答 >

java - I'm trying to make a button to count characters in a text field

问题描述:

I'm trying to use the JButton count to count the number of characters entered into the JTextField t. I'm new to Java and GUIs but here's my code:

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);

}

I'm also trying to enter the value in another uneditable JTextField x. Any help is appreciated.

网友答案:

First of all, I recommend you not to use AWT elements, since it brings tons of problems and it has little to no support, instead you can try using Swing components which are a replacement/fix for AWT. You can read more about here. You might also want to read AWT vs Swing (Pros and Cons).

Now going into your problem:

You should avoid extending from JFrame, I might recommend you to create a new JFrame object instead. Here's the reason to why. That being said, you can also remove all your this.t and other calls with this.

I'm glad you're using a Layout Manager!

And now to count the number of characters on your JTextField and set text to your other JTextField you should use this code:

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

Also I fixed your code, I changed AWT elements to Swing ones and added number of cols to your second JTextField so it would appear.

So, here's a running example that I made from your code (And removed Choice choice line since you didn't posted that code):

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();
    }
}
网友答案:

Add this to your code

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

OR

You can use lambda expression like this

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

For More http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

网友答案:

You need to add a listener

TextField t = new TextField();

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

You can read more about here

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

网友答案:

When you click on a button you should

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

To save the text from the texfield. Then you can use something like:

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

To set it to the JTextField.

分享给朋友:
您可能感兴趣的文章:
随机阅读: