java - 在 JFrame 中的特定点绘制一条线

标签 java swing layout jframe jtextfield

这是我的代码,它显示了一个 9x9 网格:

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

public class SudokuGrid extends JFrame {

private static final int ROWS = 9;
private static final int COLUMNS = 9;
int fontSize = 30;

public static void main(String[] args) {

  SudokuGrid makeSudokuGrid = new SudokuGrid();

} // end of main

// constructor SudokuGrid
public SudokuGrid() {

  JTextField[][] inputBoxes = new JTextField[ROWS][COLUMNS];
  Font font = new Font("Helvetica", Font.BOLD, fontSize);

  setLayout(new GridLayout(ROWS, COLUMNS));
  // set frame size
  setSize(400, 400);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // outer loop to create the rows
  for (int rows = 0 ; rows < ROWS ; rows++) {

    // inner loop to create the columns
    for (int columns = 0 ; columns < COLUMNS ; columns++) {

      // make text fields empty
      inputBoxes[rows][columns] = new JTextField("");
      // add text fields to the frame
      add(inputBoxes[rows][columns]);
      // center text in each text box
      inputBoxes[rows][columns].setHorizontalAlignment(JTextField.CENTER);
      // apply font to each text box
      inputBoxes[rows][columns].setFont(font);

    } // end of columns loop

  } // end of rows loop

  // make frame visible
  getContentPane().setBackground(Color.RED);
  setVisible(true);

} // end of constructor SudokuGrid

} // end of class SudokuGrid

我想做的是每隔三行画一条线。所以每隔三个文本框,应该有一条横跨所有列的粗线。希望这是有道理的。

非常感谢任何帮助。谢谢!

最佳答案

简单的答案是,GridLayout 不会做您想要的,它不够灵活,相反...

你可以...

更改布局管理器并使用 JSeparator

JSeparator

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SudokuGrid {

    public static void main(String[] args) {
        new SudokuGrid();
    }

    public SudokuGrid() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        private static final int ROWS = 9;
        private static final int COLUMNS = 9;
        int fontSize = 30;

        public TestPane() {
            JTextField[][] inputBoxes = new JTextField[ROWS][COLUMNS];
            Font font = new Font("Helvetica", Font.BOLD, fontSize);

            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.gridy = 0;

            GridBagConstraints split = new GridBagConstraints();
            split.fill = GridBagConstraints.BOTH;
            split.weightx = 1;
            split.gridx = 0;
            split.gridwidth = GridBagConstraints.REMAINDER;

            // outer loop to create the rows
            for (int rows = 0; rows < ROWS; rows++) {

                gbc.gridy++;
                // inner loop to create the columns
                for (int columns = 0; columns < COLUMNS; columns++) {

                    gbc.gridx = columns;

                    // make text fields empty
                    inputBoxes[rows][columns] = new JTextField(1);
                    // add text fields to the frame
                    add(inputBoxes[rows][columns], gbc);
                    // center text in each text box
                    inputBoxes[rows][columns].setHorizontalAlignment(JTextField.CENTER);
                    // apply font to each text box
                    inputBoxes[rows][columns].setFont(font);

                } // end of columns loop

                if ((rows + 1) % 3 == 0) {
                    System.out.println("Split");
                    split.gridy = gbc.gridy + 1;
                    gbc.gridy += 2;
                    JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
                    add(sep, split);
                }

            } // end of rows loop
        }

    }

}

你可以...

通过使用自定义绘画制作您自己的“拆分”组件

Custom Split

public static class HorizontalSplit extends JPanel {

    public HorizontalSplit() {
        setOpaque(false);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(0, 3);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); 
        Graphics2D g2d = (Graphics2D) g.create();
        int y = (getHeight() - 3) / 2;
        BasicStroke stroke = new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        g2d.setStroke(stroke);
        g2d.drawLine(0, y, getWidth(), y);
        g2d.dispose();
    }

}

这将简单地替换 JSeparator...

if ((rows + 1) % 3 == 0) {
    System.out.println("Split");
    split.gridy = gbc.gridy + 1;
    gbc.gridy += 2;
    JPanel sep = new HorizontalSplit();
    add(sep, split);
}

你可以...

使用复合布局和MatteLayout...

Compound Layout

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.MatteBorder;

public class SudokuGrid {

    public static void main(String[] args) {
        new SudokuGrid();
    }

    public SudokuGrid() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        private static final int ROWS = 9;
        private static final int COLUMNS = 9;
        int fontSize = 30;

        public TestPane() {
            JTextField[][] inputBoxes = new JTextField[ROWS][COLUMNS];
            Font font = new Font("Helvetica", Font.BOLD, fontSize);

            setLayout(new GridBagLayout());

            GridBagConstraints groupContraint = new GridBagConstraints();
            groupContraint.fill = GridBagConstraints.BOTH;
            groupContraint.weightx = 1;
            groupContraint.weighty = 1;
            groupContraint.gridwidth = GridBagConstraints.REMAINDER;

            JPanel group = new JPanel(new GridLayout(3, COLUMNS));
            group.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));

            // outer loop to create the rows
            for (int rows = 0; rows < ROWS; rows++) {

                // inner loop to create the columns
                for (int columns = 0; columns < COLUMNS; columns++) {

                    // make text fields empty
                    inputBoxes[rows][columns] = new JTextField(1);
                    // add text fields to the frame
                    group.add(inputBoxes[rows][columns]);
                    // center text in each text box
                    inputBoxes[rows][columns].setHorizontalAlignment(JTextField.CENTER);
                    // apply font to each text box
                    inputBoxes[rows][columns].setFont(font);

                } // end of columns loop

                if ((rows + 1) % 3 == 0) {
                    add(group, groupContraint);
                    group = new JPanel(new GridLayout(3, COLUMNS));
                    group.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));
                }

            } // end of rows loop
        }

    }

}

关于java - 在 JFrame 中的特定点绘制一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33717291/

相关文章:

java - 网格世界为列数分配变量

java - Java 8 负子数组

java - 如何从 Tyrus websocket 客户端获取本地 IP 地址

swing - 如何让 jfreechart ChartPanel 自动放大?

java - 比较存储在字符串中的时间

java - 使用 selenium WebDriver 自动化 Flipkart 应用程序

java - 在java中使用setbounds

html - 图片/CSS 不会在 OSCommerce 上加载

css - Bootstrap NavBar 不可折叠的输入字段

android - 以编程方式设置 android textView drawableEnd