如何获取要在textfield中显示的字符串的值

时间:2015-03-08 03:26:32

标签: java

在此先感谢我需要在文本字段中显示x1和y1值 当执行mouseclick事件时,请帮助解决我的问题   / *  *要更改此模板,请选择“工具”|模板  *并在编辑器中打开模板。  * /       包com.zetcode;

  import java.awt.Color;
  import java.awt.Font;
   import java.awt.Graphics;
   import java.awt.Graphics2D;
   import java.awt.event.MouseAdapter;
   import java.awt.event.MouseEvent;
   import java.util.Random;
   import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
     import javax.swing.SwingUtilities;


 public class NewClass extends JFrame{

  static final String AB = "1234";
  static Random rnd = new Random();
  int len;
  StringBuilder sb = new StringBuilder( len );
  static  String as;
   static  String as1;




   String randomString( int len ) 
    {

  for( int i = 0; i < len; i++ ) {
  //  sb.append( AB.charAt( rnd.nextInt(AB.length()) ) );
         System.out.print( "1st"+ sb.append( AB.charAt(    rnd.nextInt(AB.length())    ) ));
}
  return sb.toString();
   }



public NewClass(){
this.addMouseListener(new NewClass.MyMouseListener());

repaint();
 initUI();

  }

private void doDrawing(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

Graphics2D     g2d1 = (Graphics2D) g;
      Font font = new Font("Arial Black", Font.PLAIN, 20);




    g2d.setColor(new Color(212, 212, 212));
    g2d.drawRect(10, 15, 90, 60);
    g2d.drawRect(130, 15, 90, 60);
    g2d.drawRect(250, 15, 90, 60);
    g2d.drawRect(10, 105, 90, 60);
    g2d.drawRect(130, 105, 90, 60);
    g2d.drawRect(250, 105, 90, 60);
    g2d.drawRect(10, 195, 90, 60);
    g2d.drawRect(130, 195, 90, 60);
    g2d.drawRect(250, 195, 90, 60);
    g2d.setColor(new Color(125, 167, 116));
    g2d.fillRect(10, 15, 90, 60);



    g2d1.setColor(new Color(1, 1, 1));
    g2d1.setFont(font);
         g2d1.drawString("1st"+ sb.append( AB.charAt( rnd.nextInt(AB.length()) ) ), 15, 60);





    g2d.setColor(new Color(42, 179, 231));
    g2d.fillRect(130, 15, 90, 60);
    g2d.drawString("sdsa", 50, 60);




    g2d.setColor(new Color(70, 67, 123));
    g2d.fillRect(250, 15, 90, 60);

    g2d.setColor(new Color(130, 100, 84));
    g2d.fillRect(10, 105, 90, 60);

    g2d.setColor(new Color(252, 211, 61));
    g2d.fillRect(130, 105, 90, 60);

    g2d.setColor(new Color(241, 98, 69));
    g2d.fillRect(250, 105, 90, 60);

    g2d.setColor(new Color(217, 146, 54));
    g2d.fillRect(10, 195, 90, 60);

    g2d.setColor(new Color(63, 121, 186));
    g2d.fillRect(130, 195, 90, 60);

    g2d.setColor(new Color(31, 21, 1));
    g2d.fillRect(250, 195, 90, 60);


    g2d.setColor(new Color(31, 21, 1));
    g2d.fillRect(370, 15, 90, 60);

    g2d.setColor(new Color(252, 211, 61));
    g2d.fillRect(370, 105, 90, 60);


    g2d.setColor(new Color(130, 100, 84));
    g2d.fillRect(370, 196, 90, 60);

     g2d.setColor(new Color(42, 179, 231));
    g2d.fillRect(10, 280, 90, 60);

    g2d.setColor(new Color(130, 100, 84));
    g2d.fillRect(130, 280, 90, 60);

     g2d.setColor(new Color(217, 146, 54));
    g2d.fillRect(250, 280, 90, 60);

     g2d.setColor(new Color(217, 146, 54));
    g2d.fillRect(370, 280, 90, 60);

     }






 private  void  initUI() {

     DrawPanel dpnl = new DrawPanel();

     getContentPane().add(dpnl);

     dpnl.setLayout(null);
    add(dpnl);
    String df="sdda";
     JTextField  ta =new JTextField();
     ta.setText(df);
    ta.setSize(100, 100);
   ta.setVisible(true);
   ta.setBounds(500, 100, 100, 30);
    dpnl.add(ta);
    setSize(700, 400);
    setTitle("Rectangles");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


     }

     class MyMouseListener extends  MouseAdapter{
     @Override
       public void mouseClicked(MouseEvent e) {
         int  x1 = e.getX();
         int  y1 = e.getY();

         System.out.println(x1);
         System.out.println(y1);

   e.getComponent().repaint();




         //starting if loop
         if((x1>=0 && x1<=101)&&(y1>=0 && y1<=75)){
            System.out.println("less than green"); 
            x1=1; y1=1;
            as= String.valueOf(x1);
           as1= String.valueOf(y1);
            System.out.println(x1);
            System.out.println(y1);



         }


         }

         public void setLatdegreeText(String as){
        System.out.println("TextField text changed to " + as);
      }
        }


     private static class g2d1 {

    public g2d1() {
    }
     }

     public static void main(String[] args) {

     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            NewClass ex = new NewClass();
            ex.setVisible(true);
        }
        });  
       }

        }

1 个答案:

答案 0 :(得分:0)

将其放入鼠标监听器

int x1, y1;
String x1Word, y1Word;
x1Word = Integer.toString(x1);
y1Word = Integer.toString(y1);
jTextField.setText(x1Word + " " + y1Word);