gpt4 book ai didi

java - 在拖动打开的窗口之前无法看到 JFrame 的内容

转载 作者:行者123 更新时间:2023-12-01 23:52:40 25 4
gpt4 key购买 nike

这实际上是我的第一个完整程序,最终将用于为我正在制作的机器人编写代码。一切工作正常,除了当我运行它时,我必须拖动打开窗 Eloquent 能看到其中的内容。

有关如何修复它的任何建议。Frame2 - “单击以构建文件”有效。框架 - 带有按钮网格的框架,是我必须拖动打开才能看到的框架。

这是框架的设置文件(不起作用的)

package Grid;

//march 13 to April 11
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;


@SuppressWarnings("serial")
public class ButtonGrid extends JFrame {
public static int clicked[][] = new int[20][40];
static JButton button[] = new JButton[800];
static int x;
static int count = 1;
public static int clickedfinal[][];
int value;

public ButtonGrid() {

JFrame frame = new JFrame();
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

GridLayout grid = new GridLayout(20, 40, 10, 8);
frame.setLayout(grid);

for (int c = 0; c < 20; c++) {
for (int d = 0; d < 40; d++) {
clicked[c][d] = 0;
}
}
for (x = 0; x < 800; x++) {
button[x] = new JButton();
button[x].setActionCommand(Integer.toString(x));
frame.add(button[x]);
button[x].setBackground(Color.LIGHT_GRAY);
button[x].setOpaque(true);

thehandler handler = new thehandler();
button[x].addActionListener(handler);
}
}

public class thehandler implements ActionListener {
public void actionPerformed(ActionEvent e) {

for (;;) {
value = Integer.parseInt(e.getActionCommand());
button[value].setBackground(Color.BLACK);

int r = value % 40;
int m = ((value - (value % 40)) / 40);
// learn how to round up

clicked[m][r] = 1;

break;

}

}

}

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

}

}

这是第 2 帧的文件,它可以正常工作;要进行测试,只需运行这个即可。

package Grid;

import javax.swing.JButton;
import javax.swing.JFrame;

import Grid.ButtonGrid;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

@SuppressWarnings("serial")
public class Arduinowriter extends JFrame {

static int t = 1;
static int buttonpressed;
static int total;
static String Code;
static String oldcode;

public Arduinowriter() {
JFrame frame2 = new JFrame("Build File");
JButton button = new JButton("Click to Build File");
frame2.setSize(400, 400);
frame2.add(button);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);

button.setActionCommand(Integer.toString(1));
thehandler handler = new thehandler();
button.addActionListener(handler);

}

public class thehandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
buttonpressed = Integer.parseInt(e.getActionCommand());

String newLine = System.getProperty("line.separator");
String Motor2_half = "digitalWrite(Motor2, HIGH);" + newLine
+ "delay(500)" + newLine + "digitalWrite(Motor2, LOW)";

String Motor2_one = "digitalWrite(Motor2, HIGH);" + newLine
+ "delay(1000);" + newLine + "digitalWrite(Motor2, LOW);";
String Servo1_dispense = "digitalWrite(Servo1, HIGH);" + newLine
+ "delay(1000)" + newLine + "digitalWrite(Servo1, LOW);";

String Motor2_back = "digitalWrite(Motor2back, High" + newLine
+ "delay(6000)" + newLine + "digitalWrite(Motor2back, Low)";

String Motor1_forward = "digitalWrite(Motor1, HIGH);" + newLine
+ "delay(1000);" + newLine + "digitalWrite(Motor1, LOW);";

String dispenseCycle = (Motor2_one + newLine + Servo1_dispense
+ " //Domino" + newLine);

String skipCycle = (Motor2_one + newLine);

String backCycle = (Motor1_forward + newLine + Motor2_back + newLine);

while (buttonpressed == 1) {

for (int x = 0; x < 20; x++) {

Code = oldcode + "//Line " + (x + 1);
oldcode = Code;
yloop: for (int y = 0; y < 40; y++) {

boolean empty = true;
for (int check = y; check < 39; check++) {

if (ButtonGrid.clicked[x][check] == 1) {
empty = false;
System.out.println(x + " not empty");
}
}

if (ButtonGrid.clicked[x][y] == 1 && y == 0) {
Code = oldcode + newLine + Servo1_dispense
+ " //Domino" + newLine;
} else if (ButtonGrid.clicked[x][y] == 1) {
Code = oldcode + newLine + dispenseCycle + newLine;
}

else if (ButtonGrid.clicked[x][y] == 0
&& empty == false) {
Code = oldcode + newLine + skipCycle + newLine;
} else {
Code = oldcode + newLine + backCycle + newLine;
oldcode = Code;
break yloop;
}
oldcode = Code;

}
}

try {

BufferedWriter out = new BufferedWriter(new FileWriter(
"C:\\ArduinoCode.txt"));
out.write(Code);
out.close();
} catch (IOException g) {
System.out.println("Exception ");

}

return;
}

}

}

// button

public static void main(String[] args) {

new ButtonGrid();
new Arduinowriter();

}
}

注意:我是一个非常初学者。这段代码花了很多个小时来编写,我使用谷歌和 YouTube 弄清楚了一切。如果代码中的任何内容或我所说的内容不清楚或没有意义,请原谅我并询问。

最佳答案

在设置布局管理器以及影响其“视觉”状态的所有其他内容之后,调用frame.setVisible(true)

关于java - 在拖动打开的窗口之前无法看到 JFrame 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16124105/

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