使用内部类(Java)修改变量
问题描述:
所以,目前我是自学Java,我想用一个小按钮和一个使用Swing的文本字段编写一个简单的计算器。虽然我设法实际创建了一个文本字段,其中填充了文本变量,但我无法使按钮更改字段中的文本。我不断收到关于内部类无法更改外部变量的错误。什么是完成我的目标的最佳方式,为什么?使用内部类(Java)修改变量
在此先感谢,下面的代码:
退房哪里我的第一个按钮的动作(Add按钮)是,这就是我在测试更改文本字段的能力。
/**
* Created by Ray on 7/19/2015.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class main extends JFrame {
double x = 0;
double y = 0;
double z = 0;
public main() {
initUI();
}
private void initUI() {
setLayout(null);
JPanel text = new JPanel();
text.setLayout(new BorderLayout());
text.setBounds(100, 10, 200, 25);
JScrollPane pane = new JScrollPane();
JTextArea area = new JTextArea();
area.setLineWrap(true);
area.setWrapStyleWord(true);
area.setBounds(100, 10, 200, 25);
pane.getViewport().add(area);
text.add(pane);
String contents = "Test";
area.setText(contents);
JButton addButton = new JButton("Add");
addButton.setToolTipText("Addition operation.");
addButton.setBounds(10, 10, 80, 25);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
contents = "Pressed";
}
});
JButton subButton = new JButton("Sub");
subButton.setToolTipText("Subtraction operation.");
subButton.setBounds(10, 40, 80, 25);
subButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
JButton mulButton = new JButton("Mul");
mulButton.setToolTipText("Multiplication operation.");
mulButton.setBounds(10, 70, 80, 25);
mulButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
JButton divButton = new JButton("Div");
divButton.setToolTipText("Division operation.");
divButton.setBounds(10, 100, 80, 25);
divButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
add(text);
add(addButton);
add(subButton);
add(mulButton);
add(divButton);
setTitle("Simple example");
setSize(500, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
main ex = new main();
ex.setVisible(true);
}
});
}
}
答
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloWorld extends JFrame {
double x = 0;
double y = 0;
double z = 0;
public HelloWorld() {
initUI();
}
private void initUI() {
setLayout(null);
JPanel text = new JPanel();
text.setLayout(new BorderLayout());
text.setBounds(100, 10, 200, 25);
JScrollPane pane = new JScrollPane();
JTextArea area = new JTextArea();
area.setLineWrap(true);
area.setWrapStyleWord(true);
area.setBounds(100, 10, 200, 25);
pane.getViewport().add(area);
text.add(pane);
String contents1="Text";
area.setText(contents1);
JButton addButton = new JButton("Add");
addButton.setToolTipText("Addition operation.");
addButton.setBounds(10, 10, 80, 25);
addButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent event) {
final String contents = "Pressed";
}
});
JButton subButton = new JButton("Sub");
subButton.setToolTipText("Subtraction operation.");
subButton.setBounds(10, 40, 80, 25);
subButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
JButton mulButton = new JButton("Mul");
mulButton.setToolTipText("Multiplication operation.");
mulButton.setBounds(10, 70, 80, 25);
mulButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
JButton divButton = new JButton("Div");
divButton.setToolTipText("Division operation.");
divButton.setBounds(10, 100, 80, 25);
divButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
add(text);
add(addButton);
add(subButton);
add(mulButton);
add(divButton);
setTitle("Simple example");
setSize(500, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
HelloWorld ex = new HelloWorld();
ex.setVisible(true);
}
});
}
}
你只需要声明两个变量,我们可以使用此方法只最终局部变量内class.Or声明一个变量最终并使用相同的名称,文本和按钮,你不要这不想要。 所以,如果你在外面定义了一个普通的字符串,你可以正常地将它用于文本字段,但是你不能在方法本地内部类中使用它。 如果你在外面定义了一个最终变量,只需将它添加到方法local inner class中,但不要试图再次更改它,因为它是Final。
答
如果您将delogle变量String contents
作为该类的成员,那么您将不会面临此问题。在代码中,将其称为contents
以便不覆盖它,因此请从initGui()
方法中删除任何String contents = ...
声明。
public class main extends JFrame {
double x = 0;
double y = 0;
double z = 0;
String contents = "Test";
public main....
答
我建议你在实例和一个类(静态)变量之间的差异读了。
contents
是main
的实例变量,这意味着您只能在main
的上下文中引用它。创建新的ActionListener
时,您正在创建新的Anonymous
实例ActionListener
,并尝试从新创建的ActionListener
而不是main
的实例中访问contents
。
您可以通过直接从ActionEvent
访问JButton
解决您的问题:
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
Component comp = (Component) event.getSource();
if (comp instanceof JButton) {
((JButton)comp).setText("Pressed");
}
}
});
只是变量'contents'是本地'initUI'方法,所以你只需要把它声明为'final'在使用之前\ –
避免使用'null'布局,像素完美的布局是现代UI设计中的幻想。影响组件的个体大小的因素太多,其中没有一个可以控制。 Swing旨在与布局管理人员一起工作,放弃这些将导致无法结束的问题和问题,您将花费越来越多的时间来尝试纠正 – MadProgrammer
您是否尝试过使用'area.append(“一些新文本”) ;'? – MadProgrammer