作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
使用登录后,我想吐出用户名。 但是,当我尝试单击登录按钮时, 它给了我力量。 我看着logcat,但是什么也没显示。 这种编码是在说。 它将根据我在登录屏幕中输入的名称来烘烤用户名。 不会有任何密码。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我是一名优秀的程序员,十分优秀!