gpt4 book ai didi

java - 是否有可能在java中永久设置按钮的位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:56 25 4
gpt4 key购买 nike

我是 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 + "");
}

由于该代码,我无法更改按钮的位置。我写了它,我想在它改变时按钮位置。就像我说的,这对我来说很有趣 :)

最佳答案

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

试试这段代码,

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();
}

}

关于java - 是否有可能在java中永久设置按钮的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35407194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com