Java JFrame/JButton错误消息
我很难将Java JFrame/JButton错误消息,java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,我很难将JButtons添加到我的JFrame 我已经创建了两个方法(目前都在同一个类中)。如果将showGUI方法设为静态,则会收到错误: //Listen for actions on buttons. next.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT) previous.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT) classi
JButton
s添加到我的JFrame
我已经创建了两个方法(目前都在同一个类中)。如果将showGUI
方法设为静态,则会收到错误:
//Listen for actions on buttons.
next.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT)
previous.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT)
classify.addActionListener(this); (CANNOT USE THIS IN A STATIC CONTEXT)
将JButton
对象添加到JFrame时,我收到以下错误:
add(next); (Cannot make a static reference to the non-static method add(Component) from the type Container)
add(previous); (Cannot make a static reference to the non-static method add(Component) from the type Container)
add(classify); (Cannot make a static reference to the non-static method add(Component) from the type Container)
我怎样才能克服这个问题?我已将我的方法包括在下面以供参考:
public void showGUI(BufferedImage img){
next = new JButton("Next Image");
next.setMnemonic(KeyEvent.VK_N);
next.setActionCommand("disable");
previous = new JButton("Previous Image");
previous.setMnemonic(KeyEvent.VK_P);
previous.setActionCommand("disable");
classify = new JButton("Classify");
classify.setMnemonic(KeyEvent.VK_C);
classify.setActionCommand("disable");
//Listen for actions on buttons.
next.addActionListener(this);
previous.addActionListener(this);
classify.addActionListener(this);
add(next);
add(previous);
add(classify);
//Display image on the screen.
frame.setTitle("TITLE");
RMHC newContentPane = new RMHC();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);
frame.isResizable();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
使用,您的GUI可以基于from tutorial使用,您的GUI可以基于from tutorial如果某个东西是静态的,您不在任何情况下。因此,您不能使用“this”…
在发布类似文章之前,请先学习java基础知识。
如果你需要帮助,在全班张贴我知道你想做什么。。。否则我帮不了你。如果某个东西是静态的,那么你在任何情况下都不是静态的。因此,您不能使用“this”…
在发布类似文章之前,请先学习java基础知识。
如果你需要帮助,在全班张贴我知道你想做什么。。。否则我无法帮助您。我认为问题在于您从静态的main方法调用方法showGUI,因此最好是在main方法(也在EDT中)中初始化GUI框架,如:
我认为问题是因为您从静态的main方法调用方法showGUI,所以最好是在main方法中初始化GUI框架(也在EDT中),如:
为了更快地获得更好的帮助,请发布一条消息。当您随后立即创建新对象时,为什么要将nextImg设置为next?@stas我的代码现在已更新。为了更快地获得更好的帮助,请发布一条消息。当您之后立即创建新对象时,为什么要将nextImg设置为next?@stas我的代码现在已更新。
public class MainApp {
public static void main(String... args) {
EventQueue.invokeLater(
new Runnable() {
@Override
public void run() {
JFrame frame= new YourFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
);
}
}
class YourFrame () extends JFrame implements ActionListener{
public YourFrame() {
}
public void showGUI(BufferedImage img){
}
private void add(JButtob button) {
}
}