Klassen JTextField eigtl leer aber es werden Methoden ect. angezeigt

AndyFFW

Mitglied
Hallo,
durch die SuFu bin ich schonmal auf das Problem gestoßen, leider wurde das Problem dort nicht behandelt sonder nur ne stumpfe Lösung gepostet die mir aber nicht weiterhilft.

Ich wollt hier eigtl nochmal den Link zu dem Thread posten aber die SuFu hat wiedereinmal versagt und nach 5 minuten durchsuchen hab ich dann doch den Elan verloren. Die Überschrift des Threads war in etwa "Komische Zeichen"

Nichts desto Trotz hier mal n bissl Code Ausschnitt:
Java:
JTextField loginName = new JTextField("Username");
loginName.setMaximumSize(new Dimension(200, 20));
loginName.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
loginName.setForeground(Color.GRAY);
loginName.addFocusListener(new FocusHandler());
panelLogin1.add(loginName);
dazu der FocusHandler
Java:
class FocusHandler implements FocusListener
	{
                @Override
		public void focusGained(FocusEvent e)
		{
			if(e.getSource() == loginName)
			{
				loginName.setText(null);
				loginName.setForeground(Color.BLACK);
				loginName.setFont(new Font("Serif", Font.PLAIN, 13));
			}
			if(e.getSource() == loginPassword)
			{
				loginPassword.setText(null);
				loginPassword.setForeground(Color.BLACK);
				loginPassword.setFont(new Font("Serif", Font.PLAIN, 13));
			}
			
		}
		@Override
		public void focusLost(FocusEvent e)
		{
			if(e.getSource() == loginName && premiumState == 'p' && loginName.getText() == null)
			{
				loginName.setText("E-Mail");
				loginName.setForeground(Color.GRAY);
				loginName.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
			}
			else if(e.getSource() == loginName && premiumState == 'c' && loginName.getText() == null)
			{
				loginName.setText("Username");
				loginName.setForeground(Color.GRAY);
				loginName.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
			}
			
			if(e.getSource() == loginPassword && loginPassword.getPassword() == null)
			{
				loginPassword.setText("1234");
				loginPassword.setForeground(Color.GRAY);
				loginPassword.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
			}
			
			
		}
		
	}
}
Und in diesem Fall erreich ich kein If und wenn ich mir dann den text ausgeben lassen will mit
Java:
System.out.println(loginName.getText());
kommt das bei raus:
javax.swing.JTextField[,0,28,196x22,layout=javax.swing.plaf.basic.BasicTextUI$Up
dateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResour
ce$CompoundBorderUIResource@393a037e,flags=296,maximumSize=java.awt.Dimension[wi
dth=200,height=20],minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUI
Resource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=18
4,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left
=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,
b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns
=0,columnWidth=0,command=,horizontalAlignment=LEADING]

Ich bin mehr als verwirrt da ich sowas noch nicht hatte. wer weiß des rätsels lösung? danke im vorraus
 
Was für eine Variable ist denn loginName? Laut der Ausgabe scheint loginName.getText() eine Instanz vom JTextField zu liefern. Das passt aber nicht mit deinem ersten Codeschnipsel zusammen.
Kannst du mal nen bissl mehr posten?
 
Was für eine Variable ist denn loginName? Laut der Ausgabe scheint loginName.getText() eine Instanz vom JTextField zu liefern. Das passt aber nicht mit deinem ersten Codeschnipsel zusammen.
Kannst du mal nen bissl mehr posten?

loginName ist ein Objekt der Klasse JTextField und ich lasse mir den inhalt des JTextField mit der methode System.out.printl(loginName.getText()) ausgeben. mehr code gibs zu dem nicht der restliche code is für passwörter und url's ect. Ich erstelle ein Objekt loginName welches eine Instanz der Klasse JTextField ist. setze n paar attribute und im focuslistener soll er setText("E-Mail") machen wenn er den Focus verliert aber nur wenn das textfeld auch wirklich leer ist und mit getText() komm ich an den lustigen kryptischen text obwohl das textfeld leer ist
 
loginName ist ein Objekt der Klasse JTextField
Das kann nicht sein, das passt nicht mit deiner Ausgabe zusammen.

Poste mal mehr Code. Bspw. die Klasse in der der FocusHandler steht und die Klasse wo der FocusHandler instanziiert wird.
 
Das kann nicht sein, das passt nicht mit deiner Ausgabe zusammen.

Poste mal mehr Code. Bspw. die Klasse in der der FocusHandler steht und die Klasse wo der FocusHandler instanziiert wird.

Java:
package net.taess.ethyron.launcher.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Gui
{
	
	JFrame frameMain = new JFrame("Cube of Pandora Launcher für Windows");
	
	ImageIcon backgroundIcon = new ImageIcon(getClass().getClassLoader().getResource("Ressources/Background.jpg"));
	Image background = backgroundIcon.getImage();
	ImagePanel iPanelBackground = new ImagePanel(background);
	ImageIcon headlineIcon = new ImageIcon(getClass().getClassLoader().getResource("Ressources/Headline.png"));
	Image headline = headlineIcon.getImage();
	ImagePanel iPanelHead1 = new ImagePanel(headline);
	
	JPanel panelNorth = new JPanel();
	
	JPanel panelSouth = new JPanel();
	ButtonGroup mcStart = new ButtonGroup();
	JPanel panelMcStart = new JPanel();
	JRadioButton cracked = new JRadioButton("Cracked");
	JRadioButton premium = new JRadioButton("Premium");
	JPanel panelLogin1 = new JPanel();
	JPanel panelLogin2 = new JPanel();
	JPanel panelLogin = new JPanel();
	JTextField loginName = new JTextField();
	JPasswordField loginPassword = new JPasswordField();
	JButton loginButton = new JButton("Login");
	JCheckBox saveLogin = new JCheckBox("Login speichern");
	static JTextField notPrem = new JTextField();
	
	String username;
	String password;
	char premiumState = 'p';
	
	void start()
	{		
		frameMain.setSize(800, 600);								// Create Main Frame
		frameMain.setLocationRelativeTo(null);
		frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameMain.setResizable(false);
		frameMain.setLayout(new BorderLayout());
		
		iPanelBackground.setFocusable(true);
		frameMain.setContentPane(iPanelBackground);						// Set Background
		frameMain.getContentPane().setLayout(new BorderLayout());
		frameMain.setFocusable(true);
		
		iPanelHead1.setPreferredSize(new Dimension(800, 150));
		
		panelNorth.setPreferredSize(new Dimension(800, 150));
		panelNorth.setOpaque(false);
		panelNorth.add(iPanelHead1);									// Add Headline
		frameMain.getContentPane().add(BorderLayout.NORTH, panelNorth);				// Add North Panel
		
		cracked.setOpaque(false);
		cracked.addItemListener(new ItemHandler());
		premium.setOpaque(false);
		premium.setSelected(true);
		premium.addItemListener(new ItemHandler());
		mcStart.add(cracked);
		mcStart.add(premium);
		panelMcStart.setLayout(new BoxLayout(panelMcStart, BoxLayout.PAGE_AXIS));
		panelMcStart.add(cracked);
		panelMcStart.add(premium);
		panelMcStart.setOpaque(false);
		panelSouth.add(panelMcStart);
		
		loginName.setMaximumSize(new Dimension(200, 20));							// Set Login Field
		loginName.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
		loginName.setForeground(Color.GRAY);
		loginName.addFocusListener(new FocusHandler());
		loginPassword.setMaximumSize(new Dimension(200, 20));
		loginPassword.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
		loginPassword.setForeground(Color.GRAY);
		loginPassword.addFocusListener(new FocusHandler());
		saveLogin.setOpaque(false);
		panelLogin.setLayout(new BoxLayout(panelLogin, BoxLayout.PAGE_AXIS));		//Set Login Panels
		panelLogin.setOpaque(false);
		panelLogin1.setLayout(new BoxLayout(panelLogin1, BoxLayout.PAGE_AXIS));
		panelLogin1.setOpaque(false);
		notPrem.setForeground(Color.ORANGE);
		notPrem.setFont(new Font("Serif", Font.BOLD, 21));
		notPrem.setBorder(null);
		notPrem.setOpaque(false);
		notPrem.setEditable(false);
		notPrem.setFocusable(false);
		panelLogin1.add(notPrem);
		panelLogin1.add(loginName);													// Add Login Fields
		panelLogin1.add(loginPassword);
		panelLogin2.setOpaque(false);
		loginButton.addActionListener(new ActionHandler());
		panelLogin2.add(loginButton);
		panelLogin2.add(saveLogin);
		panelLogin.add(panelLogin1);												//Add Login Panels
		panelLogin.add(panelLogin2);
		panelSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
		panelSouth.setOpaque(false);
		panelSouth.add(panelLogin);
		frameMain.getContentPane().add(BorderLayout.SOUTH, panelSouth);				// Add South Panel
		
		loginName.setText("E-Mail");
		loginPassword.setText("1234");
		
		frameMain.setVisible(true);									// Set Visible
		
	}
	
	class ItemHandler implements ItemListener
	{

		@Override
		public void itemStateChanged(ItemEvent e)
		{
			if(e.getSource() == premium && premium.isSelected())
			{
				premiumState = 'p';
				loginName.setText("E-Mail");
			}
	
			if(e.getSource() == cracked && cracked.isSelected())
			{
				premiumState = 'c';
				loginName.setText("Username");
			}
		}	
	}
	
	class ActionHandler implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == loginButton && premiumState == 'p')
			{
				username = loginName.getText();
				password = String.valueOf(loginPassword.getPassword());
				//Login.hasPay(username);
				System.out.println(loginName);
				//MinecraftLaunch.launchPremium();
			}
			
			if(e.getSource() == loginButton && premiumState == 'c')
			{
				username = loginName.getText();
				MinecraftLaunch.launchCracked();
			}
		}
	}
	
	class FocusHandler implements FocusListener
	{

		@Override
		public void focusGained(FocusEvent e)
		{
			if(e.getSource() == loginName)
			{
				loginName.setText(null);
				loginName.setForeground(Color.BLACK);
				loginName.setFont(new Font("Serif", Font.PLAIN, 13));
			}
			if(e.getSource() == loginPassword)
			{
				loginPassword.setText(null);
				loginPassword.setForeground(Color.BLACK);
				loginPassword.setFont(new Font("Serif", Font.PLAIN, 13));
			}
			
		}

		@Override
		public void focusLost(FocusEvent e)
		{
			if(e.getSource() == loginName && premiumState == 'p' && loginName.getText() == null)
			{
				loginName.setText("E-Mail");
				loginName.setForeground(Color.GRAY);
				loginName.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
			}
			else if(e.getSource() == loginName && premiumState == 'c' && loginName.getText() == null)
			{
				loginName.setText("Username");
				loginName.setForeground(Color.GRAY);
				loginName.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
			}
			
			if(e.getSource() == loginPassword && loginPassword.getPassword() == null)
			{
				loginPassword.setText("1234");
				loginPassword.setForeground(Color.GRAY);
				loginPassword.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
			}
			
			
		}
		
	}
}

@SuppressWarnings("serial")											// Background Class
class ImagePanel extends JPanel
{
	
	private final Image image;
 
    public ImagePanel(Image image)
    {
    	
        this.image = image;
        setOpaque(false);
    }
 
    @Override
    public void paint(Graphics g)
    {
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        super.paint(g);
    }
}
 
Du lässt dir in Zeile 163 loginName ausgeben, daher rührt dann wohl auch deine Ausgabe.

ohja da war fail im code - aber das problem besteht weiterhin : if(e.getSource() == loginName && premiumState == 'p' && loginName.getText() == null)
{
loginName.setText("E-Mail");
loginName.setForeground(Color.GRAY);
loginName.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
}
wird nie erreicht

obwohl loginName.getText() leer ist also null

hab nochmal bissl verändert:
Java:
public void focusLost(FocusEvent e)
		{
			if(e.getSource() == loginName && premiumState == 'p' && loginName.getText() == null)
			{
				loginName.setText("E-Mail");
				loginName.setForeground(Color.GRAY);
				loginName.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
			}
			else if(e.getSource() == loginName && premiumState == 'c' && loginName.getText() == null)
			{
				loginName.setText("Username");
				loginName.setForeground(Color.GRAY);
				loginName.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 13));
			}
			else
			{
				System.out.println("Ausgabe" + loginName.getText());
			}
es wird immer das else erreicht und die ausgabe ist nur "Ausgabe"
 
obwohl loginName.getText() leer ist also null
leer ist nicht null.
Wenn das JTextField keinen inhalt hat, dann wird ein leerer String zurückgegeben:
Java:
if(e.getSource() == loginName && premiumState == 'p' && loginName.getText().equals(""))
 
leer ist nicht null.
Wenn das JTextField keinen inhalt hat, dann wird ein leerer String zurückgegeben:
Java:
if(e.getSource() == loginName && premiumState == 'p' && loginName.getText().equals(""))

ach gettext.equals - ich hatte es immer nur mit gettext oder equals versucht 😳

ok das geht jetz aber jetz weiß ich nicht wie ichs weiter lösen muss:
einmal will ich ja das im JTextField nichts gesetzt wird wenn was drinne steht, was schreib ich dann hinter equals?

und wie mach ich das beim JPasswordField weil da muss ich ja die getPassword() methode nehmen und die gibt mir ein char array zurück. geht das auch dann mit String.valueof und wenn wie wird das eingesetzt? danke
 
leer ist nicht null.
Wenn das JTextField keinen inhalt hat, dann wird ein leerer String zurückgegeben:
Java:
if(e.getSource() == loginName && premiumState == 'p' && loginName.getText().equals(""))

also das mit dem JPasswordField hab ich jetzt so gelöst:

if(e.getSource() == loginPassword && String.valueOf(loginPassword.getPassword()).equals(""))

und es geht und wenn ich die verneinung brauche müsste es ja eigtl ein == false dahinter tun. danke
 
Zuletzt bearbeitet:

Zurück
Oben