问题描述
menu和panel是两个JPanel,添加在Main_menu里。importjava.awt.Color;importjava.awt.Font;importjava.awt.FontMetrics;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.Rectangle;importjava.awt.RenderingHints;importjava.awt.Shape;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.geom.Line2D;importjava.awt.image.ImageObserver;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.text.AttributedCharacterIterator;importjava.util.ArrayList;importjava.util.List;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JMenu;importjavax.swing.JPanel;importjavax.swing.JTextField;importDBBean.DBBean;publicclassMain_menuextendsJFrame{Menumenu=newMenu();Panelpanel=newPanel();publicMain_menu(){System.out.println("Main_menu");this.add(menu);this.add(panel);//this.setVisible(true);this.setSize(1000,739);this.setLocationRelativeTo(null);setResizable(false);setTitle("李涵大天才V1.0");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);getContentPane().setBackground(Color.DARK_GRAY);}publicvoidrep(){this.setVisible(false);this.remove(panel);this.add(panel);}}
importjava.awt.Container;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjavax.swing.JFrame;importDBBean.DBBean;/*_ooOoo_o8888888o88"."88(|-_-|)O=/O____/`---'____.'\||//`./\|||:|||///_|||||-:-|||||-||\-///|||_|''---/''||.-__`-`___/-./___`..'/--.--`..__.""'<`.____<|>_/___.'>'"".||:`-`.;`_/`;.`/-`:||\`-.___/___/.-`//======`-.____`-.________/___.-`____.-'======`=---='^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^佛祖保佑永无BUG*/publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubMain_menumain_menu=newMain_menu();main_menu.setVisible(true);System.out.println("一次");}}
importjava.awt.Color;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JComboBox;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JTextField;importjavax.swing.event.AncestorEvent;importjavax.swing.event.AncestorListener;publicclassMenuextendsJPanelimplementsActionListener{privatestaticfinallongserialVersionUID=1L;privatedoublea;privatedoubleb;privateJTextFieldt1;privateJTextFieldt2;JComboBoxjcombobox;privateJLabeljlabel1;privateJLabeljlabel2;privateJLabeljlabel3;Panelpanel=newPanel();publicMenu(){this.setLayout(null);jcombobox=newJComboBox<>();jcombobox.addItem("银行");jcombobox.addItem("零售");jcombobox.setBounds(100,50,100,20);this.add(jcombobox);jlabel1=newJLabel("a=");jlabel1.setBounds(200,50,80,25);jlabel1.setBackground(Color.white);this.add(jlabel1);t1=newJTextField("10");t1.setBounds(300,50,80,20);t1.setVisible(true);t1.addActionListener(this);this.add(t1);this.setSize(1000,80);this.setBackground(Color.darkGray);}@OverridepublicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubif(e.getSource()==t1){panel.setPoint_num(Integer.parseInt(t1.getText()));panel.rep();Main_menum=newMain_menu();m.rep();System.out.println("回车");}}}
importjava.awt.BasicStroke;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.Collection;importjava.util.List;importjava.util.Map;importjava.util.TreeMap;importjavax.swing.JPanel;importDBBean.DBBean;publicclassPanelextendsJPanelimplementsKeyListener{List<Integer>l1=newArrayList<>();List<Integer>l2=newArrayList<>();staticintpoint_num=11;staticdoublemax;staticdoublemin;publicPanel(){addKeyListener(this);Mapmap=newTreeMap();this.setSize(1000,620);setBackground(newColor(0x22,0x22,0x22));//键值对拿出数据,存放两个list中DBBeand=newDBBean();Connectioncon=d.con();System.out.println(con);Stringsql="select*fromcountorderbybdesc";try{PreparedStatementps=con.prepareStatement(sql);ResultSetrs=ps.executeQuery();System.out.println(rs==null);while(rs.next()){//System.out.println("ssss"+rs.getInt(1));l1.add(rs.getInt(1));l2.add(rs.getInt(2));}}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}for(inti=0;i<l1.size();i++){System.out.println(l1.get(i));System.out.println(l2.get(i));System.out.println();}}publicintgetPoint_num(){returnpoint_num;}publicvoidsetPoint_num(intpoint_num){this.point_num=point_num;}@SuppressWarnings("null")publicvoidpaintComponent(Graphicsg){System.out.println("paint!!!!!!!!!!!!!!!");System.out.println("point_mun="+point_num);super.paintComponent(g);floatlineWidth=3.0f;((Graphics2D)g).setStroke(newBasicStroke(lineWidth));g.setColor(newColor(0x80,0x00,0x00));g.drawLine(0,390,1000,390);g.drawLine(0,81,1000,81);g.drawLine(0,708,1000,708);lineWidth=1.0f;((Graphics2D)g).setStroke(newBasicStroke(lineWidth));g.setColor(newColor(0xff,0x32,0x32));max=0;for(inti=0;i<point_num;i++){if(l1.get(i)>max)max=l1.get(i);}min=max;for(inti=0;i<point_num;i++){if(l1.get(i)<min)min=l1.get(i);}System.out.println("max"+max);System.out.println("min"+min);doublex=(max-min)/624;doubley=min/x;System.out.println(x+"xxxxxxxxxx");for(inti=0;i<point_num-1;i++){g.drawLine(i*100,((int)(l1.get(i)/x-y)+81),i*100+100,((int)(l1.get(i+1)/x-y)+81));System.out.println(i*100+""+l1.get(i)+""+i*100+100+""+l1.get(i+1));}}publicvoidrep(){revalidate();repaint();System.out.println("rep");}@OverridepublicvoidkeyTyped(KeyEvente){//TODOAuto-generatedmethodstub}@OverridepublicvoidkeyPressed(KeyEvente){//TODOAuto-generatedmethodstubintkeyCode=e.getKeyCode();if(keyCode==KeyEvent.VK_LEFT)System.out.println("left");if(keyCode==KeyEvent.VK_UP)System.out.println("left");if(keyCode==KeyEvent.VK_DOWN)System.out.println("left");if(keyCode==KeyEvent.VK_RIGHT)System.out.println("left");}@OverridepublicvoidkeyReleased(KeyEvente){//TODOAuto-generatedmethodstub}}
packageDBBean;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassDBBean{Stringdriver="com.mysql.jdbc.Driver";StringdbName="SetElement_Count";Stringpasswrod="123456";StringuserName="root";Stringurl="jdbc:mysql://localhost:3306/"+dbName;publicConnectioncon(){Connectionconn=null;try{Class.forName(driver);conn=DriverManager.getConnection(url,userName,passwrod);}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnconn;}}
问题1:运行后在Menu中的t1里输入数字回车后调用repaint,没有调用panel中的paintComponent,但是下拉菜单发生改变时,会调用paintComponent,并有打印,但是没有重画。问题2:给panel添加键盘监听事件,但是鼠标点击获取不到焦点,该怎么监听?
解决方案
解决方案二:
没有人在看么..........崩溃了.................
解决方案三:
sql的表结构和测试数据呢。