棉花糖星球最可爱的兔兔31 2021-06-19 14:43 采纳率: 50%
浏览 37

java 匿名内部类和局部内部类只能引用外部的fianl变量

package as;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class testa<finalString> extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					testa frame = new testa();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */	
	public testa() {
		setTitle("\u62BD\u5956\u7CFB\u7EDF");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 538, 395);
		contentPane = new JPanel();
		contentPane.setBackground(Color.PINK);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("号码");
		lblNewLabel.setBackground(Color.WHITE);
		lblNewLabel.setForeground(Color.BLACK);
		lblNewLabel.setBounds(39, 105, 181, 62);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("奖品等级");
		lblNewLabel_1.setBounds(309, 110, 151, 52);
		contentPane.add(lblNewLabel_1);
	
		JButton btnNewButton_2 = new JButton("添加号码");
		btnNewButton_2.setBounds(21, 257, 93, 23);
		contentPane.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("确认添加");
		btnNewButton_3.setBounds(154, 257, 93, 23);
		contentPane.add(btnNewButton_3);
		
		ArrayList<String> list = new ArrayList<String>();
		String[] ax = {"一等奖","二等奖","三等奖","特等奖","四等奖"};
		String[] number = {"18317900456","34576194588"};	
		
		btnNewButton_2.addActionListener(new ActionListener() {
			   public void actionPerformed(ActionEvent arg0) {
				   needs io = new needs();
				   btnNewButton_3.addActionListener(new ActionListener() {
					   public void actionPerformed(ActionEvent arg0) {
					
						number = insert(number,io.temp);
							io.dispose();
					   }
					  });
				   
				   
				   
			   }
			  });
		
		
		
		class mythread extends Thread{
			   private boolean suspend = true;     //设置一个布尔变量来控制暂停和恢复
			   public synchronized void toSuspend() {  //定义暂停的方法
			    suspend = true;
			   }
			   public synchronized void toRever() {   //定义恢复的方法
			    suspend = false;
			    notify();  //唤醒当前休眠,挂起的线程,没有参数
			   }
			   public void run() {     //重新定义线程的run方法
			      while(true) {    //设置一个永久的循环
			         synchronized (this) {
			                 while(suspend) {  //用suspend来控制调用线程的wait()方法  
			                       try {
			                         wait();   //线程的停止方法
			                          } catch (InterruptedException e) {
			                                 // TODO Auto-generated catch block
			                        e.printStackTrace();
			                      }
			               }
			      
			       }
			         int rnd = new Random().nextInt(number.length); //设置一个整形数让它成为字符串的随机索引
			          String str = number[rnd];
			          lblNewLabel.setText(str);  //使标签显示数组中随机一个字符串
			     	 int c = new Random().nextInt(ax.length); //设置一个整形数让它成为字符串的随机索引
			         String b = ax[c];
			         lblNewLabel_1.setText(b);  //使标签显示数组中随机一个字符串
			  
			      }
			    }
			  }
		mythread t = new mythread();
		t.start();

		JButton btnNewButton_1 = new JButton("停止");
		btnNewButton_1.addActionListener(new ActionListener() {
			   public void actionPerformed(ActionEvent e) {
			    String name = btnNewButton_1.getText();   //得到按钮上的文本
			       if(name.equals("停止")) {   
			    	   t.toSuspend(); 
			        //让他暂停滚动的功能
			    }
			   }
			  });
	
		btnNewButton_1.setBounds(397, 257, 93, 23);
		contentPane.add(btnNewButton_1);
		
		
		
		JButton btnNewButton = new JButton("抽奖");
		btnNewButton.addActionListener(new ActionListener() {
			   public void actionPerformed(ActionEvent arg0) {
			    String name = btnNewButton.getText();   //得到按钮上的文本
			       if(name.equals("抽奖")) {   
			    
			    	   t.toRever();    //让他开始滚动的功能
			    }
			   }
			  });
		btnNewButton.setBounds(281, 257, 93, 23);
		contentPane.add(btnNewButton);
		

	}
	
	
	private static String[] insert(String[] arr, String str) {
	    int size = arr.length;  //获取数组长度
	    String[] tmp = new String[size + 1];  //新建临时字符串数组,在原来基础上长度加一
	    for (int i = 0; i < size; i++){  //先遍历将原来的字符串数组数据添加到临时字符串数组
	        tmp[i] = arr[i];
	    }
	    tmp[size] = str;  //在最后添加上需要追加的数据
	    return tmp;  //返回拼接完成的字符串数组
	}	
}

package as;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class needs extends JDialog {

	private final JPanel c = new JPanel();
	public JTextField textField;
	String temp;	

	/**
	 * Create the dialog.
	 */
	public needs() {
		setBounds(100, 100, 430, 281);
		getContentPane().setLayout(new BorderLayout());
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
		setTitle("\u8BF7\u8F93\u5165\u53F7\u7801\uFF08\u82E5\u4E0D\u663E\u793A\u8BF7\u70B9\u51FB\u7A97\u53E3\u8FB9\u6846\uFF09");
		c.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(c, BorderLayout.CENTER);
		c.setLayout(null);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBounds(0, 191, 418, 49);
			c.add(buttonPane);
			buttonPane.setLayout(null);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						temp = textField.getText();	
					}
				});
				okButton.setBounds(257, 0, 113, 31);
				okButton.setActionCommand("确定");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}	
			
		}
		
		textField = new JTextField();
		textField.setBounds(56, 79, 286, 49);
		c.add(textField);
		textField.setColumns(10);

	}
}

testa    78行与112行

功能中添加选项无法进行。

  • 写回答

1条回答 默认 最新

  • li.siyuan 2021-06-28 13:25
    关注

    你的number 在哪定义的?要先定义变量 才可以使用

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?