- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对使用 swing 还很陌生,我有一个问题。我的代码今天早上停止工作,但我不知道为什么。我运行了调试器,它显示了 2 行代码。一个在主类,一个在另一个类。他们都在创建彼此的实例。
这些行是这样的:主类:
OnePlayerFrame OnePFrame = new OnePlayerFrame();
OnePlayerFrame 类:
MainFrame MainClass = new MainFrame();
我希望你能理解我在说什么,正如我所说的,我对 swing 很陌生。
谢谢-马特
好的,这就是全部内容:对了,忘了说了,我可能删除了一行简单的代码而忽略了它,所以如果是简单的事情请保持冷静:)
主类:
package rockpaperscissors;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainFrame {
public JFrame frame = new JFrame();
private final ImageIcon rock = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/rock.jpg");
private final ImageIcon paper = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/paper.png");
private final ImageIcon scissors = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/scissors.png");
private final JLabel rockLabel = new JLabel();
private final JLabel paperLabel = new JLabel();
private final JLabel scissorsLabel = new JLabel();
private final JPanel panel1 = new JPanel();
private final JPanel panel2 = new JPanel();
private final JPanel panel3 = new JPanel();
private final JPanel empty1 = new JPanel();
private final JPanel empty2 = new JPanel();
private final JPanel empty3 = new JPanel();
private final JPanel empty4 = new JPanel();
private final JPanel empty5 = new JPanel();
OnePlayerFrame OnePlayerFrame = new OnePlayerFrame();
public JButton btn1 = new JButton("1 Player");
private final JTextField text = new JTextField();
private final Font font = new Font("Showcard Gothic Regular", Font.BOLD, 28);
public MainFrame(){
frame.setSize(360, 300);
frame.setAlwaysOnTop(false);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.setLayout(new GridLayout(3, 1));
panel1.setLayout(new GridLayout(1, 3));
panel1.add(rockLabel);
panel1.add(paperLabel);
panel1.add(scissorsLabel);
panel1.setBackground(Color.WHITE);
panel2.setLayout(new GridLayout(2, 3));
panel2.add(empty1);
panel2.add(empty2);
panel2.add(empty3);
panel2.add(empty4);
panel2.add(btn1);
panel2.add(empty5);
panel3.add(text);
panel3.setBackground(Color.WHITE);
empty1.setBackground(Color.WHITE);
empty2.setBackground(Color.WHITE);
empty3.setBackground(Color.WHITE);
empty4.setBackground(Color.WHITE);
empty5.setBackground(Color.WHITE);
text.setText("Rock Paper Scissors");
text.setFont(font);
text.setPreferredSize(new Dimension(360, 80));
text.setEditable(false);
text.setBorder(null);
text.setHorizontalAlignment(JTextField.CENTER);
text.setBackground(Color.WHITE);
rockLabel.setIcon(rock);
paperLabel.setIcon(paper);
scissorsLabel.setIcon(scissors);
btn1.setFocusPainted(false);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
frame.dispose();
OnePlayerFrame.startGame1();
}
});
}
public static void main(String[] args) {
new MainFrame();
}
}
这是第二堂课:
package rockpaperscissors;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class OnePlayerFrame {
MainFrame MainClass = new MainFrame();
JFrame gameFrame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
JPanel empty1 = new JPanel();
JPanel empty2 = new JPanel();
JTextField playerText1 = new JTextField();
JTextField playerText2 = new JTextField();
JTextField playerText3 = new JTextField();
JTextField compText1 = new JTextField();
JTextField compText2 = new JTextField();
JTextField compText3 = new JTextField();
JTextField pChoose = new JTextField();
JTextField cChoose = new JTextField();
JButton btn1 = new JButton("Rock");
JButton btn2 = new JButton("Paper");
JButton btn3 = new JButton("Scissors");
JButton btn4 = new JButton("Confirm");
JButton btn5 = new JButton("Fight!");
JButton getB = new JButton();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
boolean confirmed = false;
boolean hasSelected = false;
boolean runThread = true;
String playerMove;
ImageIcon playerIcon = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/question-mark.jpg");
ImageIcon computerIcon = new ImageIcon("C:/Users/Matthew/Documents/NetBeansProjects/RockPaperScissors/src/assets/question-mark.jpg");
Font font = new Font("Showcard Gothic Regular", Font.BOLD, 18);
public OnePlayerFrame(){
gameFrame.setVisible(true);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setSize(500, 400);
gameFrame.setLocationRelativeTo(MainClass.frame);
gameFrame.setLayout(new GridLayout(3, 1));
gameFrame.add(panel1);
gameFrame.add(panel2);
gameFrame.add(panel3);
MainClass.frame.dispose();
panel1.setBackground(Color.WHITE);
panel1.setLayout(new GridLayout(1, 4));
panel1.add(panel4);
panel1.add(panel5);
panel1.add(panel6);
panel1.add(panel7);
panel2.setBackground(Color.WHITE);
panel2.setLayout(new GridLayout(3, 1));
panel2.add(pChoose);
panel2.add(panel8);
panel2.add(btn4);
panel3.setBackground(Color.WHITE);
panel3.add(cChoose);
panel3.add(panel9);
panel3.setLayout(new GridLayout(2, 1));
panel4.setBackground(Color.WHITE);
panel4.add(playerText1);
panel4.add(playerText2);
panel4.add(playerText3);
panel4.setLayout(new GridLayout(3, 1));
panel5.setBackground(Color.WHITE);
panel5.add(label1);
panel6.setBackground(Color.WHITE);
panel6.add(label2);
panel7.setBackground(Color.WHITE);
panel7.add(compText1);
panel7.add(compText2);
panel7.add(compText3);
panel7.setLayout(new GridLayout(3, 1));
panel8.setLayout(new GridLayout(1, 3));
panel8.add(btn1);
panel8.add(btn2);
panel8.add(btn3);
panel9.setLayout(new GridLayout(1, 3));
panel9.setBackground(Color.WHITE);
panel9.add(empty1);
panel9.add(btn5);
panel9.add(empty2);
empty1.setBackground(Color.WHITE);
empty2.setBackground(Color.WHITE);
playerText1.setEditable(false);
playerText1.setBorder(null);
playerText1.setText("Player");
playerText1.setBackground(Color.WHITE);
playerText1.setFont(font);
playerText1.setHorizontalAlignment(JTextField.CENTER);
playerText2.setEditable(false);
playerText2.setBorder(null);
playerText2.setText("Chose");
playerText2.setBackground(Color.WHITE);
playerText2.setFont(font);
playerText2.setHorizontalAlignment(JTextField.CENTER);
playerText3.setEditable(false);
playerText3.setBorder(null);
playerText3.setText("====>");
playerText3.setBackground(Color.WHITE);
playerText3.setFont(font);
playerText3.setHorizontalAlignment(JTextField.CENTER);
compText1.setEditable(false);
compText1.setBorder(null);
compText1.setText("Computer");
compText1.setBackground(Color.WHITE);
compText1.setFont(font);
compText1.setHorizontalAlignment(JTextField.CENTER);
compText2.setEditable(false);
compText2.setBorder(null);
compText2.setText("Chose");
compText2.setBackground(Color.WHITE);
compText2.setFont(font);
compText2.setHorizontalAlignment(JTextField.CENTER);
compText3.setEditable(false);
compText3.setBorder(null);
compText3.setText("<====");
compText3.setBackground(Color.WHITE);
compText3.setFont(font);
compText3.setHorizontalAlignment(JTextField.CENTER);
pChoose.setEditable(false);
pChoose.setBorder(null);
pChoose.setText("Choose your move");
pChoose.setFont(font);
pChoose.setHorizontalAlignment(JTextField.CENTER);
pChoose.setBackground(Color.WHITE);
cChoose.setEditable(false);
cChoose.setBorder(null);
cChoose.setText("Computer is choosing");
cChoose.setFont(font);
cChoose.setHorizontalAlignment(JTextField.CENTER);
cChoose.setBackground(Color.WHITE);
btn1.setRolloverEnabled(true);
btn1.setFocusPainted(false);
btn2.setFocusPainted(false);
btn2.setRolloverEnabled(true);
btn3.setFocusPainted(false);
btn3.setRolloverEnabled(true);
btn4.setFocusPainted(false);
btn4.setRolloverEnabled(true);
btn5.setFocusPainted(false);
btn5.setRolloverEnabled(true);
btn5.setVisible(false);
btn5.setFont(font);
label1.setIcon(playerIcon);
label2.setIcon(computerIcon);
}
public void startGame1(){
new Thread(new Runnable() {
public void run() {
while(confirmed == true || confirmed == false){
if(confirmed == false){
try{
if(confirmed == false){
cChoose.setText("Computer is choosing");
Thread.sleep(500);
}
if(confirmed == false){
cChoose.setText("Computer is choosing.");
Thread.sleep(500);
}
if(confirmed == false){
cChoose.setText("Computer is choosing..");
Thread.sleep(500);
}
if(confirmed == false){
cChoose.setText("Computer is choosing...");
Thread.sleep(500);
}
}
catch(Exception e){
gameFrame.dispose();
}
}
if(confirmed == true){
cChoose.setText("Computer has chosen!");
fightButtonSet();
}
}
}
}).start();
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(confirmed == false){
playerMove = btn1.getText();
hasSelected = true;
pChoose.setText("You have selected rock");
getB = btn1;
}
else{
pChoose.setText("You have already confirmed " + getB.getText().toLowerCase() + "!");
}
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(confirmed == false){
playerMove = btn2.getText();
hasSelected = true;
pChoose.setText("You have selected paper");
getB = btn2;
}
else{
pChoose.setText("You have already confirmed " + getB.getText().toLowerCase() + "!");
}
}
});
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(confirmed == false){
playerMove = btn3.getText();
hasSelected = true;
pChoose.setText("You have selected scissors");
getB = btn3;
}
else{
pChoose.setText("You have already confirmed " + getB.getText().toLowerCase() + "!");
}
}
});
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(hasSelected == true && confirmed == false){
confirmed = true;
pChoose.setText("You have confirmed " + getB.getText().toLowerCase() + "!");
cChoose.setText("Computer has chosen!");
}
else{
if(hasSelected == false){
pChoose.setText("You haven't selected anything yet!");
}
else if (confirmed == true){
pChoose.setText("You have already confirmed " + getB.getText().toLowerCase() + "!");
}
}
}
});
}
public void fightButtonSet(){
btn5.setVisible(true);
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
FIGHT();
}
});
}
public void FIGHT(){
if(getB.getText().equalsIgnoreCase("Rock")){
}
if(getB.getText().equalsIgnoreCase("Paper")){
}
if(getB.getText().equalsIgnoreCase("Scissors")){
}
}
}
最佳答案
我们可以抽象一点你有这个。
public class TestCircularDependency {
static class A{
B b = new B();
}
static class B{
A a = new A();
}
public static void main(String args[]){
A a = new A();
}
}
这将抛出一个StackOverflowError
。你有一个循环依赖。问题是你在构造函数中实例化每个实例,然后你就有无限递归。
在java中你可以有循环依赖,但在对象创建中不能。你可以做这样的事情。
public class TestCircularDependency {
static class A{
B b;
}
static class B{
A a;
}
public static void main(String args[]){
A a = new A();
a.b = new B();
b.a= a;
}
}
关于Java Swing 应用程序不再工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440482/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!