import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JFrame
public class AddButtons extends JFrame implements ActionListener {
private static final long serialVersionUID = 5322709709362727424L
private JButton[] jb = new JButton[20]
public AddButtons() {
this.setLayout(new FlowLayout())
this.setSize(800, 600)
this.setBounds(10, 10, 800, 600)
for (int i = 0i <jb.lengthi++) {
jb[i] = new JButton("按钮" + i)
jb[i].addActionListener(this)
this.add(jb[i])
}
this.setVisible(true)
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
}
public static void main(String[] args) {
new AddButtons()
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0i <jb.lengthi++) {
if (e.getSource().equals(jb[i])) {
System.out.println(i)
}
}
}
}
如果您想在Unity中,通过点击Button来移动ScrollView的内容,可以按照以下步骤进行:1. 在Unity UI中创建一个ScrollView和一些按钮Button,将它们排列在ScrollView的Content区域内。
2. 为每个按钮Button添加一个OnClick事件监听器,当点击该按钮时就会调用相应的方法。
3. 创建一个脚本,绑定到ScrollView的Content对象上,并在其中编写一个方法,使得当点击按钮时,ScrollView的Content向指定方向滚动一定距离。
4. 在方法中,获取ScrollView的RectTransform组件和Content对象的RectTransform组件,并根据需要计算出Content需要滚动的距离。
5. 调用ScrollView的ScrollTo方法,将Content滚动到指定位置即可。
以下是一份示例代码,仅供参考:
```csharp
using UnityEngine
using UnityEngine.UI
public class ScrollViewMove : MonoBehaviour
{
public ScrollRect scrollView
public float moveDistance = 100f
public void MoveLeft()
{
Vector2 pos = scrollView.content.anchoredPosition
pos.x += moveDistance
scrollView.content.anchoredPosition = pos
}
public void MoveRight()
{
Vector2 pos = scrollView.content.anchoredPosition
pos.x -= moveDistance
scrollView.content.anchoredPosition = pos
}
public void MoveUp()
{
Vector2 pos = scrollView.content.anchoredPosition
pos.y -= moveDistance
scrollView.content.anchoredPosition = pos
}
public void MoveDown()
{
Vector2 pos = scrollView.content.anchoredPosition
pos.y += moveDistance
scrollView.content.anchoredPosition = pos
}
}
```
在该示例代码中,我们通过定义四个移动方法 MoveLeft、MoveRight、MoveUp 和 MoveDown ,并为每个按钮添加一个 OnClick 事件监听器,当用户点击按钮时,将触发相应的方法,从而实现了通过点击按钮移动 ScrollView 内容的功能。
你a[i][j].addActionListener(this)这句就是给按钮加监听啊,你想给哪些按钮加就在哪些按钮上调用addActionListener(this)方法。你的类还实现ActionListener接口,并补全actionPerformed方法,添加监听的方法才不会报错。
有问题的话再问,把问题描述的具体些。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)