Java Swing中的JTextArea边框

4

我刚开始学习Java,正在使用Java创建UI小部件,并为此创建了以下类。但是,为了给文本区域添加边框,我知道我必须使用BorderFactory类。但由于我有单独的JFrame和JTextArea类,所以我无法做到这一点。有什么帮助吗?

类:

import javax.swing.*;
import java.awt.*;
import javax.swing.BorderFactory;

    public class UIFactory {

        //Border border = BorderFactory.createLineBorder(Color.BLACK);
        public JButton newButton(int posx, int posy, int buttonWidth, int buttonHeight) {
            JButton b = new JButton("Test");
            b.setBounds(posx, posy, buttonWidth, buttonHeight);
            return b;
        }

        public JFrame newFrame(int width, int height) {
            JFrame f = new JFrame();
            f.setSize(width, height);
            f.setLayout(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            return f;
        }

        public JTextArea newTextArea(int xpos, int ypos, int twidth, int theight) {
            JTextArea t = new JTextArea(300,300);
            JScrollPane sp = new JScrollPane(t);            
            t.setBounds(xpos, ypos, twidth, theight);
            t.setBackground(Color.orange);  
            t.setForeground(Color.black); 
         //   t.setBorder(BorderFactory.createCompoundBorder(border,BorderFactory.createEmptyBorder(10, 10, 10, 10)));            
            return t;
        }

}

以及我的主程序

import javax.swing.*;
import java.awt.*;
public class MyUI {
public static void main(String[] args) {
        UIFactory ui = new UIFactory();    
        JFrame mainf = ui.newFrame(800, 800);        
        mainf.setLocation(400, 400);

        JButton b2;
        JButton b3;


        mainf.add(b2 = ui.newButton(50, 50, 100, 50));
        mainf.add(b3 = ui.newButton(50, 100, 100, 50));   

        JTextArea area;
        mainf.add(area = ui.newTextArea(170,50,1600,300));
        mainf.setVisible(true);
        mainf.add(area = ui.newTextArea(170,400,1600,300));
        mainf.setVisible(true);
    }
}

1
避免使用“null”布局,像素完美布局在现代UI设计中是一种幻觉。有太多因素影响组件的个体大小,而你无法控制其中任何一个。Swing旨在与布局管理器一起工作,抛弃这些将导致无尽的问题和困难,你将花费越来越多的时间来尝试纠正它们。 - MadProgrammer
1
请查看 为什么在SWING中使用null布局是不被赞同的?容器内组件的布局 以获取更多详细信息。 - MadProgrammer
我的“个人”建议是让newTextArea接受Border实例作为其参数之一。 - MadProgrammer
2个回答

13

在新的文本区域(newTextArea)中尝试下面的内容

Border border = BorderFactory.createLineBorder(Color.BLACK);
    t.setBorder(BorderFactory.createCompoundBorder(border,
            BorderFactory.createEmptyBorder(10, 10, 10, 10)));

主线程异常:java.lang.Error: 未解决的编译问题:无法将Border解析为类型位于UIFactory.java的第23行,调用newTextArea方法 位于MyUi.java的第17行,调用main方法 - Rajesh
1
你好像漏掉了导入,你是用集成开发环境还是记事本写代码的? - SAQ
我使用Eclipse IDE。 - Rajesh
你是否已经添加了以下代码: import javax.swing.border.Border; import javax.swing.BorderFactory; - SAQ
我之前只添加了import javax.swing.BorderFactory;,现在添加了import javax.swing.border.Border;,它可以工作了。谢谢。 - Rajesh

2

有几种方法可以实现这个目标,你可以在事后将边框应用于框架或 JTextArea,或者根据需要向其中任一方法提供 Border 值。

我的偏好是考虑使用建造者模式,它允许你提供感兴趣的属性并制作最终结果。

由于许多属性在组件之间共享,我倾向于从抽象实现开始。

public abstract class ComponentBuilder<B extends ComponentBuilder<B, T>, T extends JComponent> {

    public static final String BORDER = "border";
    public static final String FOREGROUND = "foreground";
    public static final String BACKGROUND = "background";

    private Map<String, Object> properties = new HashMap<>();

    protected abstract B self();

    protected void put(String key, Object value) {
        properties.put(key, value);
    }

    public B withBorder(Border border) {
        put(BORDER, border);
        return self();
    }

    public B withForeground(Color color) {
        put(FOREGROUND, color);
        return self();
    }

    public B withBackground(Color color) {
        put(BACKGROUND, color);
        return self();
    }

    public abstract T build();

    public <O> O get(String key, Class<O> type, O defaultValue) {
        Object value = properties.get(key);
        if (value == null) {
            return defaultValue;
        } else if (value.getClass().isAssignableFrom(type)) {
            return (O)value;
        }
        return defaultValue;
    }

    protected Border getBorder() {
        return get(BORDER, Border.class, null);
    }

    protected int getInt(String key, int defaultValue) {
        return get(key, int.class, defaultValue);
    }

    protected Color getColor(String key, Color defaultValue) {
        return get(key, Color.class, defaultValue);
    }

    protected Color getForeground() {
        return getColor(FOREGROUND, null);
    }

    protected Color getBackground() {
        return getColor(BACKGROUND, null);
    }
}

好的,不要惊慌,这是一些很棒的通用技巧,但相信我,它使整个API非常灵活。

现在,您可以包括更多属性,例如字体,但让我们使用基本示例。

接下来,我们需要一个文本区域构建器,以我们想要的方式构建文本区域。

public class TextAreaBuilder extends ComponentBuilder<TextAreaBuilder, JTextArea> {

    public static final String ROWS = "rows";
    public static final String COLUMNS = "columns";

    @Override
    protected TextAreaBuilder self() {
        return this;
    }

    public TextAreaBuilder withRows(int rows) {
        put(ROWS, rows);
        return self();
    }

    public TextAreaBuilder withColumns(int cols) {
        put(COLUMNS, cols);
        return self();
    }

    protected int getRows(int defaultValue) {
        return getInt(ROWS, defaultValue);
    }

    protected int getColumns(int defaultValue) {
        return getInt(COLUMNS, defaultValue);
    }

    @Override
    public JTextArea build() {
        JTextArea ta = new JTextArea();
        ta.setColumns(getColumns(0));
        ta.setRows(getRows(0));
        ta.setBorder(getBorder());
        ta.setForeground(getForeground());
        ta.setBackground(getBackground());
        return ta;
    }

}

然后我们可以使用想要使用的属性轻松地创建一个新的JTextArea...
JTextArea ta = new TextAreaBuilder().
        withColumns(40).
        withRows(20).
        withBackground(Color.ORANGE).
        withForeground(Color.BLACK).
        withBorder(BorderFactory.createLineBorder(Color.RED)).
        build();

完成了!

现在,如果所有这些似乎“太难了”,您可以简单地将当前方法更改为需要一个Border的实例,例如

public JTextArea newTextArea(int rows, int cols, Border border) {
    JTextArea ta = new JTextArea(rows, cols);
    ta.setBorder(border);
    return ta;
}

避免使用null布局,因为在现代UI设计中,像素完美的布局是一种幻觉。有太多因素会影响组件的大小,这些因素都无法控制。Swing是与布局管理器紧密配合设计的,抛弃它们将导致无数问题和困难,你将花费越来越多的时间来尝试解决。
请查看为什么在SWING中使用null布局不受欢迎?容器内部组件的布局以获取更多详细信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接