I'm trying to write a piece of code that when I check one of the two checkboxes, it will change the message that appears when I then select the button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FirstWindow extends JFrame{
String message;
public static void main(String[] args){
//the string to show the message for button 1
message = "goodjob \n";
//all of the main window
JFrame frame = new JFrame("heading");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//makes seperate panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel(new GridBagLayout());
//set of buttons
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
//makes an ACTION LISTENER, which tells the button what to do when
//it is pressed.
button1.addActionListener(new ActionListener(){
//the command to button
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, message);
}
});
//for panel adds buttons
panel1.add(button1);
panel1.add(button2);
//set of checkboxes
JCheckBox checkBox = new JCheckBox("Option1");
JCheckBox checkBox2 = new JCheckBox("Option2");
//adds text if the box is checked
if (checkBox.isSelected())
{
message += "you picked option 1";
}
if (checkBox2.isSelected())
{
message += "you picked option 2";
}
//for panel2 adds checkboxes
panel2.add(checkBox);
panel2.add(checkBox2);
//makes a new label, text area and field
JLabel label = new JLabel("This is a label");
JTextArea textArea = new JTextArea("this is a text area");
JTextField textField = new JTextField("text field");
//makes an invisible grid
GridBagConstraints grid = new GridBagConstraints();
grid.insets = new Insets(15, 15, 15, 15);
//sets the the coordinates 0,0. adds the label, and sets position
grid.gridx = 0;
grid.gridy = 0;
panel3.add(label, grid);
//sets the the coordinates 0,1. adds the textArea, and sets position
grid.gridx = 0;
grid.gridy = 1;
panel3.add(textArea, grid);
//sets the the coordinates 0,2. adds the text field, and sets position
grid.gridx = 0;
grid.gridy = 2;
panel3.add(textField, grid);
//adds each PANEL to the FRAME and positions it
frame.add(panel1, BorderLayout.SOUTH);
frame.add(panel3, BorderLayout.CENTER);
frame.add(panel2, BorderLayout.NORTH);
}
}
The error message I'm receiving is:
"FirstWindow.java:12: error: non-static variable message cannot be referenced from a static context message = "goodjob \\n";"
for lines 12, 37, 53, 57. I've tried declaring the String variable in the main but then I will just receive the error:
"FirstWindow.java:38: error: local variables referenced from an inner class must be final or effectively final JOptionPane.showMessageDialog(null, message);"
How to solve this problem?
Make the message field static:
static String message;
Your main class is static (as it has to be). This means you don't have an instance of your class. But the field message is currently not static, this means it only exists as part of an instance. Since you don't have an instance you can't access it.
As a side note: I strongly suggest you get someone to code review this code. There is a lot to learn. Consider submitting it to Code Review .
The problem here is, you declared non-static variable String message outside of public static void main()
which is a static method. So there are three possible solutions
Declare string message inside the main method
public static void main(String []args){ String message = "goodjob \\n";
Based on the answers above, you can change String message to static
static String message; public static void main(String []args){ message = "goodjob \\n";
Create an object of class FirstWindow and then access the String message
FirstWindow sampleFirst = new FirstWindow(); sampleFirst.message = "goodjob \\n";
I recommend you to create a constructor and avoid the use of static variable in this case:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FirstWindow extends JFrame{
private String message;
FirstWindow (){
super("heading");
//the string to show the message for button 1
message = "goodjob \n";
//all of the main window
// remove this: JFrame frame = new JFrame("heading");
// remove this: frame.setVisible(true);
// change frame.setSize(600,400); by:
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// the rest of your code with the JFrame instantiation
// ...
//adds each PANEL to the FRAME and positions it
// here also change frame.add(... by just add(... as follows
add(panel1, BorderLayout.SOUTH);
add(panel3, BorderLayout.CENTER);
add(panel2, BorderLayout.NORTH);
}
public static void main(String[] args){
new FirstWindow().setVisible(true);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.