Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Konsole auf Textarea die zweite

Empfohlene Antworten

Veröffentlicht

Hallo, ich habe immer noch probleme die Konsolenausgabe auf eine Textarea umzuleiten. Ich ahbe folgenden Quellcode:



public ConsoleTest() {

		frame = new JFrame("Console");


		JPanel consolePanel = new JPanel();

		consolePanel.setLayout(new BorderLayout());


		consoleTextArea = new JTextArea();

		consoleTextArea.setEditable(false);


		pane = new JScrollPane(consoleTextArea);

		consolePanel.add(pane, BorderLayout.CENTER);


		buttonPanel = new JPanel();

		FlowLayout jPanel1Layout = new FlowLayout();

		consolePanel.add(buttonPanel, BorderLayout.SOUTH);

		buttonPanel.setLayout(jPanel1Layout);


		openConsolButton = new JButton();

		buttonPanel.add(openConsolButton);

		openConsolButton.setText("Open console");

		openConsolButton.setActionCommand("open");

		openConsolButton.addActionListener(this);

		openConsolButton.setPreferredSize(new java.awt.Dimension(98, 21));


		closeButton = new JButton();

		buttonPanel.add(closeButton);

		closeButton.setText("Close");

		closeButton.setActionCommand("close");

		closeButton.addActionListener(this);

		closeButton.setPreferredSize(new java.awt.Dimension(98, 21));


		frame.getContentPane().add(consolePanel);


		frame.setSize(700, 900);

		frame.setVisible(true);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


	}




		public void actionPerformed(ActionEvent evt) {

			String cmd = evt.getActionCommand();


			if (cmd.equals("close")) {

				int wahl = JOptionPane.showConfirmDialog(frame,

						"Do you really want to close?", "EXIT",

						JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);


				if (wahl == JOptionPane.YES_OPTION)

					frame.dispose();

			}


			if (cmd.equals("open")) {



				PrintStream p = new PrintStream(

						  new OutputStream() {

						    public void write( int b ) {

						      consoleTextArea.append( (char)b );

						    }

						  }

						);


				System.setErr(p);

				System.setOut(p);

			}

			}


		public static void main(String[] arg) throws IOException

	{

		new ConsoleTest(); // create console with not reference	

	}			




		}


Sollte eigentlich , denke ich?!, funktionieren?? Macht es aber leider nicht. Ich bekomme einen Fehlermeldung wenn ich die Sachen an den Textarea anhängen will.

 consoleTextArea.append( (char)b );


Er meckert über das append. Fehlermeldung. The method append (String)in the type JTextArea is not applicable for the argument (char).

Wie mache ich das denn richtig?

Vielen Dank

Stefan

Indem Du Dir die Fehlermeldung durchliest, eine API Dokumentation zur Hand nimmst, und die Dokumentation der entsprechenden Methode liest.

Die Methode append der JTextArea erwartet als Parameter einen String, keinen char. Also sorg dafür, dass Du Deinen int-Wert in einen String bekommst (die Klasse String und deren Dokumentation helfen Dir hier weiter) und hänge den an die JTextArea.

Danach kannst Du Dir die API Dokumentation gleich noch bookmarken, die brauchst Du öfter.

Peter

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.