简体   繁体   English

在paintComponent绘制形状上添加按钮

[英]Adding buttons on a paintComponent drawn shape

My program is consist of two classes(test and paintClass) in different files. 我的程序由不同文件中的两个类(test和paintClass)组成。 In the paintClass class I draw a 5x5 square board by using paintComponent method. 在paintClass类中,我使用paintComponent方法绘制了一个5x5的正方形板。 I want to add buttons in each small square in the big square. 我想在大方块的每个小方块中添加按钮。 When I run the code I don't get any buttons. 当我运行代码时,我没有任何按钮。 I want to have 25(5x5) buttons by using jpanel on a shape drawn by paintComponent. 我想通过在paintComponent绘制的形状上使用jpanel来具有25(5x5)个按钮。 Is this possible? 这可能吗? If it is, how I can do it? 如果是,我该怎么办?

EDIT : The problem was the loop. 编辑:问题是循环。 Number had a default value of 0 so the loop didn't work. Number的默认值为0,因此循环无效。 I defined number at the beginning. 我一开始就定义了数字。 It solved the problem. 它解决了问题。 Also one of the invervals were wrong. 同样,其中之一是错误的。 I changed j = 0 with j = 1. 我将j = 1更改为j = 0。

import javax.swing.*;
import java.awt.*;

public class test
{

   public static void main(String[] args) 
   {

       JFrame frame = new JFrame("buttons");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(250,250);
       PaintClass paint = new PaintClass();
       paint.repaint();
       f1.getContentPane().add(paint);
       frame.pack();
       frame.setVisible(true);
    }
}



import javax.swing.*;
import java.awt.*;

public class PaintClass extends JPanel
{
    private Graphics g;
    private int interval,side,number;
    private JButton button;

    public PaintClass() 
    {
        number = 5;
        button = new JButton();
        setLayout(new GridLayout(5,5));
        for(int i = 0; i <= number - 1; i++)
        {
            for(int j = 1; j <= number - 1; j++)
            {
                button = new JButton();//ADDED
                button.setBounds(i * interval, 0, interval, interval);
                add(button);
            }
            button = new JButton();//ADDED
            button.setBounds(0, i * interval, interval, interval);
            add(button);
        }
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        this.repaint();
        side = 250;
        number = 5;
        interval = side / number;
        g.drawRect(0,0, side, side);

        for(int i = 0; i <= number - 1; i++)
        {
            for(int j = 0; j <= number - 1; j++)
            {
                 g.drawLine(i * interval, 0, i * interval, side);
            }
            g.drawLine(0, i * interval, side, i * interval);
        }

    }   
}
private int interval,side,number;

Number has a default value of 0. Number的默认值为0。

for(int i = 0; i <= number - 1; i++)

Since number is 0, your loop will never execute. 由于number为0,因此循环将永远不会执行。

Once you do this the buttons will be added to the panel but they will cover your custom painting. 完成此操作后,按钮将添加到面板中,但它们将覆盖您的自定义绘画。 To see background lines you just need to set the background of the panel to Color.BLACK and then create your GridLayout with a gap between the components. 要查看背景线,只需将面板的背景设置为Color.BLACK,然后创建GridLayout,并在组件之间留有空隙。 Read the API for the method to use. 阅读API以了解要使用的方法。

Choose one or the other: either add the buttons using the GridLayout, or paint the buttons using paintComponent. 选择一个或另一个:使用GridLayout添加按钮,或使用paintComponent绘制按钮。 If the former, you should a) define the loop constraint (right now it is 0) b) create a new JButton for every loop (your code currently reuses the instance) and c) register the appropriate ActionListener to respond to events. 如果是前者,则应a)定义循环约束(现在为0)b)为每个循环创建一个新的JButton(您的代码当前重用该实例),以及c)注册适当的ActionListener来响应事件。 If the latter, you need to register the appropriate listener (like MouseListener ) to respond to user generated events. 如果是后者,则需要注册适当的侦听器(如MouseListener )以响应用户生成的事件。

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

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