创建粗体单元格边界线JTable [英] Creating bold cell border lines JTable

查看:95
本文介绍了创建粗体单元格边界线JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建数独游戏,但在创建粗体线条以将其分成3 x 3块时遇到了麻烦.我最近的尝试是使用JLabel将图像强加在表的顶部.问题是标签完全覆盖了JTable.我无所事事地设置了标签和表格的不透明度.以下是一些图片,显示了我的目标:

I'm trying to create a Sudoku game and I am having trouble creating the bold lines to break it into 3 x 3 blocks. My recent attempt was to impose an image on top of the table using JLabel. The problem is the label completely covers the JTable. I goofed around with setting the opacity of the label and the table with no luck. Here are some images to show what I'm aiming for:

当前外观:

目标:

如果您可以建议我创建线的方法,将不胜感激.不需要直接的答案,只需朝正确的方向指出一点即可.

If you could advise me as to a method I can use to create these lines, it would be greatly appreciated. No direct answers needed, just a point in the right direction.

推荐答案

对于任何棋盘游戏,我倾向于使用网格布局(或一组网格布局)中的按钮,例如此棋盘.

For any board game I'd tend to use buttons in a grid layout (or a group of grid layouts) like in this mine sweeper game or this chess board.

尽管如此,对于此GUI中的边框,我将使用3 x 3组的九个网格布局,每个布局都有一个LineBorder.默认情况下,边框会围绕所显示面板的所有四个侧面,但是与边框相交的地方将是两倍宽度,从而接近于重新创建第二个图像.

For the borders in this GUI though, I'd use a 3 x 3 group of nine grid layouts, each of which has a LineBorder. By default the border would go around all four sides of the panel it is displayed in, but where they meet the border would be double width, thereby coming close to recreating the second image.

E.G.

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

public class Soduko {

    private JComponent ui = null;

    Soduko() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridLayout(3,3));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        ArrayList<Integer> values = new ArrayList<>();
        for (int ii=0; ii<34; ii++) {
            values.add(0);
        }
        Random r = new Random();
        for (int ii=34; ii<81; ii++) {
            values.add(r.nextInt(9)+1);
        }
        Collections.shuffle(values);
        int count=0;
        for (int ii=0; ii<9; ii++) {
            JPanel p = new JPanel(new GridLayout(3, 3));
            p.setBorder(new LineBorder(Color.BLACK, 2));
            ui.add(p);
            for (int jj=0; jj<9; jj++) {
                int v = values.get(count++).intValue();
                String s = v>0 ? "" + v : "";
                p.add(new JButton(s));
            }
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                Soduko o = new Soduko();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

这篇关于创建粗体单元格边界线JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆