简体   繁体   English

设置按钮和动作侦听器的大小

[英]Setting the size of a button and action listener

Problem 1: 问题1:

I'm trying to set the size of my button, but doesn't seem to change and instead always fills the entire screen which isn't what I want. 我正在尝试设置按钮的大小,但似乎没有改变,而是总是填满整个屏幕,这不是我想要的。

Problem 2: 问题2:

When I add the public void actionPerformed1(ActionEvent e) with my message, it doesn't seem to show up. 当我用我的消息添加public void actionPerformed1(ActionEvent e)时,它似乎没有显示出来。 Maybe it's because of the button filling the entire space up, but I don't really know. 也许是因为按钮填满了整个空间,但我真的不知道。

Here is the code for the buttons: 这是按钮的代码:

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

Any help is appreciatedasd 任何帮助都是值得赞赏的

Start with simple exercises to make you strong in the basics. 从简单的练习开始,让您在基础知识方面表现出色。

1) Your Button fills up the space because the default layout of your content pane is Border Layout. 1)您的按钮填满了空间,因为内容窗格的默认布局是边框布局。 Set the layout to Flow Layout or any other. 将布局设置为Flow Layout或任何其他布局。

2) The function name should be exactly the same as that defined in Action Listener. 2)函数名称应与Action Listener中定义的函数名称完全相同。


public void actionPerformed(ActionEvent e)
{}

None of what I said will make sense until you are strong with the basics. 在你坚持基础知识之前,我所说的一切都没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM