Wie kann ich folgende Klasse/Methode per Button ausführen?

BlackLeakz

Neues Mitglied
Hallo liebe Community,

bin ganz neu in Java, also hab gestern meine ersten Schritte gemacht und hänge an folgender Problematik fest.
Ich hab da eine Klasse, die einen Ping-Test ausführt und möchte diese Konsolenanwendung in eine Graphische-Anwendung umwandeln.
Ich poste mal beide Codes, die ich zusammenführen möchte.

Java:
import java.io.*;
import java.net.*;
import java.util.Scanner;

class Main
{
  public static void sendPingRequest(String ipAddress)
              throws UnknownHostException, IOException
  {
    InetAddress geek = InetAddress.getByName(ipAddress);
    System.out.println("Sending Ping Request to " + ipAddress);
    if (geek.isReachable(5000))
      System.out.println("Host is reachable");
    else
      System.out.println("Sorry ! We can't reach to this host");
  }

 
  public static void main(String[] args)
          throws UnknownHostException, IOException
  {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter ip:~$ ");

    String ip = myObj.nextLine();  // Read user input
    System.out.println("Target-ip is: " + ip);  // Output user input
    sendPingRequest(ip);
  }
}

Dieser Code,soll per Buttondruck die eingegebene IP des GraphischenUserInterfaces auslesen und mit dieser eine Pingabfrage durchführen.

Java:
//#!/usr/bin/env java
//# build_production.java
// Author: BlackLeakz
// Projekt-Name: BlackLeakz-NetCmder.java
// Version: 0.0


import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class MyPanel extends JPanel implements ActionListener {

  private static final long serialVersionUID = 1L;

    private JButton setBtn;
    private JPasswordField targetipInp;
    private JPasswordField portInp;
    private JLabel labelip;
    private JLabel labelport;
    private JTextArea consoleLog;
    private JPasswordField cmdInp;
    private JButton sendBtn;
    private JMenuBar menuBar;

    public MyPanel() {
        //construct preComponents
        JMenu fileMenu = new JMenu ("File");
        JMenuItem save_logItem = new JMenuItem ("Save log");
        fileMenu.add (save_logItem);
        JMenuItem open_logItem = new JMenuItem ("Open log");
        fileMenu.add (open_logItem);
        JMenu settingsMenu = new JMenu ("Settings");
        JMenuItem load_configItem = new JMenuItem ("Load config");
        settingsMenu.add (load_configItem);
        JMenuItem save_configItem = new JMenuItem ("Save config");
        settingsMenu.add (save_configItem);
        JMenuItem updateItem = new JMenuItem ("Update");
        settingsMenu.add (updateItem);
        JMenu helpMenu = new JMenu ("Help");
        JMenuItem readmeItem = new JMenuItem ("Readme");
        helpMenu.add (readmeItem);
        JMenu aboutMenu = new JMenu ("About");
        JMenuItem contactItem = new JMenuItem ("Contact");
        aboutMenu.add (contactItem);
        JMenuItem infoItem = new JMenuItem ("Info");
        aboutMenu.add (infoItem);

        //construct components
        setBtn = new JButton ("setBtn");
        targetipInp = new JPasswordField (5);
        portInp = new JPasswordField (5);
        labelip = new JLabel ("Enter IP-Address:");
        labelport = new JLabel ("Enter Port:");
        consoleLog = new JTextArea (5, 5);
        cmdInp = new JPasswordField (5);
        sendBtn = new JButton ("sendBtn");
        menuBar = new JMenuBar();
        menuBar.add (fileMenu);
        menuBar.add (settingsMenu);
        menuBar.add (helpMenu);
        menuBar.add (aboutMenu);

        //adjust size and set layout
        setPreferredSize (new Dimension (943, 571));
        setLayout (null);


        setBtn.addActionListener(this);
        sendBtn.addActionListener(this);



        //add components
        add (setBtn);
        add (targetipInp);
        add (portInp);
        add (labelip);
        add (labelport);
        add (consoleLog);
        add (cmdInp);
        add (sendBtn);
        add (menuBar);

        //set component bounds (only needed by Absolute Positioning)
        setBtn.setBounds (220, 150, 100, 20);
        targetipInp.setBounds (55, 70, 265, 25);
        portInp.setBounds (55, 120, 265, 25);
        labelip.setBounds (55, 40, 100, 25);
        labelport.setBounds (60, 95, 100, 25);
        consoleLog.setBounds (55, 185, 845, 280);
        cmdInp.setBounds (60, 480, 695, 25);
        sendBtn.setBounds (795, 480, 100, 25);
        menuBar.setBounds (5, 0, 200, 25);



        setBtn.setActionCommand("setData");
        sendBtn.setActionCommand("sendData");

    }


    static void consoleLog() {
      System.out.println("Console<>> Program started.");
    }


    static void ping() {
      System.out.println("Console<>> This will be the Ping-Method.");
    }


    @Override
    public void actionPerformed(ActionEvent ae) {
      String action = ae.getActionCommand();
      if (action.equals("setData")) {
        System.out.println("Console<>> Set IP-Address and Port.");
      }
      String actionx = ae.getActionCommand();
      if (actionx.equals("sendData")) {
        System.out.println("Console<>> Sending Data.");
        ping();

    }
    }




    public static void main(String[] args) {
        JFrame frame = new JFrame ("MyPanel");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new MyPanel());
        frame.pack();
        frame.setVisible (true);

        consoleLog();


    }
  }
Das folgende Problem ist, dass die folgende Methode, welche in der main-Methode ausgeführt wird, warum auch immer nicht außerhalb der main-Methode ausgeführt werden kann.

Java:
 public static void main(String[] args)
          throws UnknownHostException, IOException
  {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter ip:~$ ");

    String ip = myObj.nextLine();  // Read user input
    System.out.println("Target-ip is: " + ip);  // Output user input
    sendPingRequest(ip);
  }
}

funktioniert in meinen Versuchen nicht außerhalb von :

Java:
public static void main(String[] args)
          throws UnknownHostException, IOException
  {
}
Versuche einer separaten Methode, die auf Buttondruck ausgeführt wird , führten mich ganz schnell hier her, bevor ich heute noch den unwissenden 1000 Versuch teste^^ Hab mir einiges zu Methoden und Klassen durchgelesen, für die Erleuchtung bin ich aber hier her^^

Eine schnelle Lösung wäre echt super. 😀 ...
PS: Habt ihr zufälligerweise Matrix, IRC oder etwaiges da? Quick reply wäre nice.

thx, BlackLeakz
 
Habe gerade angfangen mit der Netbeans IDE zu arbeiten, sie hatte die Lösung^^

Java:
   private void pingBtnActionPerformed(java.awt.event.ActionEvent evt) {                                       
        String ip = ipInp.getText();
        try {
            sendPingRequest(ip);
        } catch (IOException ex) {
            Logger.getLogger(BlackzApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 

Zurück
Oben