请教java中的按钮监听事件代码? 5
想请教下一下java问题,我想在“打印彩票”的按钮上添加一个打印监控事件,在“生成彩票”按钮上添加一个事件,但是不对,请教高手一下,多谢!packagecom.Lotte...
想请教下一下java问题,我想在“打印彩票”的按钮上添加一个打印监控事件,在“生成彩票”按钮上添加一个事件,但是不对,请教高手一下,多谢!
package com.Lottery;
import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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 LotteryFrame extends JFrame {
JButton b=new JButton("生成彩票");
JButton b1=new JButton("打印彩票");
JPanel jp=new JPanel();
JPanel jp1=new JPanel();
JTextField jf=new JTextField("1",10);
JTextArea jt=new JTextArea();
public LotteryFrame() throws HeadlessException{
super();
}
/*
* 界面显示
*/
public LotteryFrame(String arg0) throws HeadlessException {
super(arg0);
jt.setRows(10);
jt.setColumns(30);
jp.add(new JLabel("机选双色球"));
jp.add(jf);
jp.add(new JLabel("注"));
jp1.add(b);
jp1.add(b1);
b1.addActionListener(new PrintListener()); //这个错误,需要改正
b.addActionListener(new ActionListener()); //这个错误,需要改正
this.add(jp,BorderLayout.NORTH);
this.add(jp1,BorderLayout.SOUTH);
this.add(jt);
this.pack();
this.setLocation(200, 3400);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
/*打印按钮监听器*/
class PrintListener implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.print(jt.getText());
}
}
/*生成按钮监听器*/
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent arg0){
jt.setText("");
DoubleColorBall dbcb=new DoubleColorBall();
try{
//根据文本框里输入的数字,调用DoubleColorBall中的方法生成彩票
String[] num=dbcb.getNumber(Integer.parseInt(jf.getText()));
for(int i=0;i<num.length;i++){
jt.append(num[i]+"\n");
}
} catch(Exception e){
e.printStackTrace();
jt.setText("请输入正整数数字");
}
}
}
}
} 展开
package com.Lottery;
import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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 LotteryFrame extends JFrame {
JButton b=new JButton("生成彩票");
JButton b1=new JButton("打印彩票");
JPanel jp=new JPanel();
JPanel jp1=new JPanel();
JTextField jf=new JTextField("1",10);
JTextArea jt=new JTextArea();
public LotteryFrame() throws HeadlessException{
super();
}
/*
* 界面显示
*/
public LotteryFrame(String arg0) throws HeadlessException {
super(arg0);
jt.setRows(10);
jt.setColumns(30);
jp.add(new JLabel("机选双色球"));
jp.add(jf);
jp.add(new JLabel("注"));
jp1.add(b);
jp1.add(b1);
b1.addActionListener(new PrintListener()); //这个错误,需要改正
b.addActionListener(new ActionListener()); //这个错误,需要改正
this.add(jp,BorderLayout.NORTH);
this.add(jp1,BorderLayout.SOUTH);
this.add(jt);
this.pack();
this.setLocation(200, 3400);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
/*打印按钮监听器*/
class PrintListener implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.print(jt.getText());
}
}
/*生成按钮监听器*/
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent arg0){
jt.setText("");
DoubleColorBall dbcb=new DoubleColorBall();
try{
//根据文本框里输入的数字,调用DoubleColorBall中的方法生成彩票
String[] num=dbcb.getNumber(Integer.parseInt(jf.getText()));
for(int i=0;i<num.length;i++){
jt.append(num[i]+"\n");
}
} catch(Exception e){
e.printStackTrace();
jt.setText("请输入正整数数字");
}
}
}
}
} 展开
展开全部
把下面两个类去了,直接用匿名内部类来实现...
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.print(jt.getText());
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
jt.setText("");
DoubleColorBall dbcb = new DoubleColorBall();
try {
// 根据文本框里输入的数字,调用DoubleColorBall中的方法生成彩票
String[] num = dbcb.getNumber(Integer
.parseInt(jf.getText()));
for (int i = 0; i < num.length; i++) {
jt.append(num[i] + "\n");
}
} catch (Exception e) {
e.printStackTrace();
jt.setText("请输入正整数数字");
}
}
});
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.print(jt.getText());
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
jt.setText("");
DoubleColorBall dbcb = new DoubleColorBall();
try {
// 根据文本框里输入的数字,调用DoubleColorBall中的方法生成彩票
String[] num = dbcb.getNumber(Integer
.parseInt(jf.getText()));
for (int i = 0; i < num.length; i++) {
jt.append(num[i] + "\n");
}
} catch (Exception e) {
e.printStackTrace();
jt.setText("请输入正整数数字");
}
}
});
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-12-15
展开全部
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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 LotteryFrame extends JFrame {
JButton b=new JButton("生成彩票");
JButton b1=new JButton("打印彩票");
JPanel jp=new JPanel();
JPanel jp1=new JPanel();
JTextField jf=new JTextField("1",10);
JTextArea jt=new JTextArea();
public LotteryFrame() throws HeadlessException{
super();
}
/*
* 界面显示
*/
public LotteryFrame(String arg0) throws HeadlessException {
super(arg0);
jt.setRows(10);
jt.setColumns(30);
jp.add(new JLabel("机选双色球"));
jp.add(jf);
jp.add(new JLabel("注"));
jp1.add(b);
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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 LotteryFrame extends JFrame {
JButton b=new JButton("生成彩票");
JButton b1=new JButton("打印彩票");
JPanel jp=new JPanel();
JPanel jp1=new JPanel();
JTextField jf=new JTextField("1",10);
JTextArea jt=new JTextArea();
public LotteryFrame() throws HeadlessException{
super();
}
/*
* 界面显示
*/
public LotteryFrame(String arg0) throws HeadlessException {
super(arg0);
jt.setRows(10);
jt.setColumns(30);
jp.add(new JLabel("机选双色球"));
jp.add(jf);
jp.add(new JLabel("注"));
jp1.add(b);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把下面两个类去了,直接用匿名内部类来实现...
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.print(jt.getText());
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
jt.setText("");
DoubleColorBall dbcb = new DoubleColorBall();
try {
String[] num = dbcb.getNumber(Integer
.parseInt(jf.getText()));
for (int i = 0; i < num.length; i++) {
jt.append(num[i] + "\n");
}
} catch (Exception e) {
e.printStackTrace();
jt.setText("请输入正整数数字");
}
}
});
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.print(jt.getText());
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
jt.setText("");
DoubleColorBall dbcb = new DoubleColorBall();
try {
String[] num = dbcb.getNumber(Integer
.parseInt(jf.getText()));
for (int i = 0; i < num.length; i++) {
jt.append(num[i] + "\n");
}
} catch (Exception e) {
e.printStackTrace();
jt.setText("请输入正整数数字");
}
}
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询