所以我正在尝试为程序制作一个复活节彩蛋模式。我想要它做的是如果你点击一个按钮,它会将布尔eastermode
设置为true
。我试着让它成为最终的,公开的,私人的和许多不同的东西,但我最终决定使用来自另一个类的getter和setter。但是,这不起作用。这是我的JConsole类(稍微更改为删除此问题的不相关部分):
package main.java;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingWorker;
public class JConsole {
private KeyManager keyManager;
private boolean running = false;
private static EggManager eggmanager;
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Identity Visual");
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel topPnl = new JPanel();
JPanel btnPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel morePnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
JTextPane jta = new JTextPane();
JButton values = new JButton("I Value..");
JButton ideas = new JButton("I Believe..");
JButton inside = new JButton("Me On The Inside");
JButton outside = new JButton("Me On The Outside");
JButton other = new JButton("other About Me");
JButton influences = new JButton("How have the characteristics listed above influenced who you are as a person?");
JButton easter = new JButton(“Easter eggs“);
btnPnl.add(values);
btnPnl.add(ideas);
btnPnl.add(inside);
btnPnl.add(outside);
btnPnl.add(other);
morePnl.add(influences);
morePnl.add(easter);
btnPnl.setBorder(BorderFactory.createLineBorder(Color.BLACK));
mainPanel.add(topPnl, BorderLayout.NORTH);
mainPanel.add(btnPnl, BorderLayout.NORTH);
mainPanel.add(morePnl, BorderLayout.SOUTH);
mainPanel.add(jta,BorderLayout.CENTER);
frame.setLayout(new BorderLayout());
values.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void, Object>(){
@Override
protected Void doInBackground() throws Exception {
outputTest(“values”);
return null;
}}.execute();
}});
ideas.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void, Object>(){
@Override
protected Void doInBackground() throws Exception {
outputTest(“ideas”);
return null;
}}.execute();
}});
inside.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void, Object>(){
@Override
protected Void doInBackground() throws Exception {
outputTest(“inside");
return null;
}}.execute();
}});
outside.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void, Object>(){
@Override
protected Void doInBackground() throws Exception {
outputTest("outside.");
return null;
}}.execute();
}});
other.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void, Object>(){
@Override
protected Void doInBackground() throws Exception {
outputTest("1. I like to create things");
Thread.sleep(1000);
outputTest("2. I really like computers");
Thread.sleep(1300);
outputTest("3. I love dogs");
Thread.sleep(1250);
outputTest("4. I like reading");
Thread.sleep(1250);
outputTest("5. I really like oranges.");
Thread.sleep(1340);
outputTest("other.");
return null;
}}.execute();
}});
influences.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void, Object>(){
@Override
protected Void doInBackground() throws Exception {
outputTest("INFULENCES#TEMP");
if (eggmanager.eastermode = true){
outputTest("INFULENCES#EASTER");
//Right here is where I want it to actually show INFULENCES#EASTER when clicked and eastermode = true.
}
return null;
}}.execute();
}});
easter.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void, Object>(){
@Override
protected Void doInBackground() throws Exception {
outputTest("Easter egg mode activated");
eggmanager.setEastermode(true);
return null;
}}.execute();
}}
);
frame.add(mainPanel);
frame.setSize(1000, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
console(jta);
}
public static void outputTest(String msg){
System.out.println(msg);
}
public static void console(final JTextPane area) throws IOException {
area.setContentType("text/html");
final PipedInputStream outPipe = new PipedInputStream();
System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));
new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
@SuppressWarnings("resource")
Scanner s = new Scanner(outPipe);
while (s.hasNextLine()){
String line = s.nextLine();
publish(line + "\n");
}
return null;
}
@Override
protected void process(List<String> chunks) {
for (String line : chunks){
area.setText("<font size=\"10\" color=\"red\">"+line+"</font>");
}
}
}.execute();
}
private void tick() {
keyManager.tick();
}
public void run() {
int fps = 60;
double timePerTick = 1000000000 / fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
long timer = 0;
int ticks = 0;
while(running) {
now = System.nanoTime();
delta +=(now - lastTime) / timePerTick;
timer += now - lastTime;
lastTime = now;
if(delta >= 1) {
tick();
ticks++;
delta--;
}
if (timer >= 1000000000){
System.out.println("fps: " + ticks);
ticks = 0;
timer = 0;
}
}
}
}
这是我的EggManager(忽略密钥管理器,不重要的问题): 包main.java;
public class EggManager {
public boolean eastermode = false;
public boolean isEastermode() {
return eastermode;
}
public void setEastermode(boolean eastermode) {
this.eastermode = eastermode;
}
}
如果有人可以提供帮助,那就太棒了。感谢。