Java 使用JFrame创建颜色网格?

Java 使用JFrame创建颜色网格?,java,swing,colors,jframe,Java,Swing,Colors,Jframe,有没有办法在JFrame中创建颜色网格(8x12)?我一直在研究一个项目,到目前为止还没有找到太多的帮助。如果有人能给我提供任何文档链接,那将是非常感谢 我不太确定您要的是什么,但如果您要在JFrame中显示8x12颜色网格,那么我可以提供答案 您需要使用paint方法,并在该方法中放置两个for循环。下面是一个例子: import javax.swing.*; import java.awt.*; public class myJFrame extends JFrame{ public

有没有办法在JFrame中创建颜色网格(8x12)?我一直在研究一个项目,到目前为止还没有找到太多的帮助。如果有人能给我提供任何文档链接,那将是非常感谢

我不太确定您要的是什么,但如果您要在JFrame中显示8x12颜色网格,那么我可以提供答案

您需要使用paint方法,并在该方法中放置两个for循环。下面是一个例子:

import javax.swing.*;
import java.awt.*;
public class myJFrame extends JFrame{
    public void paint(Graphics g) {//This method is what paints the JFrame, as its name implies.
        super.paint(g);
        int sizeX = this.getWidth()/8;//Takes the size of current JFrame
        int sizeY=this.getWidth()/12;
        for(int x=0;x<sizeX;x++)//These for loops go through an 8x12 grid of the JFrame
            for(int y=0;y<sizeY;y++) {
                g.setColor(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));//This just creates a random color, you can replace this with anything else
                g.fillRect(x*sizeX,y*sizeY,sizeX,sizeY);//Creates the grid
            }
    }
    public static void main(String[] args) {
        JFrame f=new myJFrame();//If you already have a JFrame implemented, you'll have to replace its declaration with this line of code
        //f.setUndecorated(true);//If you want evenly distributed grids, uncomment this line.
        f.setVisible(true);//Sets the JFrame to be visible
        f.setSize(80,120);//This size is arbitrary, you can replace it with anything, and the program will still work
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //If you want the grid to be perfectly distributed, then uncomment the line below.

    }
}
import javax.swing.*;
导入java.awt.*;
公共类myJFrame扩展了JFrame{
public void paint(Graphics g){//正如其名称所示,此方法用于绘制JFrame。
超级油漆(g);
int sizeX=this.getWidth()/8;//采用当前JFrame的大小
int sizeY=this.getWidth()/12;

对于(int x=0;x我不太确定您要求的是什么,但如果您要求在JFrame中显示8x12颜色网格,那么我可以提供答案

您需要使用paint方法,并在该方法中放置两个for循环。以下是一个示例:

import javax.swing.*;
import java.awt.*;
public class myJFrame extends JFrame{
    public void paint(Graphics g) {//This method is what paints the JFrame, as its name implies.
        super.paint(g);
        int sizeX = this.getWidth()/8;//Takes the size of current JFrame
        int sizeY=this.getWidth()/12;
        for(int x=0;x<sizeX;x++)//These for loops go through an 8x12 grid of the JFrame
            for(int y=0;y<sizeY;y++) {
                g.setColor(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));//This just creates a random color, you can replace this with anything else
                g.fillRect(x*sizeX,y*sizeY,sizeX,sizeY);//Creates the grid
            }
    }
    public static void main(String[] args) {
        JFrame f=new myJFrame();//If you already have a JFrame implemented, you'll have to replace its declaration with this line of code
        //f.setUndecorated(true);//If you want evenly distributed grids, uncomment this line.
        f.setVisible(true);//Sets the JFrame to be visible
        f.setSize(80,120);//This size is arbitrary, you can replace it with anything, and the program will still work
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //If you want the grid to be perfectly distributed, then uncomment the line below.

    }
}
import javax.swing.*;
导入java.awt.*;
公共类myJFrame扩展了JFrame{
public void paint(Graphics g){//正如其名称所示,此方法用于绘制JFrame。
超级油漆(g);
int sizeX=this.getWidth()/8;//采用当前JFrame的大小
int sizeY=this.getWidth()/12;

对于(int x=0;x
someColorType[]]colors=new sameColorType[8][12];
数组。
someColorType[]]colors=new sameColorType[8][12];
数组。这真的很有帮助,正是我想要做的,谢谢!这真的很有帮助,正是我想要做的,谢谢!