Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从单独的类向框架添加按钮?_Java_Class_Jbutton - Fatal编程技术网

Java 如何从单独的类向框架添加按钮?

Java 如何从单独的类向框架添加按钮?,java,class,jbutton,Java,Class,Jbutton,这是三个独立的类。现在,我想能够添加按钮到主布局框架,但似乎无法找到它。当我运行这段代码时,我遇到了一个错误,我尝试了不同的方法,比如在主函数中创建按钮的实例,代码会编译,但只会弹出一个框架 听起来好像没有EventButtonextendmainloayout(因为它将是一个单独的实例,所以无法工作),您要做的是: import javax.swing.JButton; import javax.swing.JPanel; public class EventBut

这是三个独立的类。现在,我想能够添加按钮到主布局框架,但似乎无法找到它。当我运行这段代码时,我遇到了一个错误,我尝试了不同的方法,比如在主函数中创建按钮的实例,代码会编译,但只会弹出一个框架

听起来好像没有
EventButton
extend
mainloayout
(因为它将是一个单独的实例,所以无法工作),您要做的是:

    import javax.swing.JButton;
    import javax.swing.JPanel;


    public class EventButton extends MainLayout{
        //create the string for the button's text 
        private String button_text; 
        private JButton event_button; 
        private JPanel event_panel;



        public EventButton(){
            //initialize the button text
            button_text = "Set Event";
            event_button = new JButton();
            event_panel = new JPanel();
            event_button.setText(button_text);
            event_panel.add(event_button);
            add(event_panel);



        }
}


import javax.swing.JFrame;

public class MainLayout extends JFrame{
    public MainLayout(){
        EventButton eb = new EventButton();
    }
}


import javax.swing.*;
public class Tester {
    public static void main(String[] args){
        MainLayout frame = new MainLayout();
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

}
构造函数EventButton(容器)未定义
// Change the EventButton() constructor:
public EventButton(Container addTo) {
   ...
   addTo.add(event_panel);
}

// Change your MainLayout() constructor:
public MainLayout(){
   EventButton eb = new EventButton(getContentPane());
}