首页 > 其他分享 >JPanel的GridLayout布局添加网格线

JPanel的GridLayout布局添加网格线

时间:2024-06-02 16:01:45浏览次数:16  
标签:jt getHeight getWidth 网格线 JPanel graphics GridLayout new

        之前从网上找了许多添加网格线的方法,比如添加JLable,设置JLable文本框线实现添加网格线:

//初始化显示界面
JFrame jf = new JFrame("数独游戏");
//设置窗口可视化
jf.setVisible(true);
//设置窗口大小
jf.setSize(900,810);
//将窗口显示在屏幕中央
jf.setLocationRelativeTo(null);
//初始化网格界面
j.setBorder(BorderFactory.createLineBorder(Color.orange));
for (int i = 0; i < 9; i++) {
    // 为每个单元格添加JTextField
    for (int k = 0; k < 9; k++) {
        JTextField jtf = new JTextField();
        jtf.setFont(new Font("宋体",Font.PLAIN,20));
        jtf.setHorizontalAlignment(JTextField.CENTER);
        jt[i][k] = jtf; // 设置初始文本框大小
        jt[i][k].setPreferredSize(new Dimension(1, 1));
        jt[i][k].setText(null);
        jt[i][k].addFocusListener(this);
        h.addLengthLimit(jt[i][k]);
        j.add(jt[i][k]);
    }
}

但这也引起许多问题,比如不能自定义网格线的量,难以实现在9*9的格子中添加3*3的网格线时,所以可以直接重写JPanel中的paint方法,直接绘制网格线:

private JPanel j = new JPanel(new GridLayout(9,9,2,2)){
        // 重写paint方法
        @Override
        public void paint(Graphics graphics) {
            // 必须先调用父类的paint方法
            super.paint(graphics);
            // 用画笔Graphics,在画板JPanel上画一个小人
            Font f = new Font("黑体",Font.BOLD,22);
            graphics.setColor(Color.orange);
            graphics.setFont(f);
            graphics.drawLine(getWidth()/3, 0, getWidth()/3, getHeight());
            graphics.drawLine(getWidth()/3*2, 0, getWidth()/3*2, getHeight());
            graphics.drawLine(0, getHeight()/3, getWidth(), getHeight()/3);
            graphics.drawLine(0, (getHeight()-1)/3*2, getWidth(), (getHeight()-1)/3*2);
        }
    };

实现之后的效果如下图:

标签:jt,getHeight,getWidth,网格线,JPanel,graphics,GridLayout,new
From: https://blog.csdn.net/zhaools/article/details/139392637

相关文章

  • GridLayout 等控件来完成多行按钮操作
     第一步,在布局文件中添加一个GridLayout控件,设置它的行列数和间距等属性,例如:<GridLayoutandroid:id="@+id/grid_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:columnCount="4"andr......
  • fabricjs怎么添加网格线
    html文件:1<canvasid="c"width="600"height="400"></canvas>css文件:1canvas{2border:1pxsolidlightgrey;3} javascript文件1varcanvas=newfabric.Canvas('c',{2selection:false3});4v......
  • QGridLayout使用
    一、概述使用QGridLayout制作一个九宫格,如下图: 二、代码示例#include"GridLayoutExampleWindow.h"GridLayoutExampleWindow::GridLayoutExampleWindow(QWidget*parent):QWidget(parent){this->setWindowTitle("网格布局");this->setFixedSize(......
  • Highcharts饼图的主要属性和网格线属性​
    需求在Highcharts中,需要更改图表里的网格线如何去完成;在Highcharts中,你可以通过设置不同的属性来自定义你的饼图,饼图的属性于其他图表存在差别。分析饼图属性:legend.enabled:控制图例的显示与隐藏。设置为false则隐藏图例,默认为true。legend.layout:设置图例的布局方式。可......
  • echarts坐标轴线、坐标轴刻度线、网格线控制
    xAxis:{name:'',axisTick:{show:true//坐标轴刻度线},axisLine:{//轴线show:false},splitLine:{//网格线show:true},axisLabel:{//坐标轴样式textStyle:{color:'#636363'}}}参考文章echarts坐......
  • echarts坐标轴线、坐标轴刻度线、网格线控制
    xAxis:{name:'',axisTick:{show:true//坐标轴刻度线},axisLine:{//轴线show:false},splitLine:{//网格线show:true},axisLabel:{//坐标轴样式textStyle:{color:'#636363'}}}参考文章echarts坐标轴线、......
  • JAVA SWING之JFrame和JPanel布局
    初学JAVA的时候学习过SWING,每次写程序就直接复制Jframe和Jpanel设置,再调一下大小(不知道有没有人跟我一样),到现在也不清楚它们有什么关系,才回顾学习。Swing虽然是很老的技术了,但也有很多工具是Swing写的例如JetBrains系列,最常用的IDEA。开发一下小工具还是不错的。掌握整体布局后,再......
  • QT常用控件之QTimer,QDialog,QLabel,QLineEdit,QProgressBar,QComboBox,QPushButton,QGridLay
    QT常用控件的组合#ifndefPROGRESSBARWIDGET_H#definePROGRESSBARWIDGET_H#include<QWidget>#include<QTimer>#include<QDialog>#include<QLabel>#include<QLineEdit>#include<QProgressBar>//显示进度条的控件#include<QComboBo......
  • Lesson2 GridLayOut 表格布局
     packagecom.kuang.lesson1;importjava.awt.*;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;publicclassTestGridLayout{publicstaticvoidmain(String[]args){Frameframe=newFrame("TestGridLayout")......
  • Matplotlib 网格线
    Matplotlib网格线我们可以使用pyplot中的grid()方法来设置图表中的网格线。grid()方法语法格式如下:matplotlib.pyplot.grid(b=None,which='major',axis='both',)b:可选,默认为None,可以设置布尔值,true为显示网格线,false为不显示,如果设置**kwargs参数,则值为true。which:......