62,621
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}
}