我一直试图调试这几个小时.该程序应该是一个图形坐标的图形,但我不能得到任何东西甚至不是随机线,但如果我把一个打印声明放在那里它的工作原理.这是paintComponent方法的一个问题.当我在g.drawLine之前打印语句然后打印,但即使我放置一个带坐标(1,3),(2,4)的随机行,它也不会绘制任何行.
import java.awt.*;
import java.util.*;
import javax.swing.*;
public abstract class XYGrapher
{
abstract public Coordinate xyStart();
abstract public double xRange();
abstract public double yRange();
abstract public Coordinate getPoint(int pointNum);
public class Paint extends JPanel
{
public void paintGraph(Graphics g, int xPixel1, int yPixel1, int xPixel2, int yPixel2)
{
super.paintComponent(g);
g.setColor(Color.black);
g.drawLine(xPixel1, yPixel1, xPixel2, yPixel2);
}
public void paintXAxis(Graphics g, int xPixel, int pixelsWide, int pixelsHigh)
{
super.paintComponent(g);
g.setColor(Color.green);
g.drawLine(xPixel, 0, xPixel, pixelsHigh);
}
public void paintYAxis(Graphics g, int yPixel, int …
Run Code Online (Sandbox Code Playgroud) 我有两个不同类的两个组合框.我的要求是,如果我在一个类中更改数据,那么数据应该显示在其他类中.有没有听众或其他方式?你能帮我吗?
在catch块中,我想向textArea显示一条错误消息,以显示发生的错误.
我有一个jCombobox,我想移动用于显示左侧选项的按钮.知道如何做到这一点?
---------------------------
|\/ |Combobox |
--------------------------
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个项目来创建一个多玩家垄断游戏我有一个很好的想法如何实现它,但直到现在我几乎没有GUI的经验.我想要做的是我希望电路板看起来非常逼真,所以我主要担心的是SWING能够为我提供:
a)可点击的方块(支持右键菜单)和背景图像
b)移动件("从一个方格浮动到另一个方块"动画会很好),它会停留在正方形上并支持右键菜单
c)使用单选按钮弹出菜单.
基本上我的主要关注点是浮动动画和滚动骰子动画(好的,最后一个不是那么重要).
如果我不能用SWING做到这一点,我可以用QT吗?
我正在使用swing创建一个战舰类型的游戏.我在一个面板中绘制图像并显示正常,但是当我在不同的面板中绘制更多图像时,它会删除第一个面板中的大部分(如果不是全部)图像并保持第二个面板中的图像完好无损.如何在面板中绘制后保持图像不会消失.
我一直在网上寻找答案大约一个星期左右,并且没有提出任何问题......
public class GameBoard extends JPanel{
Graphics g0;
public void paintComponent(Graphics g1) {
g0 = this.getGraphics();
// fill with the color you want
int wide = 275;
int tall = 275;
// go into Graphics2D for all the fine art, more options
// optional, here I just get variable Stroke sizes
Graphics2D g2 = (Graphics2D) g1;
g2.setColor(Color.black);
g2.setStroke(new BasicStroke(1));
Graphics2D g3 = (Graphics2D) g0;
g3.setColor(Color.black);
g3.setStroke(new BasicStroke(1));
// the verticals
for (int i = 0; i < 12*25; i+=25) …
Run Code Online (Sandbox Code Playgroud) 我做了一个摆动应用程序,我需要根据需要塑造窗户.
所以为了这个目的,我制作了一个相当于我想要的形状的多边形,然后调用
setShape(myPloygon)
方法,通过该方法创建一个自定义形状的框架.
final Window w = new TestFrame();
w.setVisible(true);
Polygon polygon=new Polygon();
polygon.addPoint(10,0);
polygon.addPoint(0,10);
polygon.addPoint(0,573);
polygon.addPoint(10,583);
polygon.addPoint(570,583);
polygon.addPoint(580,573);
polygon.addPoint(580,545);
polygon.addPoint(720,545);
polygon.addPoint(730,535);
polygon.addPoint(730,90);
polygon.addPoint(720,80);
polygon.addPoint(580,80);
polygon.addPoint(580,10);
polygon.addPoint(570,0);
w.setShape(polygon); //method in jdk7
Run Code Online (Sandbox Code Playgroud)
如果使用JDK 6那么
com.sun.awt.AWTUtilities.setWindowShape(w, polygon);
Run Code Online (Sandbox Code Playgroud)
它在Windows上运行完美,但是当我在Mac上尝试它时,窗口形状功能不会创建任何形状,它只显示完整的窗口
所以我该怎么做?
我是 Maven 新手。我需要包括
private javafx.embed.swing.JFXPanel jFXPanel1;
Run Code Online (Sandbox Code Playgroud)
使用 Maven + Jigsaw 模块的新项目。
我的包有以下问题javafx.embed.swing
。
我以为添加这个依赖项会起作用,但事实并非如此。
private javafx.embed.swing.JFXPanel jFXPanel1;
Run Code Online (Sandbox Code Playgroud)
在全新安装时,我看到以下内容
(包 javafx.embed.swing 在未命名模块中声明,但模块 com.my.module 不读取它)
我不明白这一点。我看到那个JAR,它只有一个清单文件,没有任何.class
文件,这让我更加困惑。
如果我查看 m2 文件夹,我会看到一个不同的 JAR,具有相同的前缀,但带有win
后缀,并且在那里我看到了该包。
我认为这就是分类器
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-swing -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>19</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我想,但我不知道,这个 jar 是否被 Maven 选择,但似乎没有,因为我在 IntelliK 库中没有看到它。
我对 JavaFX 没有任何问题,一切都是模块化的并且正在运行。我只有这个罐子有问题。
- - - - - - - - - - - - - - -更新 - - - - - - - - - - ----------------
这里的罪魁祸首是 IntellijIdea,我确实单击了 …
我正在尝试使用 Java 的 Swing 库构建一个带有 GUI 界面的国际象棋游戏。当我开始游戏时,会打开一个带有棋盘的窗口。
我所坚持的是,每次我都想等待当前玩家采取行动,而不是无限地运行 while 循环,因为这不是很有效。
这是代码和之后的解释:
private void startGame()
{
while (!board.terminalBoard())
{
Move movePlayed = null;
// whilist making the move returns false, meaning we cant do it...
while (canMakeMove(movePlayed))
{
// TODO: Implement the getting move here
}
// If move is okay, make it
board.movePiece(movePlayed.getStart(), movePlayed.getEnd());
// Repaint the chessboard
chessboard.loadImages(board);
chessboard.repaintBoard();
currColor ^= Piece.BLACK;
}
}
Run Code Online (Sandbox Code Playgroud)
在内部 while 循环中,当用户所下的棋步无效时,它会再次迭代并等待下一个棋步。我不明白的是我如何等待玩家采取行动。
就我而言,玩家可以通过在棋盘上单击两次(源方格和目标方格)来进行移动。然后我的函数检查它是否有效。
如何等待用户在 Java Swing 窗口上单击两次?
编辑代码:问题是,如果我像你说的那样删除 thread.sleep,应用程序不会响应我的点击。
private void startGame()
{
while …
Run Code Online (Sandbox Code Playgroud) 我是否应该假设以下程序是错误的,因为 x 和 y 行(在 main 方法的最后)没有在 EDT 上执行?
public class Temp
{
private static JButton button;
private static JTextField text;
private static void showGUI()
{
JFrame frame = new JFrame();
JPanel contentPane = new JPanel();
button = new JButton( "Push Me" );
text = new JTextField( "I am the walrus" );
contentPane.add( button );
contentPane.add( text );
frame.setContentPane( contentPane );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
throws InterruptedException, InvocationTargetException
{
SwingUtilities.invokeAndWait( () …
Run Code Online (Sandbox Code Playgroud)