我现在正在上一门java的课,我决定尝试制作自己的程序。我遇到了一个问题,使一个简单的闲置点击游戏,我不完全确定我做错了什么。我一直试图在运行程序时打开一个菜单,但是由于某些原因,我添加到jframe的组件没有出现。
我尝试过重新组织我的代码,也尝试过一些解决方案,但是所有的问题都是通过 setVisible(true);
在将所有组件添加到一个框架之后,我已经完成了。
这是我的密码:
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame implements ActionListener{
int counter = 10000;
int autoClickerCost = 100;
int autoClickerDelay = 1000;
int autoClickerCount = 0;
boolean hasAutoClicker;
JLabel counterField, autoClickerSplash, invText, autoClickerCountTxt;
JButton countUp, autoClickerUpgrade;
JPanel clickerPanel, upgrades, shop, inv, mainPanel;
SpringLayout overallLayout, upgradesLayout, clickerPanelLayout, shopLayout, invLayout, mainPanelLayout;
Timer autoClickerTimer;
public Main() {
super("Idle clicker");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
overallLayout = new SpringLayout();
pane.setLayout(overallLayout);
inv = new JPanel();
invLayout = new SpringLayout();
inv.setLayout(invLayout);
mainPanel = new JPanel();
mainPanelLayout = new SpringLayout();
mainPanel.setLayout(mainPanelLayout);
clickerPanel = new JPanel();
clickerPanelLayout = new SpringLayout();
clickerPanel.setLayout(clickerPanelLayout);
upgrades = new JPanel();
upgradesLayout = new SpringLayout();
upgrades.setLayout(upgradesLayout);
invText = new JLabel("Inventory:");
invText.setFont(invText.getFont().deriveFont(24.0f));
counterField = new JLabel("Current Clicks: " + counter);
counterField.setFont(counterField.getFont().deriveFont(24.0f));
autoClickerCountTxt = new JLabel("Autoclickers: " + autoClickerCount);
autoClickerCountTxt.setFont(autoClickerCountTxt.getFont().deriveFont(12.0f));
countUp = new JButton("Click Me");
countUp.setFont(countUp.getFont().deriveFont (12.0f));
countUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
counter++;
counterField.setText("Current Clicks: " + counter);
if(counter > 50 && hasAutoClicker == false) {
upgrades.add(autoClickerUpgrade);
upgradesLayout.putConstraint(SpringLayout.NORTH, autoClickerUpgrade, 5, SpringLayout.NORTH, upgrades);
upgradesLayout.putConstraint(SpringLayout.WEST, autoClickerUpgrade, 5, SpringLayout.WEST, upgrades);
}
if(hasAutoClicker == true) {
}
}
});
autoClickerUpgrade = new JButton("Autoclicker: " + autoClickerCost + " clicks");
autoClickerUpgrade.setFont (countUp.getFont ().deriveFont (12.0f));
autoClickerUpgrade.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(counter >= autoClickerCost && hasAutoClicker == false) {
autoClickerDelay = 1000;
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
counter = counter + 1;
counterField.setText("Current Clicks: " + counter);
}
};
autoClickerTimer = new Timer(autoClickerDelay, taskPerformer);
autoClickerTimer.start();
hasAutoClicker = true;
autoClickerCount = autoClickerCount + 1;
autoClickerCountTxt.setText("Autoclickers: " + autoClickerCount);
counter = counter - autoClickerCost;
counterField.setText("Current Clicks: " + counter);
autoClickerCost = (int) Math.round(autoClickerCost * 1.25);
autoClickerUpgrade.setText("Autoclicker: " + autoClickerCost + " clicks");
}
else if(counter >= autoClickerCost && hasAutoClicker == true) {
if(autoClickerDelay > 1) {
autoClickerCount = autoClickerCount + 1;
autoClickerCountTxt.setText("Autoclickers: " + autoClickerCount);
autoClickerDelay = (int) Math.ceil(1000 / autoClickerCount);
autoClickerTimer.setDelay(autoClickerDelay);
hasAutoClicker = true;
counter = counter - autoClickerCost;
counterField.setText("Current Clicks: " + counter);
autoClickerCost = (int) Math.round(autoClickerCost * 1.5);
autoClickerUpgrade.setText("Autoclicker: " + autoClickerCost + " clicks");
}
}
}
});
inv.add(invText);
invLayout.putConstraint(SpringLayout.WEST, invText, 5, SpringLayout.WEST, inv);
invLayout.putConstraint(SpringLayout.NORTH, invText, 5, SpringLayout.NORTH, inv);
inv.add(autoClickerCountTxt);
invLayout.putConstraint(SpringLayout.WEST, autoClickerCountTxt, 5, SpringLayout.WEST, inv);
invLayout.putConstraint(SpringLayout.NORTH, autoClickerCountTxt, 10, SpringLayout.SOUTH, invText);
clickerPanel.add(counterField);
clickerPanelLayout.putConstraint(SpringLayout.WEST, counterField, 5, SpringLayout.WEST, clickerPanel);
clickerPanelLayout.putConstraint(SpringLayout.NORTH, counterField, 5, SpringLayout.NORTH, clickerPanel);
clickerPanel.add(countUp);
clickerPanelLayout.putConstraint(SpringLayout.NORTH, countUp, 5, SpringLayout.SOUTH, counterField);
clickerPanelLayout.putConstraint(SpringLayout.WEST, countUp, 5, SpringLayout.WEST, clickerPanel);
mainPanel.add(clickerPanel);
mainPanelLayout.putConstraint(SpringLayout.NORTH, clickerPanel, 5, SpringLayout.NORTH, mainPanel);
mainPanelLayout.putConstraint(SpringLayout.WEST, clickerPanel, 5, SpringLayout.WEST, mainPanel);
mainPanel.add(upgrades);
mainPanelLayout.putConstraint(SpringLayout.NORTH, upgrades, 5, SpringLayout.NORTH, mainPanel);
mainPanelLayout.putConstraint(SpringLayout.WEST, upgrades, 5, SpringLayout.SOUTH, clickerPanel);
pane.add(inv);
overallLayout.putConstraint(SpringLayout.NORTH, inv, 5, SpringLayout.NORTH, pane);
overallLayout.putConstraint(SpringLayout.WEST, inv, 5, SpringLayout.WEST, pane);
pane.add(mainPanel);
overallLayout.putConstraint(SpringLayout.NORTH, mainPanel, 5, SpringLayout.SOUTH, inv);
overallLayout.putConstraint(SpringLayout.WEST, mainPanel, 5, SpringLayout.WEST, pane);
setContentPane(pane);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
}
public static void main(String[] args) {
Main example = new Main();
}
}
我觉得我错过了一些非常明显的东西,或者可能有一个关键的概念我还不知道。太好了!
1条答案
按热度按时间kmb7vmvb1#
正如我在评论中提到的,一个常见问题的简单答案是:
但我想跟你说得再深入一点。swing是单线程的。这意味着在setvisible调用之后,所有添加/更改/删除jcomponents的操作都应该在swing/eventdispatchingthread上完成。这样做的方法(通常)是创建runnable并使用swingutilities.invokelater
这样可以确保这些操作是线程安全的,并且在edt绘制组件时不会发生。