Probleme mit while-Schleife

Status
Nicht offen für weitere Antworten.

CeRbErUs2k

Mitglied
also ich benutze in meinem programm eine while schlaufe, mit der ich verschiedene optionen von einem anderen programm auflisten will. das probelm ist nur das ich diese sogenanten commandos, in einem neuen fenster ausgeben möchte, und dies leider für jedes commando ein neues fenster öffnet! wenn ich aber nur eine if abfrage daraus mache, kriege ich nur ein commando, also das erste raus. was kann ich da machen!?

Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.awt.*;
import javax.swing.*;

public class Commands {
	private String buffer = null;
public Commands(){
	try 
    { 
     Process p = Runtime.getRuntime().exec("/opt/NXOSI/bin/imgr --help"); 
     BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      
     while ((buffer = in.readLine()) != null) 
     { 
     	
     	CommandFrame();
        System.out.println(buffer); 
     } 

     in.close(); 
	   } 
	  catch (IOException e) 
	  { 
	     e.printStackTrace(); 
	  } 
}
public void CommandFrame(){
	JFrame commandframe = new JFrame();
	commandframe.getContentPane().setLayout(new BorderLayout());
	JPanel commandpanel = new JPanel();
	commandpanel.setLayout(new BorderLayout());
	
	JLabel commandlabel = new JLabel(buffer);
	
	commandframe.getContentPane().add(commandpanel, BorderLayout.CENTER);
	commandpanel.add(commandlabel);
	
	commandframe.setTitle("Commands");
    commandframe.setVisible(true);
    commandframe.pack();
    commandframe.setLocation(600, 500);
    commandframe.setSize(300,100);
	
	
}
}
 
Speicher das CommandFrame als Variable ab, dann kannst du immer wieder dasselbe Frame benutzen.

Code:
CommandFrame frame = new CommandFrame();

while( ... ){
  System.out.println(buffer); 
  frame.addCommand( buffer );  // die Methode musst du halt noch schreiben. Sie zeigt einfach ein weiteres Command an.
}

P.S. ich interpretiere dein Frage so, dass du mehrere Commands auf dem Frame anzeigen möchtest? Falls ja: mach nicht für jedes Command ein neues Panel, verwende eine Component, die mehrere Zeilen angzeigen kann. z.B. eine JList, oder ein JTextPane, oder ein JTable...
 
Ok danke du hast mir sehr geholfen. ich werde mal ein JTextPane benutzen, da ich leider nicht genau weis wie man ein JTable macht. 😀
 
Code:
import java.awt.BorderLayout;
import java.io.*;
import javax.swing.*;
public class Commands
{
	private CommandFrame frm;
	public Commands()
	{
		this.frm = new CommandFrame();
		this.showBuffer();
	}
	
	private void showBuffer()
	{
		try
		{
			Process p = Runtime.getRuntime().exec("/opt/NXOSI/bin/imgr --help");
			BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String buffer = null;
			while ((buffer = in.readLine()) != null)
			{
				this.frm.getLblBuffer().setText(this.frm.getLblBuffer().getText() + "\n"+ buffer );
				System.out.println(buffer);
			}
			in.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	public static void main(String[] args)
	{
		new Commands();
	}
}

class CommandFrame extends JFrame
{
	private JTextArea lblBuffer;
	public CommandFrame()
	{
		this.getContentPane().setLayout(new BorderLayout());
		JPanel commandpanel = new JPanel();
		commandpanel.setLayout(new BorderLayout());
		lblBuffer = new JTextArea();
		this.getContentPane().add(commandpanel, BorderLayout.CENTER);
		commandpanel.add(lblBuffer);
		this.setTitle("Commands");
		this.setVisible(true);
		this.pack();
		this.setLocation(600, 500);
		this.setSize(300, 100);
	}
	
	public JTextArea getLblBuffer()
	{
		return lblBuffer;
	}
}
 
So jetzt bin ich gerade daran ein JTable zu schreibten, doch stellt sich mir nun die Frage, wenn ich das JTable zB. in der klasse test erstelle, und dies in ein schon bestehendes frame, das in einer anderen klasse ist zB. Frame, wie kann ich das dort in ein schon bestehendes panel reinbekommen!?

Hier mein Quelltext:

Code:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.*;
import java.awt.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;




public class ListActCl extends JButton implements ActionListener {
	private JTable table = null;
	private JFrame sorryframe = null;
	private String clients = null;
	
	
	public ListActCl(){
		
		try 
	    { 
	     Process p = Runtime.getRuntime().exec("/opt/NXOSI/bin/imgr list active clients"); 
	     BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); 
	      
	     if ((clients = in.readLine()) != null) 
	     { 
	     	Table();
	        System.out.println(clients); 
	     } 
	     else if(clients == null){
	     	
	     	
	     	SorryFrame();
	     	
	     	
	     	System.out.println("no active clients");
	     	}
	     
	     in.close(); 
	   } 
	  catch (IOException e) 
	  { 
	     e.printStackTrace(); 
	  } 
	  
	}
	
	public void Table(){
		
		table = new JTable();
		//hier muss ich doch irgendwas machen, um das JTable in die andere klasse zu exportieren! aber was!?
		
	}
	
	
	
	public void SorryFrame(){
	sorryframe = new JFrame();
	 	sorryframe.getContentPane().setLayout(new BorderLayout());
	 	JPanel sorrypanel = new JPanel();
	 	sorrypanel.setLayout(new GridLayout(3,3));
	 	
	 	
	 	JLabel sorrylabel = new JLabel("ERROR:  NO ACTIVE CLIENTS");
	 	JButton sorrybutton = new JButton("OK");
	 	sorrybutton.addActionListener(this);
	 	sorrybutton.setActionCommand("OK");
	 	
	 	
	 	sorryframe.getContentPane().add(sorrypanel, BorderLayout.CENTER);
	 	
	 	
	 	sorrypanel.add(sorrylabel);
	 	sorrypanel.add(sorrybutton);
	 	
	 	
	 	sorryframe.setTitle("ERROR");
	    sorryframe.setVisible(true);
	    sorryframe.pack();
	    sorryframe.setLocation(600, 500);
	    sorryframe.setSize(300,100);
	    
	    
		}
	
		
		
	
	public void actionPerformed(ActionEvent e){
		if (e.getActionCommand() == "OK") { 
			setVisible(false);
			sorryframe.dispose(); 
	        
	}
}
	
		
	
	}
und das ist die klasse in die es reinkommen sollte:

Code:
import java.awt.*;
import javax.swing.*;
//import java.util.regex.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Frame extends JMenuBar implements ActionListener{

	
		 

	public Frame() {
		
		Version version = new Version();
		
		
		
		JFrame Frame1 = new JFrame();
		Frame1.getContentPane().setLayout(new BorderLayout());
		
		JMenuBar menubar = new JMenuBar();
		
		
		JMenu mf,mh,ml;
		JMenuItem about,close,commands,licl,liactcl,lideacl,liallcl,limo,lire,lise;
		
		
		
		mf = new JMenu("File");
		menubar.add(mf);
		close = new JMenuItem("Close");
		close.addActionListener(this);
		close.setActionCommand("exit");
		mf.add(close);
		
		ml = new JMenu("List");
		menubar.add(ml);
		licl = new JMenu("Clinets");
		ml.add(licl);
		liactcl = new JMenuItem("Active");
		liactcl.addActionListener(this);
		liactcl.setActionCommand("Active");
		licl.add(liactcl);
		lideacl = new JMenuItem("Deactivate");
		licl.add(lideacl);
		liallcl = new JMenuItem("All");
		licl.add(liallcl);
		limo = new JMenuItem("Modules");
		ml.add(limo);
		lire = new JMenuItem("Release");
		ml.add(lire);
		lise = new JMenuItem("Services");
		ml.add(lise);
		mh = new JMenu("Help");
		menubar.add(mh);
		commands = new JMenuItem("Commands");
		commands.addActionListener(this);
		commands.setActionCommand("Commands");
		mh.add(commands);
		about = new JMenuItem("About");
		about.addActionListener(this);
		about.setActionCommand("About");
		mh.add(about);
		
		Frame1.setJMenuBar(menubar);
		
		JLabel Label1 = new JLabel(" ");
	
		JPanel Pane = new JPanel();
		Pane.setLayout(new GridLayout(3,1));
//irgendwo hier sollte ja jetzt meine JTable rein! aber wie!?
		Frame1.getContentPane().add(menubar, BorderLayout.NORTH);
		Frame1.getContentPane().add(Pane, BorderLayout.CENTER);
	    
		
		
	    Frame1.setTitle(version.GetVersion());
	    Frame1.setVisible(true);
	    Frame1.pack();
	    Frame1.setLocation(400, 300);
	    Frame1.setSize(650,500);
	
	    
	    
	}

	
	public void actionPerformed(ActionEvent e) { 
	       if (e.getActionCommand() == "exit") { 
	          System.exit(0); 

			
			
				}
	       else if(e.getActionCommand() == "Commands"){
			new Commands();
		}
	       
	else if(e.getActionCommand() == "About"){
		new About();
	}
	else if(e.getActionCommand() == "Active"){
		new ListActCl();
	}
	}
		
	
	public static void main(String[]arg){
		
		new Frame();
		
	}
	}

ich hoffe ihr versteht was ich damit meine
 
Code:
public class Frame extends JMenuBar
Wenn du eine Klasse Frame erzeugen willst, solltest du auch von JFrame erben. Danach kannst du dir eine Gettermethode für den ContentPane machen und einen JTable hinzufügen.
Code:
public Container getContentPane()
{
   return this.getContentPane();
}
......

Code:
myFrame.getContentPane().add( myTable);
 
Da dein Programm aussieht wie Kraut und Rüben, hab ich es mal einwenig aufgeräumt, damit du eine vernünftige Basis hast:
Code:
package commanddemo;

import java.awt.*;
import java.awt.Container;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class Gui extends JFrame
{
	private JTextArea txtOutput;
	private Container con;
	private JButton btnCommand;
	public Gui()
	{
		this.setSize(300, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setJMenuBar( new CommandMenuBar() );
		this.con 					= this.getContentPane();
		this.txtOutput 		= new JTextArea();
		this.btnCommand   = new JButton("Execute Command");
		this.btnCommand.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
					Gui.this.showBuffer();				
			}

		});
		this.con.setLayout(new BorderLayout());
		this.con.add(this.btnCommand , BorderLayout.NORTH);
		this.con.add(this.txtOutput , BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	private void showBuffer()
	{
		 try
		 {
			Process p = Runtime.getRuntime().exec("/opt/NXOSI/bin/imgr --help");
			BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String buffer = null;
			while ((buffer = in.readLine()) != null)
			{
				txtOutput.setText(txtOutput.getText() + "\n"+ buffer );
			   System.out.println(buffer);
			}
			in.close();
		 }
		 catch (IOException e)
		 {
			e.printStackTrace();
		 }
	} 
	public static void main(String[] args)
	{
		new Gui();
	}
}

Code:
package commanddemo;

import java.awt.event.*;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.JMenuBar;

public class CommandMenuBar extends JMenuBar implements ActionListener
{
	private JMenu mniFile;
	private JMenuItem mniClose;
	public CommandMenuBar()
	{
		this.mniFile = new JMenu("File");
		this.mniClose = new JMenuItem("Close");
		
		this.mniClose.setActionCommand("cmd_close");
		this.mniClose.addActionListener(this);
		this.mniFile.add(this.mniClose);
		this.add(this.mniFile);
	}
	
	public void actionPerformed(ActionEvent e)
	{
			String s = e.getActionCommand();
			if (s.equals("cmd_close"))
			{
				System.out.println("close");
				// do something
			}
	}

}
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben