我试图用Java创建GUI彩票程序。彩票计划的实际代码运作良好,我也创建了GUI。问题是我不能让程序在" Go"按下按钮,信息不会显示在相应的GUI文本字段中。
以下是基本的Lotto代码,其中包含完整的GUI:
package slots;
import java.util.*;
public class Slots {
static int displaynum[] = new int[5];
static int compnum[] = new int[5];
static int counter=0;
static int matchFound=0;
static int nummatch[] = new int[6];
public static void main(String[] args) {
for(int z=0; z<5;z++) {
Random i = new Random();
//Change 10 to 60
compnum[z]=i.nextInt((10 - 1) + 1) + 1;
}
System.out.println(" " + compnum[0] +" " + compnum[1] +" " + compnum[2] +" " + compnum[3] +" " + compnum[4]);
while(nummatch[5]==0) {
nextGo();
matchFound=0;
if(compnum[0]==displaynum[0]) {
matchFound++;
}
if (compnum[1]==displaynum[1]){
matchFound++;
}
if (compnum[2]==displaynum[2]){
matchFound++;
}
if (compnum[3]==displaynum[3]){
matchFound++;
}
if (compnum[4]==displaynum[4]){
matchFound++;
}
nummatch[matchFound]++;
}
System.out.println(" Zero Matches=" + nummatch[0] + " One Matches=" + nummatch[1] +" Two Matches=" + nummatch[2]+ " Three Matches=" + nummatch[3]+ " Four Matches=" + nummatch[4]+ " Total Draws=" + counter);
}
static void nextGo() {
for(int x=0;x<5;x++) {
Random i = new Random();
//Change 10 to 60
displaynum[x]=i.nextInt((10 - 1) + 1) + 1;
}
counter++;
System.out.println(" " + displaynum[0] +" " + displaynum[1] +" " + displaynum[2] +" " + displaynum[3] +" " + displaynum[4]);
}
}
因此上面的代码显示了每个乐透绘图并正确显示了不同的匹配。但是我无法在GUI文本字段中显示相同的输出。
这是我尝试将上述代码和GUI结合起来:
package slots;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.*;
public class LottoGUI extends Frame implements ActionListener {
private static TextField tfOne;
private static TextField tfTwo;
private static TextField tfThree;
private static TextField tfFour;
private static TextField tfFive;
private Label lblmnZero;
private static TextField mnZero;
private Label lblmnOne;
private static TextField mnOne;
private Label lblmnTwo;
private static TextField mnTwo;
private Label lblmnThree;
private static TextField mnThree;
private Label lblmnFour;
private static TextField mnFour;
private Label lbltfDraws;
private static TextField tfDraws;
private Button btnGo;
private Button btnClose;
public LottoGUI() {
setLayout(new FlowLayout());
tfOne = new TextField("", 2);
tfOne.setEditable(false);
add(tfOne);
tfTwo = new TextField("", 2);
tfTwo.setEditable(false);
add(tfTwo);
tfThree = new TextField("", 2);
tfThree.setEditable(false);
add(tfThree);
tfFour = new TextField("", 2);
tfFour.setEditable(false);
add(tfFour);
tfFive = new TextField("", 2);
tfFive.setEditable(false);
add(tfFive);
lblmnZero = new Label("Zero Matches:");
add(lblmnZero);
mnZero = new TextField("");
mnZero.setEditable(false);
add(mnZero);
lblmnOne = new Label("One Matches:");
add(lblmnOne);
mnOne = new TextField("");
mnOne.setEditable(false);
add(mnOne);
lblmnTwo = new Label("Two Matches:");
add(lblmnTwo);
mnTwo = new TextField("");
mnTwo.setEditable(false);
add(mnTwo);
lblmnThree = new Label("Three Matches:");
add(lblmnThree);
mnThree = new TextField("");
mnThree.setEditable(false);
add(mnThree);
lblmnFour = new Label("Four Matches:");
add(lblmnFour);
mnFour = new TextField("");
mnFour.setEditable(false);
add(mnFour);
lbltfDraws = new Label("Total Draws:");
add(lbltfDraws);
tfDraws = new TextField("");
tfDraws.setEditable(false);
add(tfDraws);
btnGo = new Button("GO");
add(btnGo);
btnClose = new Button("CLOSE");
add(btnClose);
btnGo.addActionListener(this);
btnClose.addActionListener(this);
setTitle("Lotto");
setSize(1000, 100);
setVisible(true);
}
public static void main(String[] args) {
new LottoGUI();
}
public static class Slots {
static int displaynum[] = new int[5];
static int compnum[] = new int[5];
static int counter=0;
static int matchFound=0;
static int nummatch[] = new int[6];
public static void main(String[] args) {
for(int z=0; z<5;z++) {
Random i = new Random();
//Change 10 to 60
compnum[z]=i.nextInt((10 - 1) + 1) + 1;
}
//System.out.println(" " + compnum[0] +" " + compnum[1] +" " + compnum[2] +" " + compnum[3] +" " + compnum[4]);
Object[] obcm0 = {compnum[0]};
mnZero.setText(Arrays.toString(obcm0));
Object[] obcm1 = {compnum[1]};
mnOne.setText(Arrays.toString(obcm1));
Object[] obcm2 = {compnum[2]};
mnTwo.setText(Arrays.toString(obcm2));
Object[] obcm3 = {compnum[3]};
mnThree.setText(Arrays.toString(obcm3));
Object[] obcm4 = {compnum[4]};
mnFour.setText(Arrays.toString(obcm4));
while(nummatch[5]==0) {
nextGo();
matchFound=0;
if(compnum[0]==displaynum[0]) {
matchFound++;
}
if (compnum[1]==displaynum[1]){
matchFound++;
}
if (compnum[2]==displaynum[2]){
matchFound++;
}
if (compnum[3]==displaynum[3]){
matchFound++;
}
if (compnum[4]==displaynum[4]){
matchFound++;
}
nummatch[matchFound]++;
}
//System.out.println(" Zero Matches=" + nummatch[0] + " One Matches=" + nummatch[1] +" Two Matches=" + nummatch[2]+ " Three Matches=" + nummatch[3]+ " Four Matches=" + nummatch[4]+ " Total Draws=" + counter);
Object[] obnm0 = {nummatch[0]};
mnZero.setText(Arrays.toString(obnm0));
Object[] obnm1 = {nummatch[1]};
mnOne.setText(Arrays.toString(obnm1));
Object[] obnm2 = {nummatch[2]};
mnTwo.setText(Arrays.toString(obnm2));
Object[] obnm3 = {nummatch[3]};
mnThree.setText(Arrays.toString(obnm3));
Object[] obnm4 = {nummatch[4]};
mnFour.setText(Arrays.toString(obnm4));
Object[] obcn = {counter};
tfDraws.setText(Arrays.toString(obcn));
}
static void nextGo() {
for(int x=0;x<5;x++) {
Random i = new Random();
//Change 10 to 60
displaynum[x]=i.nextInt((10 - 1) + 1) + 1;
}
counter++;
//System.out.println(" " + displaynum[0] +" " + displaynum[1] +" " + displaynum[2] +" " + displaynum[3] +" " + displaynum[4]);
Object[] obdn0 = {displaynum[0]};
tfOne.setText(Arrays.toString(obdn0));
Object[] obdn1 = {displaynum[1]};
tfTwo.setText(Arrays.toString(obdn1));
Object[] obdn2 = {displaynum[2]};
tfThree.setText(Arrays.toString(obdn2));
Object[] obdn3 = {displaynum[3]};
tfFour.setText(Arrays.toString(obdn3));
Object[] obdn4 = {displaynum[4]};
tfFive.setText(Arrays.toString(obdn4));
}
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if(str.equals("GO"))
new Slots();
else if (str.equals("CLOSE"))
System.exit(0);
}
}
点击&#34; GO&#34;没有任何反应。按钮。也许我将打印命令放在代码的错误部分,或者我没有做错其他的事情?我真的很感激任何指导。
这也是我第一次发布到这个网站,所以如果我搞砸了什么,请告诉我。我一直在寻求帮助,找不到与我的问题有关的内容。谢谢!
答案 0 :(得分:2)
您希望尝试将静态线性控制台程序与事件驱动的GUI合并,但这种方法永远不会有效。首先,你的while循环阻塞你的Swing事件线程,这可能会使GUI无法运行。您需要重新编写程序逻辑,以便它以事件驱动的方式工作。创建真实对象,并使几乎所有字段和方法都是非静态的。
例如,除了常量之外,我创建了大部分字段,非静态字段。我将我的大多数字段设为私有,只制作需要由其他类公开调用的方法。我将引用传递给需要调用原始对象方法的对象。例如,这是一个小程序,可以解决您尝试做的事情(但不完全是作弊行为),并可以提供我所得到的知识:
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.*;
@SuppressWarnings("serial")
public class SimpleGui extends JPanel {
private static final int TEXT_FIELD_COLUMNS = 3;
private JTextField[] textFields = new JTextField[SimpleModel.VALUES_COUNT];
private JButton goButton = new JButton(new GoAction("Go", KeyEvent.VK_G));
private JButton exitButton = new JButton(new ExitAction("Exit",
KeyEvent.VK_X));
private SimpleModel simpleModel = new SimpleModel(this);
public SimpleGui() {
for (int i = 0; i < textFields.length; i++) {
textFields[i] = new JTextField(TEXT_FIELD_COLUMNS);
textFields[i].setEditable(false);
textFields[i].setFocusable(false);
add(textFields[i]);
}
add(goButton);
add(exitButton);
}
public void setTextFieldText(int index, String text) {
if (index < 0 || index >= textFields.length) {
throw new IllegalArgumentException("index: " + index);
} else {
textFields[index].setText(text);
}
}
private class GoAction extends AbstractAction {
public GoAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
simpleModel.go();
}
}
private class ExitAction extends AbstractAction {
public ExitAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
Window win = SwingUtilities.getWindowAncestor(btn);
win.dispose();
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("SimpleGui");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new SimpleGui());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class SimpleModel {
public static final int MAX_VALUE = 100;
public static final int VALUES_COUNT = 4;
private Random random = new Random();
private SimpleGui simpleGui;
private int[] values = new int[VALUES_COUNT];
public SimpleModel(SimpleGui simpleGui) {
this.simpleGui = simpleGui;
}
public void go() {
for (int i = 0; i < values.length; i++) {
values[i] = random.nextInt(MAX_VALUE) + 1;
simpleGui.setTextFieldText(i, String.valueOf(values[i]));
}
// TODO: use the values array here
}
}
通过将this
传递给模型的构造函数,将SimpleGui类的引用传递给SimpleModel类:
private SimpleModel simpleModel = new SimpleModel(this);
该模型使用此引用并使用它来设置GUI字段:
class SimpleModel {
//....
private SimpleGui simpleGui;
// ...
public SimpleModel(SimpleGui simpleGui) {
this.simpleGui = simpleGui;
}
然后模型的go方法调用GUI的公共方法来设置文本字段文本:
public void go() {
for (int i = 0; i < values.length; i++) {
values[i] = random.nextInt(MAX_VALUE) + 1;
simpleGui.setTextFieldText(i, String.valueOf(values[i]));
}
// TODO: use the values array here
}
请注意,如果这是真正的MVC,模型视图控制,模型根本不会调用GUI的方法,而是会通知侦听器状态的变化,但是这样做是为了后来的讨论。
答案 1 :(得分:0)
你忘了打电话给nextGo()
替换
if(str.equals("GO"))
new Slots();
与
if(str.equals("GO"))
new Slots().nextGo();
这应该可以解决问题。
更新1:
请从nextGo()中删除静态从静态方法访问非静态对象时,您确定编译代码吗?