【问题标题】:Remove label before I click in java在我点击java之前删除标签
【发布时间】:2022-01-16 08:57:18
【问题描述】:

我有关于删除空字符串值的问题,就像我们在图片中看到的那样, 第一次如果这里是空的,他会给出一个错误,但在那之后即使我们在那个空白处写了一些字符串,它仍然给出同样的错误我如何在再次发送之前删除这个标签我如何修复这个问题我尝试了一些代码,但没有任何效果,请帮助解决这个问题

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;


public class ui {

    public static void main(String[] args) {
        uiVision();
    }
    public static void uiVision() {
        ImageIcon eyes = new ImageIcon("a.png");
        Globals.jf.setTitle("Deneme Uygulamasi");
        Globals.jf.setLocation(100,200);
        JLabel label1,label2,label3;
        Globals.jf.getContentPane().setLayout(new FlowLayout());
        JTextField isim = new JTextField(20);
        JTextField soyisim = new JTextField(20);
        JTextField pasaport = new JTextField(20);
        JTextField mail = new JTextField(20);
        JPasswordField passwordField = new JPasswordField(10);
        JPasswordField passwordField2 = new JPasswordField(10);



        JButton buton1 = new JButton("Send");
        JButton buton2 = new JButton(eyes);
        JButton buton3 = new JButton(eyes);
        JButton buton4 = new JButton("!");

        label1 = new JLabel("Name:");// -8
        label2 = new JLabel("Surname:");// -9
        label3 = new JLabel("Passaport-ID:");//+ 10
        JLabel label4 = new JLabel("Mail:");// +10
        JLabel label5 = new JLabel("Password:");//+10
        JLabel label6 = new JLabel("Re-Password:");// +20

        buton1.setBounds(170,400,150,30);
        buton2.setBounds(320,190,50,30);
        buton3.setBounds(320,230,50,30);
        buton4.setBounds(370,230,50,30);

        isim.setBounds(170,30,150,30);
        soyisim.setBounds(170,70,150,30);
        pasaport.setBounds(170,110,150,30);
        mail.setBounds(170,150,150,30);
        passwordField.setBounds(170,190,150,30);
        passwordField2.setBounds(170,230,150,30);



        label1.setBounds(125,30,150,30);
        label2.setBounds(106,70,150,30);
        label3.setBounds(90,110,150,30);
        label4.setBounds(132,150,150,30);
        label5.setBounds(105,190,150,30);
        label6.setBounds(91,230,150,30);

        Globals.jf.add(buton1);Globals.jf.add(buton2);Globals.jf.add(buton3);
        Globals.jf.add(label1);Globals.jf.add(label2);Globals.jf.add(label3);Globals.jf.add(label4); Globals.jf.add(label5);Globals.jf.add(label6);
        Globals.jf.add(isim);Globals.jf.add(soyisim);Globals.jf.add(pasaport);Globals.jf.add(mail);Globals.jf.add(passwordField);Globals.jf.add(passwordField2);
        Globals.jf.setSize(1000,500);



        buton2.addActionListener(l -> {
            if ( passwordField.getEchoChar() != '\u0000' ) {
                passwordField.setEchoChar('\u0000');
            } else {
                passwordField.setEchoChar((Character) UIManager.get("PasswordField.echoChar"));
            }
        });

        buton3.addActionListener(l -> {
            if ( passwordField2.getEchoChar() != '\u0000' ) {
                passwordField2.setEchoChar('\u0000');

            } else {
                passwordField2.setEchoChar((Character) UIManager.get("PasswordField.echoChar"));
            }
        });
        buton1.addActionListener(e -> {
            checkEmpty(isim.getText(),label1.getText(),label1);
            checkEmpty(soyisim.getText(),label2.getText(),label2);
            checkEmpty(pasaport.getText(),label3.getText(),label3);
            checkEmpty(mail.getText(),label4.getText(),label4);
            ExitWhenLoopEnd();
            Globals.globalInt = 0;
            System.out.println(passwordField.getPassword());
            System.out.println(passwordField2.getPassword());
            Globals.clickCount++;

        });
        Globals.jf.setLayout(null);
        Globals.jf.setVisible(true);
        Globals.jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void checkEmpty(String value,String label,JLabel labelname) {
        Integer syc = Integer.valueOf(0);

        if(value != null && !value.trim().isEmpty()) {
            if(Globals.globalInt != 4) {
                Globals.globalInt++;
            }
            syc = 1;
        }
        else {
            
            CreateEmptyMessageError(label,labelname,Globals.jf);
            syc = -1;
        }


        System.out.println(syc);

    }

    public static void CreateEmptyMessageError(String labelError,JLabel label,JFrame jf) {
        Globals.labelx = new JLabel(labelError.split(":")[0]+" is empty!");
        Globals.labelx.setBounds(label.getBounds().x+250,label.getBounds().y,label.getWidth(),label.getHeight());
        Globals.labelx.setForeground(Color.RED);
        jf.add(Globals.labelx);
        jf.revalidate();
        jf.repaint();

    }
    public class Globals {
        public static int globalInt = 0;
        public static JLabel labelx = null;
        public static JFrame jf = new JFrame();
        public static int clickCount = 0;
        public static int lastVal = 0;
        public static int syc = 0;


    }

    public static void ExitWhenLoopEnd() {
        if(Globals.globalInt == 4)  {
            System.exit(0);
        }
    }
}

【问题讨论】:

  • 通常不建议使用全局变量。除非您知道自己在做什么,否则尽量不要使用 static 字段。
  • 另外,这个Globals.jf.setLayout(null); 和所有那些.setBounds 都不是一个好主意。另外,给你的变量起更好的名字,描述性的名字,让你的代码自我注释。
  • @HovercraftFullOfEels .setBounds 函数可以使用什么
  • 您需要使用布局管理器。您可以在此处找到布局管理器教程:Layout Manager Tutorial,您可以在此处找到 Swing 教程和其他 Swing 资源的链接:Swing Info

标签: java swing jlabel


【解决方案1】:

您的问题是,每次调用 CreateEmptyMessageError(...) 时,您都在创建一个新的 JLabel 并将其添加到 GUI 中,通过这样做,您将无法引用该对象,也无法更改其状态。

解决方案是不这样做,而是在创建 GUI 本身时创建错误消息标签,将其分配给实例字段,并在上述方法中不创建新的 JLabel 对象,而是设置文本现有对象,如果 JTextField 为空则显示警告,如果 JTextField 有文本,则将 JLabel 文本设置为空字符串 ""

还有,

  • 正如 Progman 在 cmets 中建议的那样,避免使用静态字段和方法,除非使用建议应该使用它们,而这里不是这种情况。相反,请使用私有 instance 字段和方法。这将使您的代码更容易模拟/测试/扩展和重用,从而通过降低代码的圈复杂度和耦合度来减少难以识别的错误的可能性。
  • 避免使用空布局和setBounds(...),而是学习和使用布局管理器。
  • 学习和使用Java naming conventions。变量名应全部以小写字母开头,而类名应以大写字母开头。学习并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解他人的代码。
  • 为您的字段提供描述它们所代表内容的名称,使您的代码能够自我注释并更易于理解。

【讨论】:

    猜你喜欢
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 2013-12-01
    相关资源
    最近更新 更多