-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

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()を渡す必要があります。これらの変数を宣言し、クラスに渡します。DateStringgenInReceipt()

ただし、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
        }
    });
    
    }
    }
    

そして、ここClassWithgenInReceiptgenInReceipt()クラスがあります

    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 に答える
0

クラスのインスタンスを作成して から、次のgenInReceiptように呼び出す必要があります。

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

しかし、最初にJavaとプログラミング自体についてのベースを読み始めるのは素晴らしいことです.

于 2016-07-23T11:47:51.973 に答える