- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试向 Rectangle
元素添加边框,但由于某种原因它不起作用,它与 JFrame
不兼容吗?我可以将我的整个 JFrame
设置为具有边框,但它找不到带有我的矩形的 setBorder
。这是我的代码:
package trivia;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.border.Border;
@SuppressWarnings("serial")
public class Main extends JFrame{
boolean mainMenu = true;
static Color tan = Color.decode("#F4EBC3");
static Color darkGreen = Color.decode("#668284");
static Color buttonColor = Color.decode("#A2896B");
Rectangle header = new Rectangle(0, 0, 500, 100);
Rectangle body = new Rectangle(0, 100, 500, 400);
Rectangle start = new Rectangle(150, 150, 200, 40);
public Main() {
setTitle("Trivia Game!");
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
@Override
public void paint(Graphics g) {
Dimension d = this.getSize();
Border blackline;
blackline = BorderFactory.createLineBorder(Color.black);
if(mainMenu = true){
g.setColor(darkGreen);
g.fillRect(header.x, header.y, header.width, header.height);
g.setFont(new Font("Courier", Font.BOLD, 24));
g.setColor(Color.BLACK);
drawCenteredString("Trivia Game!", d.width, 125, g);
g.setColor(tan);
g.fillRect(body.x, body.y, body.width, body.height);
g.setColor(buttonColor);
g.fillRect(start.x, start.y, start.width, start.height);
}
}
public void drawCenteredString(String s, int w, int h, Graphics g) {
FontMetrics fm = g.getFontMetrics();
int x = (w - fm.stringWidth(s)) / 2;
int y = (fm.getAscent() + (h- (fm.getAscent() + fm.getDescent())) / 2);
g.drawString(s, x, y);
}
public static void main(String[] args) {
@SuppressWarnings("unused")
Main m = new Main();
}
}
当我在我的 paint
函数中添加它时:
start.setBorder(blackline);
它给我错误:
The method setBorder(Border) is undefined for the type Rectangle
我不确定如何让它识别setBorder
函数,有人可以帮忙吗?非常感谢所有帮助!
最佳答案
Rectangle
没有 setBorder
方法,而是使用 Graphics#setColor(Color )
并使用 Graphics#drawRect(int, int, int, int)
或 Graphics2D#draw(Shape)
super.paint
,您可以防止这样做,并可能导致许多令人讨厌的副作用,这些都不是您真正想要的......paint
,例如 JFrame
;它们不是双缓冲的;框架顶部还有许多其他组件可能会在其上绘制(paint);等。相反,创建一个自定义组件,从类似 JPanel
的东西扩展并覆盖它的 paintComponent
方法(确保您首先调用 super.paintComponent
)...参见 Painting in AWT and Swing , Performing Custom Painting和 2D Graphics了解更多详情
关于java - 如何使用 setBorder 和 JFrame 在 Java 中为矩形添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27457235/
当我使用 GridBagLayout 为 JPanel 中的每个项目设置边框颜色时,组件会折叠。 但是当我仅删除一个组件的边框时,填充会保持预期的状态。 我做错了什么? 边框设置: setBo
我的代码工作正常,因为它做了它应该做的事情,(感谢这个网站的帮助!)但我忘记了一个细节:我的面板周围缺少边框。问题是,我不知道该怎么做,当我为面板分配标签时,它并没有包围所有组件,而是以一种非常奇怪的
我目前正在用 Java 编写一个国际象棋应用程序,但在创建图 block 边框和图像平滑方面遇到一些问题。 编辑:边界问题已解决。请参阅my answer下面; 结果: 正如您所看到的,棋子上有锯齿状
我已经导入了 javax.swing.border.*;有错误的代码是教授提供的。产生的错误是 AccountApplet.java:56: cannot find symbol symbol :
我有一个扩展 JLabel 的自定义类。对于该类的特定实例,我想在左侧的文本中添加一些间距。我需要间距,因为我正在设置此 JLabel 的背景,并且我不希望文本在彩色背景边缘旁边凸起。我摸索了很多并实
我创建了一个包含 4 列的 JTable,其中填充了 txt 文件中的数据。因此,不会对每个单元格中插入的数据类型进行验证。第二列和第三列必须填充“int”。我想为这两列中不包含“int”值的每个单元
有没有办法为 Text1 和 Text2 文本定义我自己的字体和配色方案在 setBorder 方法中。 Java 新手,在 SUN 教程中找不到它。 我的代码 //Create Positions
代码是这样的: JTextField txt = new JTextField(); txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Col
本文整理了Java中com.tc.admin.common.XContainer.setBorder()方法的一些代码示例,展示了XContainer.setBorder()的具体用法。这些代码示例主
我正在尝试向 Rectangle 元素添加边框,但由于某种原因它不起作用,它与 JFrame 不兼容吗?我可以将我的整个 JFrame 设置为具有边框,但它找不到带有我的矩形的 setBorder。这
我一直在为我的一门课开发蛇程序。除了一个小问题外,它运行完美:在 View 中(扩展 JLabel )我将背景设置为 Color.WHITE , opaque至true和 Color.GREEN 的边
我是 iText 的新手,我正在尝试构建一个表格。但由于某种原因 table.getDefaultCell().setBorder(PdfPCell.NO_BORDER) 没有效果,我的表格仍然有
我是一名优秀的程序员,十分优秀!