- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在绘制 jcomponent 时遇到问题
//应在其中绘制矩形的类
public class Board extends JComponent
{
private Case[][] cases= new Case[10][10];
public Plateau() {
super();
this.setLayout(new GridLayout(10,10));
this.setSize(getPreferredSize());
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if ((i + j) % 2 == 0) {
cases[i][j] = new WhiteCase(j * Case.LONGUEUR, i * Case.LONGUEUR, Case.LONGUEUR, Case.LONGUEUR);
} else {
cases[i][j] = new BlackCase(j * Case.LONGUEUR, i * Case.LONGUEUR, Case.LONGUEUR, Case.LONGUEUR);
}
add(cases[i][j]);
}
}
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
}
//矩形基类
public abstract class Case extends JComponent {
protected static final int LONGUEUR=60;
protected int x,y,width,height;
protected abstract void paintComponent(Graphics g);
public Dimension getPreferredSize() { return new Dimension(LONGUEUR, LONGUEUR);
}
}
///黑壳
public class BlackCase extends Case
{
private Piece piece;
private static final long serialVersionUID = 1L;
public CaseNoire(int x, int y,int width,int height)
{
this.x=x;
this.y=y;
this.width = width;
this.height= height;
}
public Dimension getPreferredSize() {
return new Dimension(LONGUEUR, LONGUEUR);
}
@Override
protected void paintComponent(Graphics g)
{
g.setColor(Color.darkGray);
g.fillRect(x, y,width,height);
}
}
public class CaseWhite extends Case {
/**
*
*/
private static final long serialVersionUID = 1L;
public CaseBlanche(int x, int y,int width,int height)
{
this.x=x;
this.y=y;
this.width = width;
this.height= height;
}
@Override
public void paintComponent(Graphics g)
{
g.setColor(Color.white);
g.fillRect(x, y,width,height);
g.setColor(Color.BLACK);
g.drawString("X= "+x , 10, 10);
}
public Dimension getPreferredSize() {
return new Dimension(LONGUEUR, LONGUEUR);
}
}
//Main class
public class CheckersGame extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args )
{
CheckersGame checkers= new CheckersGame();
}
public CheckersGame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Jeu de Dames");
JPanel panelPrincipalDame = new JPanel(new GridBagLayout());
Board board = new Board();
GridBagConstraints c = new GridBagConstraints();
c.fill= GridBagConstraints.NONE;
c.gridx =0;
c.gridy = 0 ;
c.gridheight= 2;
c.gridwidth= 2;
panelPrincipalDame.add(plateau,c);
setSize(800, 700);
setContentPane(panelPrincipalDame);
![//setVisible(true);][1]
setResizable(false);
}
}
这段代码的结果是(注意X+0等..仅用于调试目的)
但我想要的是这个
请问为什么我只得到一个矩形?
最佳答案
非常感谢我在上一个问题中给出的不创建“CaseNoire”和“CaseBlanch”类的建议:paintComponent does not paint correctly从两周前开始。这些类(class)是不需要的。如果您想让用户灵活地选择方 block 的颜色,会发生什么?你的游戏逻辑永远不应该基于类名或类似的东西。因此,摆脱这些类并使用内置的 Swing 功能来为组件的背景着色。
我认为问题是因为您在 Case 类中创建了变量“x,y,宽度,高度”。我相信这些变量已经在 Component 类中定义,用于表示组件的大小/位置。
摆脱变量,您不需要管理每个组件的大小/位置,因为 GridLayout 会为您做这件事。
再次查看我给您的示例代码,它展示了如何创建“ChessBoard”。
关于java - 添加到 JComponent 的绘制 JComponent 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736696/
这个问题已经有答案了: Mirroring an Object in JFrame with paintComponent (1 个回答) 已关闭 6 年前。 我已经知道 JComponent 无法添
我在绘制 jcomponent 时遇到问题 //应在其中绘制矩形的类 public class Board extends JComponent { private Case[][] case
在 Swing 中,鼠标事件会自动定位到具有鼠标监听器的最深组件。当有一个组件具有鼠标监听器并添加到其他组件之上时,如何将它们再次定位到更深的组件? 应该在不将组件设置为不可见或删除其鼠标监听器的情况
最近我在使用 Swing 时遇到了一些问题。我正在尝试快速制作下图所示的内容,以说明算法的数据结构。 (来源:ius.edu) 我在接下来的类(class)中尝试做的就是画出一些带有数字的矩形。并翻译
我已经阅读了 oracle 提供的 Java trail,他们说对象属性设置为与操作属性相匹配。我想知道这是否是一组有限的共享属性,或者是否将 Action 强制转换为与您将操作设置为的对象相同的类。
我有一个 JCombobox,我在运行时添加一些项目。其中一些非常长,我的 Jcombobox 也长得很长。它位于带有 BoxLayout (PAGE_AXIS) 的 JPanel 容器中。我不知道如
我正在处理一个项目,该项目要求我能够在同一容器内的组件顶部绘制半透明的 JPanel。使用 setComponentZOrder() 方法对于重叠(即下方的组件未完全覆盖)效果很好,但当下方的组件完全
目前,我必须将 JComponent 包含在具有垂直框布局的 JPanel 中。这样,我可以将第一个组件居中,如下所示,并将底部组件(相当长)放在下面。但是,由于底部组件非常长,我想仅为该特定组件添加
我有一个 JComponent 子类,我用它在屏幕上绘制形状。在构造函数中,我尝试将 ballX 和 ballY 设置为 X 和 Y 大小值的一半JComponent,我想我做错了。我现在已经查了很多
我想完成一些与带有选择器线的矩形图像非常相似的事情。 基本上,我有一个矩形,我想在它周围有一条选择器线。为此,我想创建一个额外的 JComponent。 目前我只能绘制矩形。如何在 Rectangle
到目前为止,我一直在使用 VB 来开发应用程序。现在我必须使用Java来开发前端。我对组件很困惑。需要帮助。书籍引用或网站引用也可以完成这项工作。 基本上,我将使用菜单栏、工具栏、带有 JEditor
我有一个自定义 JComponent,它在调用 paint 时绘制一些内容。但是,在 Border 布局中,它的 minimumSize 并未得到尊重。我已经包含了这个 @Override publi
我是一名初学者程序员,我正在尝试让这个程序运行。尽管 JComponent 的新 Java 屏幕上没有出现任何图像,但一切都可以正确编译。该程序要做的几乎就是获取一个输入值并将其分配给条形图的大小值。
我有以下代码: package gui; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; imp
如何获取JComponent名称(JComboBox,JCheckBox,JTextComponent...等) 我如何获取 fieldName 并将此名称放入 HashMap,例如:
我在面板上有一个 JTable 和一个 JLabel。我将其设置为背景图像,但无法摆脱表格和标签中的灰色背景。 我该怎么做? 最佳答案 尝试在 JTable 和 JLabel 上执行 .setOpaq
我正在尝试将组件添加到我的 JPanel,但是它们的大小不正确且位置不正确。这是我的组件代码。 button = new JButton(); button.setSize(100, 100); bu
好的。我正在编写我的 Ball 类,但球没有显示。我尝试将其他组件添加到我的容器中并且它们被显示,所以我认为可以安全地假设问题出在我的球上。类代码: import java.awt.Color; im
作为项目的一部分,我有一个自定义 JComponent,它实现了一部分用户界面。这是一个相当奇怪的控制,涉及围绕一组 x-y 点移动,但这并不重要,因为它已经实现并且工作正常。 该组件有一个正方形的可
嗨,我是java初学者,这里我有我的程序,但是,当我尝试调整面板大小时,我的文本就消失了?此外,如何在文本下绘制一条粗绿线,即使在调整大小时它也会保留在文本下方,我很无能? import javax.
我是一名优秀的程序员,十分优秀!