gpt4 book ai didi

java - 无法在 JTextfield 中输入文本

转载 作者:行者123 更新时间:2023-12-01 11:34:02 26 4
gpt4 key购买 nike

所以,我只是编写了一个非常简单的程序,它有 1 个 JLabel、1 个 JTextField 和 2 个 JButton。问题是 JTextField 不允许我输入任何文本。我一直在认真思考这个问题,所以任何帮助将不胜感激!

Animated picture

/* Method: init() */
/**
* This method has the responsibility for reading in the data base
* and initializing the interactors at the bottom of the window.
*/
public void init() {
setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
setFont("Courier-24");

nameLabel = new JLabel("Name");
add(nameLabel, SOUTH);

nameField = new JTextField(10);
add(nameField, SOUTH);
nameField.addActionListener(this);

graphButton = new JButton("Graph");
add(graphButton, SOUTH);

clearButton = new JButton("Clear");
add(clearButton, SOUTH);

addActionListeners();
}

/* Method: actionPerformed(e) */
/**
* This class is responsible for detecting when the buttons are
* clicked, so you will have to define a method to respond to
* button actions.
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nameField)
println("Graph: " + nameField.getText());
if (e.getSource() == graphButton)
println("Graph");
if (e.getSource() == clearButton)
println("Clear");
}

/* Private instance variables */
private JLabel nameLabel;
private JTextField nameField;
private JButton graphButton;
private JButton clearButton;

最佳答案

对我有用...

Field

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
*
* @author swhitehead
*/
public class JavaApplication1251 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new JavaApplication1251();
}

public JavaApplication1251() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel implements ActionListener {

public TestPane() {
init();
}

public void init() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;

nameLabel = new JLabel("Name");
add(nameLabel, gbc);

nameField = new JTextField(10);
add(nameField, gbc);
nameField.addActionListener(this);

graphButton = new JButton("Graph");
add(graphButton, gbc);

clearButton = new JButton("Clear");
add(clearButton, gbc);

// addActionListeners();
}

/* Method: actionPerformed(e) */
/**
* This class is responsible for detecting when the buttons are clicked, so you will have to define a method to respond to button actions.
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nameField) {
System.out.println("Graph: " + nameField.getText());
}
if (e.getSource() == graphButton) {
System.out.println("Graph");
}
if (e.getSource() == clearButton) {
System.out.println("Clear");
}
}

/* Private instance variables */
private JLabel nameLabel;
private JTextField nameField;
private JButton graphButton;
private JButton clearButton;
}

}

考虑提供runnable example这说明了你的问题。这不是代码转储,而是您正在执行的操作的示例,它突出显示了您遇到的问题。这将减少困惑并获得更好的响应

关于java - 无法在 JTextfield 中输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30182105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com