java - 我们如何在输出屏幕上的文本字段上输入值?

标签 java swing jtextfield

我想在输出屏幕上将值放入 txtf1 中并获取它。我们如何在输出屏幕上的文本字段上输入值?

import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class demog extends JPanel implements ActionListener{

private TextField textf, txtf1;

public void jhand(){
textf = new TextField();
    textf.setSize(40, 40);
    textf.setText("20");
    textf.setEditable(false);
    textf.setBackground(Color.WHITE);
    textf.setForeground(Color.BLACK);
    //textf.setHorizontalAlignment(SwingConstants.CENTER);
    textf.setLocation(15, 15);
    //textf.addActionListener(this);
    txtf1 = new TextField();
    txtf1.setSize(40, 40);
    txtf1.getText();
    txtf1.setEditable(false);
    txtf1.setBackground(Color.WHITE);
    txtf1.setForeground(Color.BLACK);
    //txtf1.setHorizontalAlignment(SwingConstants.CENTER);
    txtf1.setLocation(50, 50);
    JFrame frame = new JFrame("demo");
    JPanel p = new JPanel();
    p.setOpaque(true);
      p.setBackground(Color.WHITE);
      p.setLayout(null);
      frame.setContentPane(p);
      frame.setSize(500,500);
        frame.setVisible(true);
        p.add(textf);
        p.add(txtf1);
}

public void actionPerformed(ActionEvent evt) {
    String text = textf.getText();
    System.out.println(text);
}

public static void main(String... args){
    demog g = new demog();
    g.jhand();
}
}

最佳答案

您必须更改一些代码才能工作。您的代码中存在一些问题,我在以下代码中为您解决了这些问题。请参阅评论以了解一些有关 swing 的知识 ;-)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

// Use upper Case in the start of you class names:
public class Demog extends JPanel implements ActionListener {

    private JTextField textf, txtf1;

    public Demog() {
        jhand();
    }

    public void jhand() {
        setLayout(new FlowLayout()); // Always set the layout before you add components

        // you can use null layout, but you have to use setBounds() method 
        //      for placing the components. For an advanced layout see the 
        //      tutorials for GridBagLayout and mixing layouts with each other.

        textf = new JTextField(); // Do not mix AWT component with 
                                  //    Swing (J components. See the packages)
        //textf.setSize(40, 40); // Use setPreferredSize instead
        textf.setPreferredSize(new Dimension(40, 40));
        textf.setText("20");
        textf.setEditable(false); // Text fields are for getting data from user
                                  //    If you need to show something to user
                                  //    use JLabel instead.
        textf.setBackground(Color.WHITE);
        textf.setForeground(Color.BLACK);
        add(textf);

        txtf1 = new JTextField();
        //txtf1.setSize(40, 40); Use setPreferredSize instead
        txtf1.setPreferredSize(new Dimension(40, 40));
        txtf1.getText();
        txtf1.setEditable(false);
        txtf1.setBackground(Color.WHITE);
        txtf1.setForeground(Color.BLACK);
        add(txtf1);

        JButton b = new JButton("Click ME!");
        b.addActionListener(this);
        add(b);
    }

    public void actionPerformed(ActionEvent evt) {
        String text = textf.getText();
        JOptionPane.showMessageDialog(Demog.this, "\"textf\" text is: "+text);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Demog p = new Demog();
        p.setBackground(Color.WHITE);
        frame.setContentPane(p);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}

祝你好运。

关于java - 我们如何在输出屏幕上的文本字段上输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31177878/

相关文章:

java - 在 swing 中设置 jLabel 的大小

java - KeyTyped 方法说 TextField 为空...但实际上不是

java - 获取 MySQL 表中某列的 SUM 并将其放入 JTextField

java - 创建多个由用户输入定义的 JTextField s,以便通过按下按钮从所有这些输入中读取文本

java - 为什么我无法在第38行输入多个itemNumber

java - 在 JLabel 上分层文本

java - 无限递归,spring mvc客户端挂了

java - 在 Swing 中实现拒绝用户更改值的最佳方法?

java - Java 中的异常处理 (GWT)

java - 在我的自定义 ListView 项上,为什么 (string.length() <= 0) 始终为真,而 (string == "") 始终为假?