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

时间:2016-07-23 11:42:52

标签: java user-interface methods mainclass

我在找到一种方法来执行一种在我的主类中执行打印的方法时遇到问题,来自另一个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?
            }
        });

    }

}

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

3 个答案:

答案 0 :(得分:0)

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

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

但首先要开始阅读有关java和编程本身的基础知识。

答案 1 :(得分:0)

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

实施例

    plotter.AddLineGraph(ds, Colors.Green, 2, "Volts");

答案 2 :(得分: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
        }
    });
    
    }
    }
    
  3. 以下是ClassWithgenInReceipt genInReceipt()班级所在的地方

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

    注意:我对java也很新。所以我可能会回答一些错误。但是,如果有错误,请告诉我学习目的:)。