好菜的人 2021-06-09 13:29 采纳率: 100%
浏览 25
已采纳

萌新求助 Gui事件处理 我设置了点击事件但是JLabel标签的内容没有改变

package pag;

import java.awt.Color;
import java.awt.Font;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.Timer;


public class WordsLearner extends JFrame  {
		private static JMenuBar menubar = new JMenuBar();
		private static JLabel  wd1= new JLabel();
		private static JLabel  wd2= new JLabel();
		private static JLabel  wd3= new JLabel();
		private static JLabel  wd4= new JLabel();
		private static JLabel  wd5= new JLabel();
		private static JLabel[] labs = {wd1,wd2,wd3,wd4,wd5};
		private static JMenu pat = new JMenu("词汇(W)");
		private static List<Integer> contain = new ArrayList<Integer>(5);
		private static JCheckBoxMenuItem four = new JCheckBoxMenuItem("四级");
		private static JCheckBoxMenuItem six = new JCheckBoxMenuItem("六级");
		private static JMenu pat1 = new JMenu("模式(M)");
		private static JRadioButtonMenuItem fux = new JRadioButtonMenuItem("复习");
		private static JRadioButtonMenuItem xunl = new JRadioButtonMenuItem("训练");
		private static JPanel Wordspan = new JPanel();
		private static JPanel Wordspan1 = new JPanel();
		private static JPanel choms = new JPanel();
		private static Font fot = new Font("宋体",Font.PLAIN,16);
		private static Timer time;
		private static JLabel timeLabel = getTimelabel();
		private static JButton btn = new JButton("换一组");
//		private static String[] tips = {"请选择模式","拼写","选择"};
//		private JComboBox ms = new JComboBox(tips);
		WordsLearner() throws SQLException{
			setTitle("单词学习小程序");
			setLayout(null);
			ButtonGroup btns = new ButtonGroup();
			btns.add(four);
			btns.add(six);
			ButtonGroup btns1 = new ButtonGroup();
			fux.doClick();
			four.doClick();
			btns1.add(four);
			btns1.add(six);
			setBounds(500,200,800,600);
			pat.add(four);
			pat.addSeparator();
			pat.add(six);
			pat1.add(fux);
			pat1.add(xunl);
			menubar.add(pat);
			menubar.add(pat1);
			btn.setBounds(650,200,100,100);
			add(btn);
			setJMenuBar(menubar);
			iniWordspa(Wordspan);
			add(Wordspan);
			add(Wordspan1);
			Wordspan1.setVisible(false);
			btn.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseEntered(MouseEvent e) {
					btn.setBackground(new Color(200,200,169));
				}

				@Override
				public void mouseExited(MouseEvent e) {
					try {
						InitLabel(labs);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					btn.setBackground(new Color(240, 240, 240));
				}
			});
			btn.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					if(e.getActionCommand()!=null) {
						try {
							InitLabel(labs);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			});
		}
		public void iniWordspa(JPanel jp) throws SQLException {
			jp.setLayout(null);
			jp.setBackground(new Color(255,255,255));
			jp.setOpaque(true);
			InitLabel(labs);
//				int i=0;
//				int id = contain.get(i++);
//				re=con.prepareStatement(s+id).executeQuery();
//				while(re.next()) {
//					String word = re.getString("word");
//					String meaning = re.getString("meaning");
//					String s1 = word+"               "+meaning;
//					wd1.setText(s1);
//				}
//				wd1.setForeground(Color.blue);
//				wd1.setBounds(0,0,600,50);
//				wd1.setFont(fot);
//				id = contain.get(i++);
//				re=con.prepareStatement(s+id).executeQuery();
//				while(re.next()) {
//					String word = re.getString("word");
//					String meaning = re.getString("meaning");
//					String s1 = word+"         "+meaning;
//					wd2.setText(s1);
//				}
//				wd2.setFont(fot);
//				wd2.setForeground(Color.blue);
//				wd2.setBounds(0,100,600,50);
//				id = contain.get(i++);
//				re=con.prepareStatement(s+id).executeQuery();
//				while(re.next()) {
//					String word = re.getString("word");
//					String meaning = re.getString("meaning");
//					String s1 = word+"               "+meaning;
//					wd3.setText(s1);
//				}
//				wd3.setFont(fot);
//				wd3.setForeground(Color.blue);
//				wd3.setBounds(0,200,600,50);
//				id = contain.get(i++);
//				re=con.prepareStatement(s+id).executeQuery();
//				while(re.next()) {
//					String word = re.getString("word");
//					String meaning = re.getString("meaning");
//					String s1 = word+"               "+meaning;
//					wd4.setText(s1);
//				}
//				wd4.setFont(fot);
//				wd4.setForeground(Color.blue);
//				wd4.setBounds(0,300,600,50);
//				id = contain.get(i++);
//				re=con.prepareStatement(s+id).executeQuery();
//				while(re.next()) {
//					String word = re.getString("word");
//					String meaning = re.getString("meaning");
//					String s1 = word+"               "+meaning;
//					wd5.setText(s1);
//				}
//				wd5.setFont(fot);
//				wd5.setForeground(Color.blue);
//				wd5.setBounds(0,400,600,50);
				jp.add(wd1);
				jp.add(wd2);
				jp.add(wd3);
				jp.add(wd4);
				jp.add(wd5);
				jp.setBounds(0,0,600,450);
				jp.setVisible(true);
		}

		public void InitLabel(JLabel[] labs) throws SQLException
		{	
			String word,meaning,s1 = null;
			JDBC_Con c = new JDBC_Con();
			Random r = new Random();
			Connection con = c.getConnection();
			String s ="select * from Four where id=";
			ResultSet re = null ;
			for(int i=0;i<5;i++)
			{
				int id = r.nextInt(3200);
				id++;
				if(contain.contains(id)) {
					i--;
				}
				else { 
					contain.add(id);
				}
			}
			
			for(int i=0;i<5;i++) {
				int id = contain.get(i);
				re=con.prepareStatement(s+id).executeQuery();
				while(re.next()) {
					word = re.getString("word");
					meaning = re.getString("meaning");
					s1=word+"               "+meaning;
					
					
				}
				labs[i].setText(s1);
				labs[i].setFont(fot);
				labs[i].setForeground(Color.blue);
				labs[i].setBounds(0,i*80,600,50);
			}
		}
		
		private static JLabel getTimelabel() {
			if (timeLabel == null) {
				timeLabel = new JLabel("");
				timeLabel.setFont(new Font("宋体", Font.BOLD, 15));
				timeLabel.setForeground(new Color(255,0,0));
				time = new Timer(100,new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						timeLabel.setText(new SimpleDateFormat("yyyy年MM月dd日 EEEE hh:mm:ss").format(new Date()));
					}
				});
				time.start();
			}
			return timeLabel;
		}
		public static void main(String[] args) throws SQLException {
			WordsLearner owd=  new WordsLearner();
			owd.setResizable(false);
			owd.setVisible(true);
		}
}
  • 写回答

1条回答 默认 最新

  • 好菜的人 2021-06-09 13:35
    关注

    static的问题 才发现,,我太菜了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题