Java 动作初始化中的差异
我试图创建一个程序,其中Action类是一个类的内部类,但其对象的初始化发生在另一个类中,但我总是遇到异常。下面我将介绍我的问题的简化模型: 有一个类用于构建GUI并包含内部操作类:Java 动作初始化中的差异,java,swing,event-handling,action,Java,Swing,Event Handling,Action,我试图创建一个程序,其中Action类是一个类的内部类,但其对象的初始化发生在另一个类中,但我总是遇到异常。下面我将介绍我的问题的简化模型: 有一个类用于构建GUI并包含内部操作类: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; public class So
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class SomeClass {
JTextArea area;
public static void main(String[] args){
SomeClass outerAction = new SomeClass();
outerAction.go();
}
public void go(){
JFrame frame = new JFrame();
area = new JTextArea(5,10);
JToolBar toolBar = new JToolBar(SwingConstants.HORIZONTAL);
CreateAction createAction = new CreateAction();
JButton button1 = new JButton(innerGetAction());
JButton button2 = new JButton(createAction.otherGetAction());
toolBar.add(button1);
toolBar.add(button2);
frame.getContentPane().add(BorderLayout.PAGE_START,toolBar);
frame.getContentPane().add(BorderLayout.CENTER,area);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public Action innerGetAction(){
Action action = new SomeAction("DO","Do sth",KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
return action;
}
public class SomeAction extends AbstractAction{
public SomeAction(String name, String desc, KeyStroke key){
super(name);
putValue(SHORT_DESCRIPTION,desc);
putValue(ACCELERATOR_KEY,key);
}
public void actionPerformed(ActionEvent e) {
area.setText("Done");
}
}
}
有两个具有相同操作的JButton,但是第一个的初始化发生在SomeClass中,另一个发生在单独的CreateActionClass中:
import javax.swing.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class CreateAction {
public static Action otherGetAction(){
Action action = new SomeClass().new SomeAction("DO","Do sth",KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
return action;
}
}
一个按钮工作正常,另一个按钮例外。我不明白为什么
你能告诉我:
您将获得NullPointerException,因为您使用了错误的SomeClass在此处创建操作:
Action action = new SomeClass().new SomeAction("DO", "Do sth",
KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
此新SomeClass()
不是显示的SomeClass
而是考虑在可视化对象中传递。< /P>
public static Action otherGetAction(SomeClass someClass) {
return someClass.new SomeAction("DO", "Do sth",
KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
}
如果要在GUI之外进行设置,请考虑将操作从GUI中分离出来。例如:
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Test2 extends JPanel implements Fooable {
private JTextField field = new JTextField(10);
private JTextArea textarea = new JTextArea(20, 30);
private JButton button = new JButton();
public Test2() {
textarea.setFocusable(false);
JScrollPane scrollPane = new JScrollPane(textarea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(field);
add(scrollPane);
add(button);
}
public void setActions(Action action) {
button.setAction(action);
field.setAction(action);
}
@Override
public String getFieldText() {
field.selectAll();
return field.getText();
}
@Override
public void appendText(String text) {
textarea.append(text + "\n");
}
private static void createAndShowGui() {
Test2 mainPanel = new Test2();
Action myAction = new MyAction("Press Me", KeyEvent.VK_P, mainPanel);
mainPanel.setActions(myAction);
JFrame frame = new JFrame("Test2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
interface Fooable {
String getFieldText();
void appendText(String text);
}
class MyAction extends AbstractAction {
private Fooable fooable;
public MyAction(String name, int mnemonic, Fooable fooable) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
this.fooable = fooable;
}
@Override
public void actionPerformed(ActionEvent e) {
if (fooable != null) {
fooable.appendText(fooable.getFieldText());
}
}
}
请参见编辑以回答问题。