求教java大神,怎么在文本框输入之后,敲回车在标签上显示,如图片所示
源代码packageLottery;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Flo...
源代码package Lottery;import java.awt.BorderLayout;import java.awt.Color;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.*;import javax.swing.border.Border;public class cp extends JFrame { private String str; private JPanel p1=new JPanel(); private JPanel p2=new JPanel(); private JPanel p3=new JPanel(); private JPanel p4=new JPanel(); private JPanel p5=new JPanel(); private JPanel p6=new JPanel(); private JPanel p7=new JPanel(); private JLabel l1=new JLabel("35选7",JLabel.LEFT); private JLabel l2=new JLabel("输入选号:"); private JLabel l3=new JLabel("当前选号:"); private JLabel l4=new JLabel(str); private JTextField jtf=new JTextField(5); private JTextArea jta=new JTextArea(5,20); private JButton b1=new JButton("添加本组号码"); JButton b2=new JButton("验证并保存到文件"); public cp(){ { p1.setBorder(BorderFactory.createEtchedBorder()); p5.setBorder(BorderFactory.createEtchedBorder()); p6.setBorder(BorderFactory.createEtchedBorder()); l1.setFont(new Font(null,Font.BOLD,20)); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); p1.add(l1); p2.setLayout(new FlowLayout(FlowLayout.LEFT)); p2.add(l2);p2.add(jtf); jtf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { str=jtf.getText(); l4 = new JLabel(str); } }); p3.setLayout(new FlowLayout(FlowLayout.LEFT)); p3.add(l3);p3.add(l4); p4.setLayout(new FlowLayout(FlowLayout.LEFT)); p4.add(b1); p5.setLayout(new GridLayout(3,1)); p5.add(p2);p5.add(p3);p5.add(p4); p6.setLayout(new FlowLayout(FlowLayout.LEFT)); p6.add(jta);p6.add(b2); p7.setLayout(new GridLayout(2,1)); p7.add(p5); p7.add(p6); add(p1,BorderLayout.NORTH); add(p7,BorderLayout.CENTER); } } public static void main(String[] args) { cp frame=new cp(); frame.setTitle("Lottery"); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,300); frame.setVisible(true); }}
展开
展开全部
要在文本框输入之后敲回车在标签中显示,需要给文本框加上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();
}
}
运行结果:
追问
如果是要把文本框里的数据保存在数组中呢,然后对数组排序,再把排序后的在标签中显示,谢谢大神
追答
@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{
String[]a=(jl4.getText()+","+jtf.getText()).split(",");
int []b=new int[a.length];
for(int i=0;i<a.length;i++){
b[i]=Integer.parseInt(a[i].trim());
}
Arrays.sort(b);
String tmp=Arrays.toString(b);
String s=tmp.substring(1,tmp.length()-1);
jl4.setText(s);
}
}
}
}
展开全部
输入选号的input标签 看看有没有onchange事件,在onchange事件中,将你这个input的值,设置到当前选号的label中,先找到label,再使用innerHTML属性
追问
看不懂啊
追答
日你一脸:前台代码贴出来,不要图片
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
jtf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str=jtf.getText();
// l4 = new JLabel(str);// 这里错了,不能每次去new一个
l4.setText(str);
}
});
追答
l4.setText(l4.getText()/*获取标签原有的*/+","/*分隔符*/+ str/*追加新的*/);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询