展开全部
要在文本框62616964757a686964616fe58685e5aeb931333361303635输入之后敲回车在标签中显示,需要给文本框加上ActionListener监听器,并重写其
actionPerformed方法,完整的Java程序如下:(改动的地方见注释,当"输入选号"为空时回车,会把"当前选号"置为空)import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;//这里导入监听接口
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CCG extends JFrame implements ActionListener{//这里加监听接口
JLabel jl1=new JLabel("35选7",JLabel.LEFT);
JLabel jl2=new JLabel("输入选号:");
JLabel jl3=new JLabel("当前选号:");
JLabel jl4=new JLabel(); //这里改一下
JTextField jtf=new JTextField(5);
JTextArea jta=new JTextArea(5,20);
JButton jb1=new JButton("添加本组号码");
JButton jb2=new JButton("验证并保存到文件");
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
JPanel jp5=new JPanel();
JPanel jp6=new JPanel();
JPanel jp7=new JPanel();
CCG(){
super("Lottery");
jtf.addActionListener(this); //这里加监听器
jl1.setFont(new Font(null,Font.BOLD,20));
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
jp1.add(jl1);
jp1.setBorder(BorderFactory.createEtchedBorder ());
jp4.setLayout(new FlowLayout(FlowLayout.LEFT));
jp4.add(jl2);jp4.add(jtf);
jp5.setLayout(new FlowLayout(FlowLayout.LEFT));
jp5.add(jl3);jp5.add(jl4);
jp6.setLayout(new FlowLayout(FlowLayout.LEFT));
jp6.add(jb1);
jp2.setLayout(new GridLayout(3,1));
jp2.add(jp4);jp2.add(jp5);jp2.add(jp6);
jp2.setBorder(BorderFactory.createEtchedBorder ());
jp3.setLayout(new FlowLayout(FlowLayout.LEFT));
jp3.add(jta);jp3.add(jb2);
jp3.setBorder(BorderFactory.createEtchedBorder ());
jp7.setLayout(new GridLayout(2,1));
jp7.add(jp2);
jp7.add(jp3);
add(jp1,BorderLayout.NORTH);
add(jp7,BorderLayout.CENTER);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {//这里加监听函数
if(ae.getSource()==jtf){
if(jtf.getText().equals("")){
jl4.setText("");
}else{
if(jl4.getText().equals("")){
jl4.setText(jtf.getText());
}else{
jl4.setText(jl4.getText()+","+jtf.getText());
}
}
}
}
public static void main(String[] args) {
new CCG();
}
}
运行结果: