问题1:
我正在尝试设置按钮的大小,但似乎没有改变,而是总是填满整个屏幕,这不是我想要的。
问题2:
当我在消息中添加 public void actionPerformed1(ActionEvent e) 时,它似乎没有显示。也许是因为按钮填满了整个空间,但我真的不知道。
这是按钮的代码:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class HockeyGame extends JFrame implements
ActionListener, KeyListener, Runnable, WindowListener //, ActionListener
{
Thread t;
int xpuck, ypuck,xspeed,yspeed,gx,gy,redx2,redy2,lg,rg, redscore, bluescore,
redx,redy, gx2, gy2, bluex, bluey,bluex2,bluey2,rg2,lg2;
//int SQUARE_SIZE=10;
public HockeyGame ( ) {
}
public static void main ( String [ ] commandLine ) {
HockeyGame hg=new HockeyGame();
hg.init();
}
public void actionPerformed1(ActionEvent e) {
JOptionPane.showMessageDialog(null,"testing");
}
public void init() {
Button b;
b = new Button("Click me");
b.addActionListener(this);
b.setSize(40, 40);
add(b);
t=new Thread(this);
this.addKeyListener(this);
this.setFocusable(true);
setSize(700,700);
setVisible ( true );
任何帮助表示感谢
最佳答案
从简单的练习开始,让您的基础知识更加扎实。
1) 您的按钮填满了空间,因为内容 Pane 的默认布局是边框布局。将布局设置为“流布局”或任何其他布局。
2)函数名称应与Action Listener中定义的完全一致。
public void actionPerformed(ActionEvent e)
{}
除非您掌握了基础知识,否则我所说的一切都没有意义。
关于java - 设置按钮和 Action 监听器的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8874799/