无法在矩形顶部创建按钮

时间:2017-06-10 13:20:02

标签: java swing user-interface jframe jpanel

这是一段代码:

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class QuitButton extends JPanel implements ActionListener
{
    static JButton button = new JButton("Panic");
    Color[] colors = new Color[9];

    boolean pressed = false;

    public QuitButton()
    {
        button.addActionListener(this);
        colors[0] = Color.RED;
        colors[1] = Color.BLUE;
        colors[2] = Color.GREEN;
        colors[3] = Color.YELLOW;
        colors[4] = Color.BLACK;
        colors[5] = Color.PINK;
        colors[6] = Color.MAGENTA;
        colors[7] = Color.ORANGE;
        colors[8] = Color.CYAN;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        pressed = true;
    }

    public static void main(String args[])
    {
        JFrame frame = new JFrame("Do NOT Panic!!");
        QuitButton qb = new QuitButton();   
        frame.add(qb);
        frame.add(button);
        frame.setLayout(new FlowLayout());
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        //frame.pack();
        button.requestFocus();
        qb.gameLoop();
    }

    public void gameLoop()
    {
        while (true)
        {
            repaint();
            try
            {
                Thread.sleep(200);
            } catch (Exception e)
            {
            }
        }
    }

    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        if (pressed == false)
        {
            super.paint(g2d);
            g2d.setColor(Color.GRAY);
            g2d.fillRect(0, 0, 400, 400);
        } else
        {
            super.paint(g2d);
            Random r = new Random();
            int min = 0;
            int max = 8;
            int index = r.nextInt(max - min) + min;
            g2d.setColor(colors[index]);
            g2d.fillRect(0, 0, 400, 400);
        }
    }

此程序的目的:矩形应该是灰色的,但是当我点击紧急按钮时颜色应该开始变化 请不要混淆QuitButton类的名称 但我的矩形并没有占据整个窗口。相反,我得到一个像这样的小小矩形:http://g.recordit.co/xJAMiQu6fM.gif 我认为这是因为我使用的布局,我没有指定任何按钮将位于顶部。可能这就是他们并肩前进的原因。我是GUI创建的新手,感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您似乎在猜测如何执行此操作,这不是学习使用库的好方法。你的第一步应该是检查相关的教程,其中大部分都可以在这里找到:Swing Info由于这似乎是作业,我不打算给你一个代码解决方案,而是建议你如何改进:

  • 覆盖paintComponent,而不是绘制,因为后者提供双缓冲并且风险较小(边框和子组件问题的绘制较少)
  • 在paintComponent覆盖中,请务必先调用super的paintComponent方法来清除"脏"像素。
  • 使用Swing Timer,而不是游戏循环的while循环。这将阻止你的while循环冻结Swing事件线程,这个问题可能会冻结你的程序。谷歌的教程非常有帮助。
  • 在ActionListener的代码中进行随机化(这里可能是Swing Timer的ActionListener),而不是在绘图代码中。绘画代码不应该改变对象的状态,而应该只显示对象的状态。
  • FlowLayout将尊重组件的preferredSize,并且您组件的首选大小为0,0或接近它。改变这个。最好覆盖public Dimension getPreferredSize()并返回与矩形大小相匹配的维度。
  • 避免使用"魔法"数字,例如矩形的大小,而是使用常量或字段。
  • 在Timer的ActionListener中调用repaint(),以便JVM知道绘制组件。