swing JTextField 中文输入法不兼容

ldaokun2006 2018-03-19 05:26:10
用java写了一个swing程序,但是发现打成jar包后,中文输入法不兼容

...全文
819 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
linyuanshan 2018-03-23
  • 打赏
  • 举报
回复
楼主,代码稍微加注释
ldaokun2006 2018-03-20
  • 打赏
  • 举报
回复
这个问题解决了,有的时候人生充满了戏剧性。 测试发现启动时jdk1.8u71就好用,使用jdk1.8u151和最新的jdk1.8u161都有问题,具体产生原因不清楚
ldaokun2006 2018-03-20
  • 打赏
  • 举报
回复
下面是这个Input框的包装代码,没有你说的那样设置了只能输入字母和数字

package com.my.controller.component;

import com.my.constants.ConstantsUI;

import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;

public class MyTextComponent extends BaseInputText<String> {
    public MyTextComponent(String label){
        super(label);
        textField=new JTextField();
        add(textField);
        textField.setPreferredSize(ConstantsUI.SearchPanel.SEARCH_INPUT_TEXT_SIZE);
    }

    public MyTextComponent(String labelText, ComponentSize componentSize) {
        this(labelText);
        setComponentSize(componentSize);
    }

    public synchronized void addTextMouseListener(MouseListener l) {
        textField.addMouseListener(l);
    }

    public void addKeyboardListener(KeyListener l){
        textField.addKeyListener(l);
    }

    @Override
    public String getData() {
        return textField.getText();
    }

    @Override
    public void setData(String data) {
        this.textField.setText(data);
    }

    @Override
    public void resetData() {
        this.textField.setText("");
    }

    protected JTextField textField;

    public JLabel getLabel(){
        return super.label;
    }
}
package com.my.controller.component;

import com.my.constants.ConstantsUI;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.List;

@Slf4j
@Getter
public abstract class BaseInputText<T> extends JPanel implements Serializable{
    protected JLabel label;
    protected BaseInputText(String labelText){
        setLayout(new FlowLayout());
        setBackground(ConstantsUI.MAIN_BACK_COLOR);
        this.label=new JLabel(labelText);
        this.label.setFont(ConstantsUI.FONT_NORMAL);
        this.setBackground(ConstantsUI.MAIN_BACK_COLOR);
        add(label);
    }

    protected void setComponentSize(ComponentSize componentSize){
        setLayout(null);
        setPreferredSize(componentSize.getPanelSize());
        Component[] components = this.getComponents();
        List<Rectangle> componentSize1 = componentSize.getComponentSize();
        if(components.length!= componentSize1.size()){
            throw new RuntimeException("The number of components and the length of the component are not equal");
        }
        for(int i = 0; i< componentSize1.size(); i++){
            components[i].setBounds(componentSize1.get(i));
        }
    }
    public abstract T getData();
    public abstract void setData(T data);
    public abstract void resetData();
    public BaseInputText clone(){
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bais = null ;
        ObjectInputStream ois = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(this);
            // 将流序列化成对象
            bais = new ByteArrayInputStream(baos.toByteArray());
            ois = new ObjectInputStream(bais);
            return (BaseInputText) ois.readObject();
        }catch (Exception e){
            throw new RuntimeException(e.getMessage(),e);
        }finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (oos != null) {
                    oos.close();
                }
                if (bais != null) {
                    bais.close();
                }
                if (ois != null) {
                    ois.close();
                }
            }catch (Exception e){
                throw new RuntimeException(e.getMessage(),e);
            }
        }
    }
}
kuangbao9 2018-03-20
  • 打赏
  • 举报
回复
你是不是限制只能输入字母或数字了?
ldaokun2006 2018-03-19
  • 打赏
  • 举报
回复
使用的是 java -jar [jar包名] 启动的程序

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧