从另一个类JPanel调用一个类

时间:2017-11-12 00:21:46

标签: java swing jframe jpanel

最近我开始使用JFrame,而我却因为从另一个类调用一个类而陷入困境。如果有人能帮助我,我将不胜感激。这是代码: 包含两个内联按钮的工具类

package test;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Tools extends JFrame {
    private JButton exit, reset;
    private JFrame jFrame = new JFrame();
    private JPanel jPanel = (JPanel) jFrame.getContentPane();

    public Tools() {
        jPanel.setLayout(new FlowLayout());

        exit = new JButton("Exit");
        reset = new JButton("Reset");

        exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        reset.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                board.reset();
            }
        });

        jPanel.add(exit);
        jPanel.add(reset);

    }

}

控制类,它创建一个Tools对象的实例并将它们放在屏幕的底部(它应该这样做,但是会发生一些错误)

    package test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Control extends JFrame {

    private Tools tButtons;

    Control() {


        JFrame jFrame = new JFrame();
        JPanel jPanel = (JPanel) jFrame.getContentPane();
        jPanel.setLayout(new BorderLayout());

        jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);


        tButtons = new Tools();


        jPanel.add(tButtons, BorderLayout.SOUTH);


        jFrame.setVisible(true);
        jFrame.setSize(350, 350);

    }

}

主要课程:

package test;

public class Main {
    public static void main(String[] args) {
        Control control = new Control();
    }
}

0 个答案:

没有答案