[英]JFrame components are not showing up after being added
I am currently taking a class on Java and I decided to try to make my own program.我目前正在 Java 上使用 class,我决定尝试制作自己的程序。 I've run into a problem in making a simple idle clicker game and I'm not exactly sure what I am doing wrong.
我在制作一个简单的空闲点击游戏时遇到了问题,我不确定我做错了什么。 I have been trying to get a menu open up when I run the program but for some reason, the components that I add to the JFrame don't appear.
当我运行程序时,我一直试图打开一个菜单,但由于某种原因,我添加到 JFrame 的组件没有出现。
I have tried reorganizing my code and I've done some looking around on solutions, but all of the problems are solved by putting setVisible(true);
我已经尝试重新组织我的代码,并且我已经对解决方案进行了一些环顾,但是所有问题都通过
setVisible(true);
解决了。 after adding all of the components to a frame, which I have already done.在将所有组件添加到框架之后,我已经完成了。
Here's my code:这是我的代码:
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();
}
}
I feel like I am missing something quite obvious or maybe there is a key concept I don't know yet.我觉得我错过了一些非常明显的东西,或者可能有一个我还不知道的关键概念。 If anyone could just nudge me in the right direction that would be great!
如果有人可以将我推向正确的方向,那就太好了!
As mentioned in my comment, easy answer to a common issue:正如我的评论中提到的,一个常见问题的简单答案:
parent.revalidate(); parent.repaint();
But I want to take this a little further with you.但我想和你一起更进一步。 Swing is built to be single-threaded.
Swing 被构建为单线程。 Which means after your setVisible call, all adding/changing/removing JComponents should be done on the Swing / EventDispatchingThread.
这意味着在您的 setVisible 调用之后,所有添加/更改/删除 JComponents 都应该在 Swing / EventDispatchingThread 上完成。 The way you do that (normally) is creating runnables and using SwingUtilities.invokeLater
你这样做的方式(通常)是创建可运行文件并使用 SwingUtilities.invokeLater
SwingUtilities.invokeLater(new Runnable() {
public void run() {
parentX.add(childY);
parentX.revalidate();
parentX.repaint();
}
}
This way you ensure these operations are thread-safe and not happening while the EDT is painting the component.这样可以确保这些操作是线程安全的,并且在 EDT 绘制组件时不会发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.