gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:56:05 25 4
gpt4 key购买 nike

这是我的代码,它显示了一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com