这实际上是我的第一个完整程序,最终将用于为我正在制作的机器人编写代码。一切正常,除了当我运行它时,我必须拖动打开窗口才能看到里面的内容。
有关如何修复它的任何建议。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();
}
}
这是frame2的文件,它确实有效;要测试只需运行这个。
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 弄清楚了一切。如果代码中的任何内容或我所说的内容不清楚或没有意义,请原谅我并问。