我正在尝试编写一段代码,当我选中其中一个复选框时,它会更改我选择按钮时出现的消息。
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);
}
}
我收到的错误消息是:
“FirstWindow.java:12:错误:无法从静态上下文引用非静态变量消息 message =“goodjob \ n”;“
第12,37,53,57行的。我尝试在main中声明String变量,但之后我只会收到错误:
“FirstWindow.java:38:错误:从内部类引用的局部变量必须是最终的或有效的最终JOptionPane.showMessageDialog(null,message);”
如何解决这个问题?
答案 0 :(得分:0)
将消息字段设为静态:
static String message;
你的主要类是静态的(因为它必须是)。这意味着您没有班级实例。但是字段消息当前不是静态的,这意味着它只作为实例的一部分存在。由于您没有实例,因此无法访问它。
作为旁注:我强烈建议您让某人代码审核此代码。有很多东西需要学习。请考虑将其提交至Code Review。
答案 1 :(得分:0)
这里的问题是,您在public static void main()
之外声明了非静态变量字符串消息,这是一种静态方法。
所以有三种可能的解决方案
在main方法中声明字符串消息
public static void main(String []args){
String message = "goodjob \n";
根据上面的答案,您可以将String消息更改为静态
static String message;
public static void main(String []args){
message = "goodjob \n";
创建FirstWindow类的对象,然后访问String消息
FirstWindow sampleFirst = new FirstWindow();
sampleFirst.message = "goodjob \n";
答案 2 :(得分:0)
我建议您创建一个构造函数,并避免在这种情况下使用静态变量:
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);
}
}