-2

我很难找到一种方法来在我的主类中执行打印方法,从另一个类中使用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("%s\n", "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("%s\n","    1343KZT/MOBIL"));// +  new String(new byte[] {ESC, 'E', 0x1B, GS, '!', 0x10, 0x01}) + String.format("        MOBIL"));
        serialPort.writeString(String.format("%s\n", "    PP6-ADE SILFIANAH"));
        serialPort.writeString(String.format("    In      : %s\n", df.format(in)));
        serialPort.writeString(String.format("    Out     : %s\n", "21 Jul 2016  17:00:00"));
        serialPort.writeString(String.format("    Duration: %s\n", "1 hours 29 minutes")); //Pakegate
        serialPort.writeString(new String(new byte[] {ESC, 'J', 0x4A}));
        serialPort.writeString(String.format("    Sewa Parkir: %s\n", "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 KASIH\n".getBytes()));
        serialPort.writeString(new String("ATAS KUNJUNGAN ANDA\n".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?
            }
        });

    }

}

我知道我的代码有问题,有人可以帮助我吗?

4

3 回答 3

0

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

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

但首先开始阅读有关 Java 和编程本身的贝司会很棒。

于 2016-07-23T11:47:51.973 回答
0

假设您的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
        }
    });
    
    }
    }
    

这是班级所在的ClassWithgenInReceipt地方genInReceipt()

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

注意:我对 java 也很陌生。所以我可能会用一些错误来回答这个问题。但是,如果有错误,请告诉我以供学习:)。

于 2016-07-23T14:04:44.737 回答
0

你需要/想要的很简单,你需要一个类的实例并调用方法......

例子:

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
    
}
于 2016-07-23T12:00:13.990 回答