从另一个类访问并运行主类方法



在我的主类中,从另一个具有JButton的类中找到执行打印方法的方法时遇到问题。

这是我想要执行的Mainclass方法。

public void genInReceipt(Date in, String res) 
        throws Exception
    {
        log.debug("start printing...");
//      TOP
        serialPort.writeString(new String(new byte[] {ESC, 'J', 0x4A}));
        serialPort.writeString(new String(new byte[] {ESC, 'J', 0x4A}));
        serialPort.writeString(new String(new byte[] {ESC, 'J', 0x4A}));
        serialPort.writeString(new String(new byte[] {ESC, 'J', 0x4A}));
        serialPort.writeString(new String(new byte[] {ESC, '@', 0x1b, 0x61, 0x01}));
        serialPort.writeString(new String(new byte[] {ESC, '!', 0x08}));
        serialPort.writeString(new String(new byte[] {ESC, 'E', 0x1b}));
        serialPort.writeString(String.format("%sn", "PLAZA INDONESIA"));
        serialPort.writeString(new String(new byte[] {ESC, 'J', 0x4A}));
        serialPort.writeString(new String(new byte[] {ESC, 'J', 0x4A}));
//      PARAGRAPH 1 Still Fix(Add Parimeter)
        serialPort.writeString(new String(new byte[] {ESC, '!', 0x08, ESC, 'a', 0x00}) + String.format("%sn","    1343KZT/MOBIL"));// +  new String(new byte[] {ESC, 'E', 0x1B, GS, '!', 0x10, 0x01}) + String.format("        MOBIL"));
        serialPort.writeString(String.format("%sn", "    PP6-ADE SILFIANAH"));
        serialPort.writeString(String.format("    In      : %sn", df.format(in)));
        serialPort.writeString(String.format("    Out     : %sn", "21 Jul 2016  17:00:00"));
        serialPort.writeString(String.format("    Duration: %sn", "1 hours 29 minutes")); //Pakegate
        serialPort.writeString(new String(new byte[] {ESC, 'J', 0x4A}));
        serialPort.writeString(String.format("    Sewa Parkir: %sn", "Rp     6.000"));
        serialPort.writeString(new String(new byte[] {ESC, 'J', 0x4A}));
//      BOTTOMLINE
        serialPort.writeString(new String(new byte[] {ESC, 'a', 0x01}));
        serialPort.writeString(new String("TERIMA KASIHn".getBytes()));
        serialPort.writeString(new String("ATAS KUNJUNGAN ANDAn".getBytes())); 
        log.debug("              ... done");        
        serialPort.writeString(new String(new byte[] {GS, 'v', 0x1D}));
        serialPort.writeString(new String(new byte[] {0x1b, 0x64, 0x05}));
        serialPort.writeString(new String(new byte[] {0x1d, 0x56, 0x42, 0x00}));
    }

这是我的GUI代码

package unibit.embedded.parking;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

public class GUIPrinter {
    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button1 = new JButton();
        frame.add(panel);
        panel.add(button1);
        frame.setVisible(true);
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //What i have to add here to execute genInReciept method?
            }
        });
    }
}

我知道我的代码有问题,有人能帮我吗?

您需要用genInReceipt创建类的实例,然后这样调用它:

  ClassWithGenInReceiptMethod o = new ClassWithGenInReceiptMethod();
  o.genInReceipt();

但首先,如果能开始阅读有关java和编程本身的基础知识,那就太好了。

您需要/想要的非常简单,您需要一个类的实例并调用方法。。。

示例:

public void actionPerformed(ActionEvent arg0) {
    //What i have to add here to execute genInReciept method?
    Mainclass mc = ...//get a MainClass or create a new instance like doing new Mainclass();...
    mc.genInReceipt(new Date() ,"???"); //I dont know what res is, since I can not find it in the method
    
}

假设GUIprinter主类与genInReceipt()类在同一文件中。我就是这么做的。

public class GUIPrinter extends JFrame{
public static void main(String[] args) {
    final JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton button1 = new JButton();
    final Date date = new Date();// declare Date variable
    final String yourString = "Your String here";// declare String variable
    frame.add(panel);
    panel.add(button1);
    frame.setVisible(true);
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                try {
                    genInReceipt(date,yourString);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    });
}
public static void genInReceipt(Date in, String res){
    // your code
}
}

注意:genInReceipt()类要求您传递DateString变量。声明这些变量并将其传递给genInReceipt()类。

但是如果你的getInReceipt()类在另一个文件上。按照前面给出的答案进行操作。所以总结一下如何做前面的答案。

  1. 声明genInReceipt()所在类的实例
  2. 呼叫genInReceipt()

    public class ClassThatWouldCallgenInReceipt extends JFrame {
    ClassWithgenInReceipt o = new ClassWithGenInReceipt();// Here is the class where genInReceipt() is located
    public static void main(String[] args) {
    final JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton button1 = new JButton();
    final Date date = new Date();// declare Date variable
    final String yourString = "Your String here";// declare String variable
    frame.add(panel);
    panel.add(button1);
    frame.setVisible(true);
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            o.genInReceipt(date, yourString);// Call genInReceipt() like this
        }
    });
    }
    }
    

这里是genInReceipt()类所在的ClassWithgenInReceipt

    public class ClassWithgenInReceipt{
    public void genInReceipt(Date date, String yourString){
        //your code
    }
    }

注意:我对java也相当陌生。所以我可能会在回答时出现一些错误。但是,如果有错误,请出于学习目的告诉我:)。

相关内容

  • 没有找到相关文章

最新更新