java - 在Java中设置文本字段的位置?

标签 java position jtextfield

我目前正在编写一个需要用户在框架内输入的程序。有没有一种简单的方法来设置输入文本字段的位置(在 X 和 Y 坐标中)? JTextField 的构造函数和修改器方法似乎无法执行此操作。

这是代码:

import java.awt.image.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.net.*;

class labMain extends Frame implements KeyListener, MouseListener, MouseMotionListener {

    String screenState;

    Color bfi = new Color(155, 48, 255);
    Color bfh = new Color(155, 48, 255, 200);
    Color bfs = new Color(155, 48, 255, 200);
    Color bbi = new Color(255, 255, 255);
    Color bbh = new Color(5, 12, 15);
    Color bbs = new Color(220, 20, 60);

    quadButton startSession, gotoUser, gotoInventory, gotoCalendar, gotoProcedures, gotoResults, gotoEmergencies;

    quadButton addUsers, findUsers;

    int mouseX, mouseY;

    javax.swing.Timer mainT;

    BufferedImage imageBuffer;
    Graphics2D  graphicsBuffer;
    boolean[] keys = new boolean[525];

    public labMain() {

        setTitle("uwDNA Lab Pro");
        setSize(new Dimension(1365, 765));

        this.addWindowListener (

                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        labMain.this.windowClosed();
                    }
                }

        );

        setFocusable(true);
        addKeyListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);

    }

    protected void windowClosed() { System.exit(0); }

    public void runLab() throws IOException {

        imageBuffer = (BufferedImage) createImage(getWidth(), getHeight());
        graphicsBuffer = (Graphics2D) imageBuffer.getGraphics();

        screenState = "Main";

        mainScreenInit();
        usersScreenInit();
        addUsersScreenInit();

        ActionListener mainS = new ActionListener() {

            public void actionPerformed(ActionEvent evt) {

                detectMouseHover();

                paint(getGraphics());

            }

        };

        mainT = new javax.swing.Timer(1, mainS);
        mainT.start();

    }

    public void detectMouseHover() {

        if (startSession.intersects(mouseX, mouseY)) {

            startSession.setState(aButton.isHover);

        }

        else {

            startSession.setState(aButton.isIdle);

        }

        if (gotoUser.intersects(mouseX, mouseY)) {

            gotoUser.setState(aButton.isHover);

        }

        else {

            gotoUser.setState(aButton.isIdle);

        }

        if (gotoInventory.intersects(mouseX, mouseY)) {

            gotoInventory.setState(aButton.isHover);

        }

        else {

            gotoInventory.setState(aButton.isIdle);

        }

        if (gotoCalendar.intersects(mouseX, mouseY)) {

            gotoCalendar.setState(aButton.isHover);

        }

        else {

            gotoCalendar.setState(aButton.isIdle);

        }

        if (gotoProcedures.intersects(mouseX, mouseY)) {

            gotoProcedures.setState(aButton.isHover);

        }

        else {

            gotoProcedures.setState(aButton.isIdle);

        }

        if (gotoResults.intersects(mouseX, mouseY)) {

            gotoResults.setState(aButton.isHover);

        }

        else {

            gotoResults.setState(aButton.isIdle);

        }

        if (gotoEmergencies.intersects(mouseX, mouseY)) {

            gotoEmergencies.setState(aButton.isHover);

        }

        else {

            gotoEmergencies.setState(aButton.isIdle);

        }

        if (addUsers.intersects(mouseX, mouseY)) {

            addUsers.setState(aButton.isHover);

        }

        else {

            addUsers.setState(aButton.isIdle);

        }

        if (findUsers.intersects(mouseX, mouseY)) {

            findUsers.setState(aButton.isHover);

        }

        else {

            findUsers.setState(aButton.isIdle);

        }

    }

    public void mainScreenInit() {

        startSession = new quadButton(getWidth() / 2, getHeight() / 2, 400, 200, 10, Font.BOLD, 40, "Start Lab Session", "Arial", bfi, bfh, bfs, bbi, bbh, bbs);

        int bbWidth = 200;
        int bbHeight = bbWidth / 2;
        int bbFontStyle = Font.PLAIN;
        int bbFontSize = 20;
        int bbBorderWidth = 5;
        String bbFontName = "Arial";

        gotoUser = new quadButton(((getWidth() - bbWidth * 6) / 7) * 1 + (bbWidth / 2 * 1), (getHeight() * 3 / 4) + 20, bbWidth, bbHeight, bbBorderWidth, bbFontStyle, bbFontSize, "Users", bbFontName, bfi, bfh, bfs, bbi, bbh, bbs);
        gotoInventory = new quadButton(((getWidth() - bbWidth * 6) / 7) * 2 + (bbWidth / 2 * 3), (getHeight() * 3 / 4) + 20, bbWidth, bbHeight, bbBorderWidth, bbFontStyle, bbFontSize, "Inventory", bbFontName, bfi, bfh, bfs, bbi, bbh, bbs);
        gotoCalendar = new quadButton(((getWidth() - bbWidth * 6) / 7) * 3 + (bbWidth / 2 * 5), (getHeight() * 3 / 4) + 20, bbWidth, bbHeight, bbBorderWidth, bbFontStyle, bbFontSize, "Calendar", bbFontName, bfi, bfh, bfs, bbi, bbh, bbs);
        gotoProcedures = new quadButton(((getWidth() - bbWidth * 6) / 7) * 4 + (bbWidth / 2 * 7), (getHeight() * 3 / 4) + 20, bbWidth, bbHeight, bbBorderWidth, bbFontStyle, bbFontSize, "Procedures", bbFontName, bfi, bfh, bfs, bbi, bbh, bbs);
        gotoResults = new quadButton(((getWidth() - bbWidth * 6) / 7) * 5 + (bbWidth / 2 * 9), (getHeight() * 3 / 4) + 20, bbWidth, bbHeight, bbBorderWidth, bbFontStyle, bbFontSize, "Results", bbFontName, bfi, bfh, bfs, bbi, bbh, bbs);
        gotoEmergencies = new quadButton(((getWidth() - bbWidth * 6) / 7) * 6 + (bbWidth / 2 * 11), (getHeight() * 3 / 4) + 20, bbWidth, bbHeight, bbBorderWidth, bbFontStyle, bbFontSize, "Emergencies", bbFontName, bfi, bfh, bfs, bbi, bbh, bbs);

    }

    public void usersScreenInit() {

        int ubWidth = 400;
        int ubHeight = ubWidth / 2;
        int ubFontStyle = Font.BOLD;
        int ubFontSize = 20;
        int ubBorderWidth = 10;
        String ubFontName = "Arial";

        addUsers = new quadButton(((getWidth() - ubWidth * 2) / 3) * 1 + (ubWidth / 2 * 1), (getHeight() * 9 / 16) + 20, ubWidth, ubHeight, ubBorderWidth, ubFontStyle, ubFontSize, "Add Users", ubFontName, bfi, bfh, bfs, bbi, bbh, bbs);
        findUsers = new quadButton(((getWidth() - ubWidth * 2) / 3) * 2 + (ubWidth / 2 * 3), (getHeight() * 9 / 16) + 20, ubWidth, ubHeight, ubBorderWidth, ubFontStyle, ubFontSize, "Find Users", ubFontName, bfi, bfh, bfs, bbi, bbh, bbs);

    }

    public void addUsersScreenInit() {



    }

    public void keyPressed(KeyEvent e) {



    }

    public void keyReleased(KeyEvent e) {



    }

    public void keyTyped(KeyEvent e) {}

    public void mousePressed(MouseEvent e) {



    }

    public void mouseReleased(MouseEvent e) {



    }

    public void mouseEntered(MouseEvent e) {



    }

    public void mouseExited(MouseEvent e) {



    }

    public void mouseClicked(MouseEvent e) {

        if (screenState == "Main") {

            if (gotoUser.intersects(e.getX(), e.getY())) {

                screenState = "Users";

            }

        }

        if (screenState == "Users") {

            if (addUsers.intersects(e.getX(), e.getY())) {

                screenState = "addUsers";

            }

        }

    }

    public void mouseMoved(MouseEvent e) {

        mouseX = e.getX();
        mouseY = e.getY();

    }

    public void mouseDragged(MouseEvent e) {



    }

    public void paint(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;

        Color bkgBlue = new Color(102, 170, 205);

        graphicsBuffer.setColor(bkgBlue);
        graphicsBuffer.fillRect(0, 0, getWidth(), getHeight());

        setBackground(bkgBlue);

        BufferedImage bkg;

        if (screenState.equals("Main")) {

            try {

                graphicsBuffer.setColor(Color.white);
                graphicsBuffer.fillRect((getWidth() / 2) - 455, (getHeight() / 2) - 355, 910, 175);

                URL url = new URL("http://i.imgur.com/iLm99QZ.jpg?1?9353");
                bkg = javax.imageio.ImageIO.read(url);
                graphicsBuffer.drawImage(bkg, (getWidth() / 2) - 450, (getHeight() / 2) - 350, this);

            }
            catch (IOException e) { System.err.println(e); }

            startSession.paint(graphicsBuffer);
            gotoUser.paint(graphicsBuffer);
            gotoInventory.paint(graphicsBuffer);
            gotoCalendar.paint(graphicsBuffer);
            gotoProcedures.paint(graphicsBuffer);
            gotoResults.paint(graphicsBuffer);
            gotoEmergencies.paint(graphicsBuffer);

        }

        if (screenState.equals("Users")) {

            try {

                graphicsBuffer.setColor(Color.white);
                graphicsBuffer.fillRect((getWidth() / 2) - 455, (getHeight() / 2) - 355, 910, 175);

                URL url = new URL("http://i.imgur.com/nUrYx7q.jpg?1");
                bkg = javax.imageio.ImageIO.read(url);
                graphicsBuffer.drawImage(bkg, (getWidth() / 2) - 450, (getHeight() / 2) - 350, this);

            }
            catch (IOException e) { System.err.println(e); }

            addUsers.paint(graphicsBuffer);
            findUsers.paint(graphicsBuffer);

        }

        if (screenState.equals("addUsers")) {

            try {

                graphicsBuffer.setColor(Color.white);
                graphicsBuffer.fillRect((getWidth() / 2) - 455, (getHeight() / 2) - 355, 910, 175);

                URL url = new URL("http://i.imgur.com/GDCTw65.jpg?1");
                bkg = javax.imageio.ImageIO.read(url);
                graphicsBuffer.drawImage(bkg, (getWidth() / 2) - 450, (getHeight() / 2) - 350, this);

            }
            catch (IOException e) { System.err.println(e); }

        }

        g2.drawImage(imageBuffer, 10, 25, getWidth(), getHeight(), this);

    }

}

最佳答案

要设置位置,只需使用此代码

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

                 class SetTextfield extends JFrame
                 {

                    JTextField t1,t2;
                 public SetTextfield()
                 {
                   t1=new JTextfield();
                   t2=new JTextField();
                   t1.setBounds(100,100,200,40);
                   t2.setBounds(180,100,200,40);
                   add(t1);
                    add(t2);
                  }
                  public static void main(String []args)
                  {
                     SetTextfield obj=new SetTextfield();
                      obj.setSize(500,500);
                      obj.setVisible(true);
                       obj.setResizable(false);
                  }
                  }                    

关于java - 在Java中设置文本字段的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24842068/

相关文章:

java - HashSet<List<Integer>> 时间复杂度

java - 在时区之间转换纪元毫秒

html - html 对象标签的滚动位置

java - 设置默认 LAF 的 JTextField 尺寸

java - 单击 JTextField 是否调用 actionPerformed 方法

java - 删除 Eclipse 中行左侧的绿色十字

java - IPv6 被编码在请求参数中

ios - iBeacon 三边测量并在 map iOS 上显示用户的位置

Java - 从方法的参数获取数组位置

java - 如何向 JCombobox 添加填充