我希望边框位于文本区域周围而不是文本区域内。
/**
This frame shows a data set and its statistics.
*/
public class FileGUI extends JFrame
{
private static JPanel panel;
private JTextArea display;
private JButton open;
private JButton save;
private JButton clear;
//declare data fields here (GUI Components and other data fields)
public FileGUI()
{
//Initialize data fields and construct GUI
open = new JButton("Open file");
save = new JButton("Save to File");
clear = new JButton("Clear Display");
display = new JTextArea(30,30);
Border border = BorderFactory.createTitledBorder("File Summary");
display.setBorder(border);
panel = new JPanel();
panel.add(open);
panel.add(save);
panel.add(clear);
panel.add(display);
}
//supporting methods here.
public static void main(String[] args)
{
JFrame frame = new FileGUI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 600);
frame.setVisible(true);
frame.add(panel);
}
}
答案 0 :(得分:0)
如果希望边框包含文本字段,则必须将其包装到另一个组件(例如JPanel)中并在该组件上设置边框。边框始终在您为其分配的组件范围内呈现。
顺便说一句:您正在从主线程创建和初始化swing组件。这不是一个好方法。 Swing是一个单线程GUI,需要在事件派发线程上进行所有交互。我推荐concurrency in swing lesson