在 Java JFrame 中绘制多个正方形并访问其中的每一个?

发布于 2025-01-03 03:41:44 字数 139 浏览 2 评论 0原文

我基本上想将 300 x 300 JFrame 窗口分成大约 30 个正方形(无需单独绘制它们)并能够访问每个正方形(也许将它们放入数组/数组列表/堆栈/向量/等中)。如果不写drawRect(x, y, width, height) 30次,这可能吗?提前致谢。

I basically want to divide a 300 by 300 JFrame window into like 30 squares (without drawing them all separately) and be able to access each of the squares (maybe put them into an array/arraylist/stack/vector/etc). Is this possible without writing drawRect(x, y, width, height) 30 times? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您可以将组件保存在 ArrayList 中并使用 GridLayout
和以下代码片段:

getContentPane.setLayout(new GridLayout(x,y));

其中 x 和 y 分别表示行数和列数。
之后,您可以将组件添加到 JFrame 对象的内容中。

示例代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Window;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyFrame extends JFrame
{
    public static void main ( String [] args )
    {
        MyFrame myframe = new MyFrame();
        myframe.setSize( 300, 300 );
        myframe.setResizable( false );
        myframe.setLocationRelativeTo( null );
        myframe.setLayout( new GridLayout(3,3) );

        Container container = myframe.getContentPane();
        ArrayList < JPanel > components = new ArrayList < JPanel >();
        JPanel temp = null;

        // Populating Arraylist object.
        for ( int i = 0; i < 9; i++ )
        {
            temp = new JPanel();
            temp.setSize( 100,100 );
            components.add( temp );
            container.add(temp);
        }

        myframe.pack();
        myframe.setVisible( true );

        // Accessing components via index.
        components.get( 5 ).setBackground( Color.BLUE );
        components.get( 8 ).setBackground( Color.GREEN );
    }
}

You can keep your components in an ArrayList and get use of GridLayout
and the following snippet:

getContentPane.setLayout(new GridLayout(x,y));

where x and y denotes the number of rows and columns respectively.
After that you can add components to the content of your JFrame object.

Sample code:

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Window;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyFrame extends JFrame
{
    public static void main ( String [] args )
    {
        MyFrame myframe = new MyFrame();
        myframe.setSize( 300, 300 );
        myframe.setResizable( false );
        myframe.setLocationRelativeTo( null );
        myframe.setLayout( new GridLayout(3,3) );

        Container container = myframe.getContentPane();
        ArrayList < JPanel > components = new ArrayList < JPanel >();
        JPanel temp = null;

        // Populating Arraylist object.
        for ( int i = 0; i < 9; i++ )
        {
            temp = new JPanel();
            temp.setSize( 100,100 );
            components.add( temp );
            container.add(temp);
        }

        myframe.pack();
        myframe.setVisible( true );

        // Accessing components via index.
        components.get( 5 ).setBackground( Color.BLUE );
        components.get( 8 ).setBackground( Color.GREEN );
    }
}
娇纵 2025-01-10 03:41:44

为什么不选择 JTable 而不是这个呢?

Why not go for a JTable instead of this ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文