是否有可能在java中永久setLocation按钮

时间:2016-02-15 10:42:01

标签: java

我是Java的新手,我试图创建一个无法点击按钮的应用程序。当鼠标悬停在按钮上时,按钮位置将随机变化。首先我打开了一个JForm,我没有手动创建按钮。

我正在处理mouseEntered事件,这些是我的代码;

 int x = 100, y = 100;
Random r1 = new Random();
Random r2 = new Random();
private void jButton1MouseExited(java.awt.event.MouseEvent evt) {                                     
    jButton1.setLocation(x, y);

}                                    

private void jButton1MouseEntered(java.awt.event.MouseEvent evt) {                                      
    x = r1.nextInt(300);
    y = r2.nextInt(300);
    jButton1.setLocation(x, y);
}        

这是我程序中的所有代码。另一部分是InitComponents。我只是尝试过不使用随机。 这是问题,代码更改位置。然而,当我移动鼠标时,按钮位置变为旧位置(我的意思是它转回默认位置)。我认为它是关于Java布局的东西。另外我设置按钮的位置特别像300,400但仍然是同样的问题。当光标移动时,按钮仍然会回到旧位置,这就是为什么我认为我可以说我的问题不是随机创建新位置。乙 所以你有什么想法吗?

编辑---

感谢你的回答@Mohith P.我开了一个课程并编写了你给我的代码。它在这里工作的代码;

public class Unclickable extends JFrame implements ActionListener {

    JFrame frm;
    JButton btn;
    JPanel pnl;

    int x = 100;
    int y = 100;

    Random rnd = new Random();

    public Unclickable() {
        frm = new JFrame();
        btn = new JButton("A-A");
        pnl = new JPanel();

        btn.setLocation(x, y);
        btn.setPreferredSize(new Dimension(50, 50));

        pnl.setSize(400, 400);
        pnl.setLocation(100, 100);
        pnl.add(btn);


        btn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                x = rnd.nextInt(300);
                y = rnd.nextInt(300);
                btn.setLocation(x, y);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btn.setLocation(x, y);
            }
        });

        frm.setSize(500, 500);
        frm.add(pnl);
        frm.setVisible(true);


    }
@Override
    public void actionPerformed(ActionEvent ae) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    public static void main(String[] args) {
        Unclickable a = new Unclickable();
    }

就像说它有效但如果使用" JFrame Form"它仍然无法正常工作。我还是不明白为什么。它可能适用于InitCompenent

中的这些代码
  layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(99, 99, 99)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(109, Short.MAX_VALUE))
        );

我试图设置标签的位置,没有问题,但按钮是大问题。它可以是关于grouplayout(JButton布局的类型)吗?最后我使用面板,我在面板中添加了按钮,仍然相同:/

最终编辑

我解决了这个问题。问题真的很有趣:)我写了一个代码,它给了我按钮位置。

private void formMouseMoved(java.awt.event.MouseEvent evt) {                                
        jLabel1.setText(jButton1.getLocation().x + "");
        jLabel2.setText(jButton1.getLocation().y + "");
    }                              

该代码的原因我无法改变按钮的位置。我写道,我想在它改变时按钮位置。就像我说它对我有趣:)

1 个答案:

答案 0 :(得分:1)

在您的代码中,您尝试根据随机生成的值设置按钮位置,因此按钮位置会随机变化。而不是使用随机值,您只需使用一些固定值作为按钮位置。此外,在上面的代码中,当鼠标进入被调用的事件时,它会随机生成位置值并更改位置。

试试这段代码,

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
import java.lang.Math.*;

class Tst extends JFrame implements ActionListener
{
    JFrame f1;
    JPanel p1;
    JButton b1;
    int x=100,y=100;
    Random rand = new Random();
    Tst()
    {


        f1=new JFrame("Sample");
        p1=new JPanel();
        b1=new JButton("B1");
        b1.setLocation(x, y);
        b1.setPreferredSize(new Dimension(100, 50));

        p1.setSize(500,500);
        p1.setLocation(100,100);
        p1.add(b1);

        //b1.addActionListener(this);
        b1.addMouseListener(new java.awt.event.MouseAdapter() 
        {
            public void mouseEntered(java.awt.event.MouseEvent evt) 
            {
                //jButton1.setBackground(Color.GREEN);
                x=rand.nextInt(10);
                y=rand.nextInt(10);
                b1.setLocation(x, y);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) 
            {
                //jButton1.setBackground(UIManager.getColor("control"));
                b1.setLocation(x, y);
            }
        });

        f1.setSize(500,500);
        f1.setLayout(new GridLayout(1,1));
        f1.add(p1);
        f1.setVisible(true);
        f1.addWindowListener (new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                f1.setVisible(false);
                System.exit(0);
            }
        });
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==b1)
        {

        }
    }

    public static void main(String args[])
    {
        Tst t=new Tst();
    }

}
相关问题