Reputation: 61
I have a JButton
that has an action assigned to it that is blank.
I want it to increment a variable by one each time I click it, and display that value inside of a JTextField
named "AgeOutputBox".
I have been trying for over an hour, and I still can't seem to do it right. I am probably missing something obvious again, but please note that I am still somewhat new to Java. Any answers should help. If possible, I'd like a short and simplified bit of code to keep it neat.
Here is the code
package mainPackage;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class MainGUIClass extends JFrame {
public MainGUIClass() {
}
//Default auto-generated variables
private static final long serialVersionUID = 8360937123719531815L;
private static JTextField AgeStringBox;
private static JTextField AgeOutputBox;
//End of default auto-generated variables
public static void mainGUIClassMethod() {//In order to see this in Window builder, change name to mainGUIClass()
//My variables area
//End of variable area
JFrame frame = new JFrame();
frame.setTitle("Text Based Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setBounds(0, 0, 434, 261);
frame.getContentPane().add(layeredPane);
layeredPane.setLayout(null);
JTabbedPane MenuBar = new JTabbedPane(JTabbedPane.TOP);
MenuBar.setBounds(0, 0, 139, 19);
layeredPane.add(MenuBar);
JMenuBar JobsTab = new JMenuBar();
MenuBar.addTab("Jobs", null, JobsTab, null);
JPanel JobsPanel = new JPanel();
JobsTab.add(JobsPanel);
JMenuBar ShopTab = new JMenuBar();
MenuBar.addTab("Shop", null, ShopTab, null);
MenuBar.setEnabledAt(1, true);
JPanel ShopPanel = new JPanel();
ShopTab.add(ShopPanel);
ShopPanel.setLayout(null);
JMenuBar StatsTab = new JMenuBar();
MenuBar.addTab("Stats", null, StatsTab, null);
JPanel StatsPanel = new JPanel();
StatsTab.add(StatsPanel);
StatsPanel.setLayout(null);
JPanel MainGamePanel = new JPanel();
MainGamePanel.setBounds(0, 0, 434, 261);
layeredPane.add(MainGamePanel);
/*I need the button below to increment a variable by 1 each time,
*and display it on the AgeOutputBox. In order to see it in Window
*Builder, rename MainGUIClassMethod() to MainGUIClass(), and comment
*out, or remove the main method at the bottom. This will make it easier
*to see what you are looking at.
*/
/*-->*/ Button AgeButton = new Button("Age");
AgeButton.setBounds(0, 218, 434, 43);
AgeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Assign action to AgeButton here
}
});
MainGamePanel.setLayout(null);
MainGamePanel.add(AgeButton);
JScrollPane OutputScrollPane = new JScrollPane();
OutputScrollPane.setBounds(0, 21, 434, 197);
OutputScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
MainGamePanel.add(OutputScrollPane);
JTextArea TextOutputArea = new JTextArea();
OutputScrollPane.setViewportView(TextOutputArea);
TextOutputArea.setEditable(false);
TextOutputArea.setForeground(Color.WHITE);
TextOutputArea.setBackground(Color.GRAY);
TextOutputArea.setFont(new Font("Calibri", Font.PLAIN, 15));
AgeStringBox = new JTextField();
AgeStringBox.setBounds(348, 0, 37, 20);
AgeStringBox.setEditable(false);
AgeStringBox.setText("Age:");
MainGamePanel.add(AgeStringBox);
AgeStringBox.setColumns(10);
AgeOutputBox = new JTextField();
AgeOutputBox.setBounds(382, 0, 52, 20);
AgeOutputBox.setEditable(false);
AgeOutputBox.setColumns(10);
AgeOutputBox.setText("" + 1);
MainGamePanel.add(AgeOutputBox);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
mainGUIClassMethod();
}
});
}
}
Upvotes: 1
Views: 75
Reputation: 4084
public MainGUIClass() {
...
private int age = 1;
...
ageOutputBox.setText("" + age);
...
ageButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ageOutputBox.setText("" + (++age) );
}
});
}
Also, the ageOutputBox should be made non-editable, or else add listeners to modify the age variable if the user edits the contents.
Upvotes: 1