我是java新手并使用java创建UI小部件,并为此创建了以下类。但是为了向textarea添加边框,我知道我必须使用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);
}
}
答案 0 :(得分:8)
在newTextArea中尝试以下
Border border = BorderFactory.createLineBorder(Color.BLACK);
t.setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
答案 1 :(得分: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非常灵活
现在,您可以包含更多属性,例如字体,但让我们坚持使用基本示例。
接下来,我们需要一个文本区域构建器来按照我们想要的方式构建textarea
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旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正
有关详细信息,请查看Why is it frowned upon to use a null layout in SWING?和Laying Out Components Within a Container