通过 JButton 从 JTextField 获取字符串
Get a String from a JTextField through a JButton
我正在编写一个程序,我快完成了。我想要的是:在 JTextField 中键入一个字符串,按下一个 JButton,然后我希望该字符串在另一个 JTextField 中出现。所以我有:2 个 JTextField 和 1 个按钮。这是我的代码:
class Fenster extends JFrame {
JTextField inputfield;
JTextField outputfield;
JButton button;
public Fenster() {
JTextField outputfield = new JTextField();
outputfield.setBounds(50, 315, 400, 32);
add(outputfield);
JTextField inputfield = new JTextField();
inputfield.setBounds(50, 115, 400, 32);
add(inputfield);
//The Button
JButton button = new JButton("Klick me :D");
button.setBounds(154, 250, 92, 32);
button.addActionListener(new buttonlistener());
add(button);
private class buttonlistener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String string = inputfield.getText();
outputfield.setText(string);
}
}
}
我知道我没有包含 JFrame 设置。请帮助,因为每次我按下按钮时都会出现错误:"buttonlistener.actionPerformed(Fenster.java:70)" :S
提前致谢
您可以在初始化时直接实现 ActionListener
,而不是为它使用单独的函数。
//The Button
JButton button = new JButton("Klick me :D");
button.setBounds(154, 250, 92, 32);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String string = inputfield.getText();
outputfield.setText(string);
}
});
add(button);
字段 outputfield
和 inputfield
被声明为 Fenster
class 的属性和 Fenster
构造函数的局部变量。
我们在面板上看到的是本地声明的,但动作侦听器正在尝试使用从未初始化的声明为 class 属性的那些。因此 NullPointerException
.
我无法准确解读您的问题。所以我假设你问
"How can I get my string to output unto a JButton?"
我将向您提供我过去常常这样做的代码。现在,我创建了一个字符串值,您可以开始编码了!:
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.lang.Object;
import java.io.*;
import javax.imageio.*;
public class jframetest extends Object
{
public static void main(String args[]) throws Exception
{
JFrame frame = new JFrame("Not Main");
/* Background Image*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* JLabel image = new JLabel(new ImageIcon("a.jpg")); *
* image.setBounds(0,0, 800, 600); *
* frame.getContentPane().add(image, BorderLayout.CENTER); *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Random generate = new Random();
Random rand = new Random();
String[] name = {"Landry", "Azariah", "Oakley", "Lennon", "Charlie", "Skyler", "Dakota", "Armani", "Phoenix" , "Justice", "Casey", "Emory", "Remy", "Emerson", "Amari", "Roxie", "Hayden", "River", "Milan", "Tatum", "Jessie", "Finley", "Riley", "Rowan", "Sage", "Jamie", "Rory", "Harley", "Leighton", "Peyton", "Dallas", "Remington", "Quinn", "Alexis", "Sawyer", "Kamryn", "Parker", "Avery", "Eden", "Lyric", "Elliot", "Reese", "Zion", "Rylan", "Jordan", "Taylor", "Morgan", "Kendall", "Rylee", "Ryan", "Reagan", "Logan", "Hunter", "Carter"};
int index = (int) (Math.random() * (name.length - 1));
JButton buttun = new JButton(name[index]);
buttun.setBounds(190,65,400,50);
JLabel label2 = new JLabel("Customer: " + name[generate.nextInt(20)]);
label2.setBounds(190,65,400,50); //setBounds(x,y,width,height);
label2.setFont(new Font("Serif", Font.BOLD, 40));
frame.setPreferredSize(new Dimension(200, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.getContentPane().add(button); (not needed when Background Image is commented)
frame.setVisible(true);
frame.pack();
frame.add(buttun);
frame.add(label2);
frame.setSize(800, 600);
frame.setLayout(null);
}
}
我正在编写一个程序,我快完成了。我想要的是:在 JTextField 中键入一个字符串,按下一个 JButton,然后我希望该字符串在另一个 JTextField 中出现。所以我有:2 个 JTextField 和 1 个按钮。这是我的代码:
class Fenster extends JFrame {
JTextField inputfield;
JTextField outputfield;
JButton button;
public Fenster() {
JTextField outputfield = new JTextField();
outputfield.setBounds(50, 315, 400, 32);
add(outputfield);
JTextField inputfield = new JTextField();
inputfield.setBounds(50, 115, 400, 32);
add(inputfield);
//The Button
JButton button = new JButton("Klick me :D");
button.setBounds(154, 250, 92, 32);
button.addActionListener(new buttonlistener());
add(button);
private class buttonlistener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String string = inputfield.getText();
outputfield.setText(string);
}
}
}
我知道我没有包含 JFrame 设置。请帮助,因为每次我按下按钮时都会出现错误:"buttonlistener.actionPerformed(Fenster.java:70)" :S
提前致谢
您可以在初始化时直接实现 ActionListener
,而不是为它使用单独的函数。
//The Button
JButton button = new JButton("Klick me :D");
button.setBounds(154, 250, 92, 32);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String string = inputfield.getText();
outputfield.setText(string);
}
});
add(button);
字段 outputfield
和 inputfield
被声明为 Fenster
class 的属性和 Fenster
构造函数的局部变量。
我们在面板上看到的是本地声明的,但动作侦听器正在尝试使用从未初始化的声明为 class 属性的那些。因此 NullPointerException
.
我无法准确解读您的问题。所以我假设你问
"How can I get my string to output unto a JButton?"
我将向您提供我过去常常这样做的代码。现在,我创建了一个字符串值,您可以开始编码了!:
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.lang.Object;
import java.io.*;
import javax.imageio.*;
public class jframetest extends Object
{
public static void main(String args[]) throws Exception
{
JFrame frame = new JFrame("Not Main");
/* Background Image*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* JLabel image = new JLabel(new ImageIcon("a.jpg")); *
* image.setBounds(0,0, 800, 600); *
* frame.getContentPane().add(image, BorderLayout.CENTER); *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Random generate = new Random();
Random rand = new Random();
String[] name = {"Landry", "Azariah", "Oakley", "Lennon", "Charlie", "Skyler", "Dakota", "Armani", "Phoenix" , "Justice", "Casey", "Emory", "Remy", "Emerson", "Amari", "Roxie", "Hayden", "River", "Milan", "Tatum", "Jessie", "Finley", "Riley", "Rowan", "Sage", "Jamie", "Rory", "Harley", "Leighton", "Peyton", "Dallas", "Remington", "Quinn", "Alexis", "Sawyer", "Kamryn", "Parker", "Avery", "Eden", "Lyric", "Elliot", "Reese", "Zion", "Rylan", "Jordan", "Taylor", "Morgan", "Kendall", "Rylee", "Ryan", "Reagan", "Logan", "Hunter", "Carter"};
int index = (int) (Math.random() * (name.length - 1));
JButton buttun = new JButton(name[index]);
buttun.setBounds(190,65,400,50);
JLabel label2 = new JLabel("Customer: " + name[generate.nextInt(20)]);
label2.setBounds(190,65,400,50); //setBounds(x,y,width,height);
label2.setFont(new Font("Serif", Font.BOLD, 40));
frame.setPreferredSize(new Dimension(200, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.getContentPane().add(button); (not needed when Background Image is commented)
frame.setVisible(true);
frame.pack();
frame.add(buttun);
frame.add(label2);
frame.setSize(800, 600);
frame.setLayout(null);
}
}