有人可以告诉我为什么我的文字消失以及如何解决吗?

时间:2018-10-22 18:29:52

标签: java swing

如您所见,我创建了一个名为JLabeledComponentField的对象类,并将其与其他相同的对象放入JPanel中。 这是代码示例

class JLabeledComponentField extends JPanel{
private JLabel jLabel;
private JComponent comp;
private boolean editable;

public JLabeledComponentField(JComponent Comp) {
    jLabel = new JLabel();
    jLabel.setBorder(BorderFactory.createLineBorder(new Color(122,138,153)));
    jLabel.setOpaque(true);
    jLabel.setBackground(Color.white);
    jLabel.setHorizontalAlignment(SwingConstants.CENTER);

    onClicked_Label(jLabel);
    comp = Comp;
    if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
    {
        ((JTextField)comp).setUI(new MetalTextFieldUI());
        ((JTextField)comp).setBorder(BorderFactory.createLineBorder(new Color(122,138,153)));
    }
    editable = true;
    onExit_TextField();
    setLayout(new BorderLayout());
    add(jLabel,BorderLayout.CENTER);
}
private void onExit_TextField() {
    comp.addFocusListener(new FocusListener(){
        public void focusGained(FocusEvent arg0) {
        }
        public void focusLost(FocusEvent arg0) {
            set();
            remove(comp);//remove component
            add(jLabel, BorderLayout.CENTER);//
            //refresh JFrame
            revalidate();
            repaint();
        }
    });
}

private void onClicked_Label(JLabel lbl) {
    lbl.addMouseListener(new MouseListener(){
        public void mouseClicked(MouseEvent mouseEvent)
        {
            if(editable)
            {
                remove(jLabel);//remove component
                add(comp, BorderLayout.CENTER);//
                comp.requestFocus();

                //refresh JFrame
                revalidate();
                repaint();
            }
        }
        public void mouseEntered(MouseEvent mouseEvent) {}
        public void mouseExited(MouseEvent mouseEvent) {}
        public void mousePressed(MouseEvent mouseEvent){}
        public void mouseReleased(MouseEvent mouseEvent){}
    });
}
private void set()
{
    if(comp.getClass().toString().equals("class javax.swing.JComboBox"))
    {
        String text = ((JComboBox)comp).getSelectedItem().toString();
        jLabel.setText(text);
    }
    if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
    {
        String text = ((JTextField)comp).getText();
        jLabel.setText(text);
    }
}
public void setColor(String text)
{
    if(text.equals(""))
        changeColor();
    else
        returnColor();
}
public void changeColor()
{
    jLabel.setBackground(Color.yellow);
    comp.setBackground(Color.yellow);
}
public void returnColor()
{
    jLabel.setBackground(Color.gray);
    comp.setBackground(Color.gray);
}
public void setSelectedItem(String text)throws Exception
{
    if(comp.getClass().toString().equals("class javax.swing.JComboBox"))
    {
        jLabel.setText(text);
        ((JComboBox)comp).setSelectedItem(text);
    }
    else
        throw new Exception("Exception occered : it's not JComboBox, its "+comp.getClass().toString());
}
public void setText(String text)throws Exception
{
    if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
    {
        jLabel.setText(text);
        ((JTextField)comp).setText(text);
        jLabel.setToolTipText(text);
        ((JTextField)comp).setToolTipText(text);

    }
    else
        throw new Exception("Exception occered : it's not JTextField, its "+comp.getClass().toString());
}
public String getSelectedItem()throws Exception
{
    if(comp.getClass().toString().equals("class javax.swing.JComboBox"))
        return ((JComboBox)comp).getSelectedItem().toString();
    else
        throw new Exception("Exception occered : it's not JComboBox, its "+comp.getClass().toString());
}
public String getText()throws Exception
{
    if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
        return jLabel.getText();
    else
        throw new Exception("Exception occered : it's not JTextField, its "+comp.getClass().toString());
}
public void setEditable(boolean bool)throws Exception
{
    if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class javax.swing.JComboBox"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
    {
        editable = bool;
        SetEnabled(bool);
    }
    else
        throw new Exception("Exception occered : it's not selected component, its "+comp.getClass().toString());
}
public void SetEnabled(boolean bool)throws Exception
{
    if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class javax.swing.JComboBox"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
    {
        jLabel.setEnabled(bool);
    }
    else
        throw new Exception("Exception occered : it's not selected component, its "+comp.getClass().toString());
}
public void SetFocus()
{
        if(editable)
        {
            remove(jLabel);//remove component
            add(comp, BorderLayout.CENTER);//
            comp.requestFocus();

            //refresh JFrame
            revalidate();
            repaint();
        }
}}

就像我看到代码后看到的那样,我将其调用并将对象放在jpanel中。

JLabeledComponentField name = new JLabeledComponentField(new JTextfield());
JP.add(name);

它工作正常且非常干净,但是... 然后,当我双击该组件并用JLabel替换JTextfield时,该文本在大约50%的情况下以某种方式消失了,这种情况并没有发生,看起来它进入了某种区域,我们仍然可以看到JTextfield但没有文本。 我需要专家意见,可以建议我该怎么做。 有任何想法吗?

0 个答案:

没有答案