我是
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);
}
}