gpt4 book ai didi

Java 数字未显示在文本字段中,我想以字符串形式读入

转载 作者:行者123 更新时间:2023-12-01 10:46:30 25 4
gpt4 key购买 nike

我是 Java 新手。我编写了一个类似的应用程序,当我单击按钮时,它将文本放入 JTextField 中,与按钮关联的文本将显示在 JTextField 中。现在我正在做数字(尝试读取字符串),但是当我单击按钮时,什么也没有显示。我知道这可能是我错过的一些小事。如果您能提供帮助,我将非常感激! :)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleButtons_Wallace extends JPanel implements ActionListener {
private JButton button0;
private JButton button1;
private JButton button2;
private JButton button3;
private JTextField Textfield;
public SimpleButtons_Wallace() {
GridLayout experimentLayout = new GridLayout(0, 2);
JPanel MiniME = new JPanel();
button0 = new JButton("zero");
button0.addActionListener(this);
button1 = new JButton("1");
button1.addActionListener(this);
button2 = new JButton("2");
button2.addActionListener(this);
button3 = new JButton("3");
button3.addActionListener(this);
Textfield = new JTextField(10);
Textfield.addActionListener(this);
MiniME.setLayout(experimentLayout);
add(MiniME);
MiniME.add(button0);
MiniME.add(button1);
MiniME.add(button2);
MiniME.add(button3);
MiniME.add(Textfield);
}
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
Textfield.setText(b.getText());
}
}
import javax.swing.JFrame;
import javax.swing.*;
public class GuiMain_Wallace extends JFrame {
public GuiMain_Wallace() {
getContentPane().add(new SimpleButtons_Wallace());
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]) {
GuiMain_Wallace aframe = new GuiMain_Wallace();
aframe.setSize(225, 230);
aframe.setVisible(true);
}
}

//这是我尝试让您使用的两个类。我能做些什么来//让 JTextField 被填充吗?

最佳答案

but when I click the button, nothing is showing up.

问题是您正在尝试管理框架的大小(您将框架尺寸设置得太小,因此文本字段的文本被截断)。不要那样做。使用 pack() 方法,以便组件可以按其首选尺寸显示:

//aframe.setSize(225, 230);
aframe.pack();

创建带有数字的 JTextField 始终是一个好主意。该数字用于文本字段以确定其首选大小。在您的例子中,您指定了 10,这意味着在您需要滚动文本之前它将至少显示 10 个字符。

关于Java 数字未显示在文本字段中,我想以字符串形式读入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34139422/

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