我正在使用一个 JDialog,其中添加了一个 JPanel。Jpanel的布局是BorderLayout。现在我有一些信息和一张要在该面板中显示的图像。所以我在 Border.Center 中添加了所有信息,并在 Border.south 中添加了图像。但是图像没有正确定位。以下内容可能有助于理解:
图像显示在底部和水平中心。但我想要的是该图像应显示在 Border.south 的左上角。我怎样才能做到这一点?可能吗 ?
编辑:-
public static void main(String[] args) {
JDialog dialog = new JDialog();
dialog.setSize(350, 350);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JScrollPane scroll = new JScrollPane(panel);
JEditorPane textPane = new JEditorPane();
textPane.setContentType("text/html");
StringBuffer buffer = new StringBuffer();
buffer.append(String.format("<div><b>Name:</b>%s</div>", "Harry"));
buffer.append(String.format("<div><b>Id:</b>%s</div>", "Joy"));
textPane.setText(buffer.toString());
panel.setBackground(Color.white);
panel.add(textPane,BorderLayout.CENTER);
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JLabel lbl = new JLabel("Image will be here.");
lbl.setFont(new Font(Font.SANS_SERIF,0,40));
jPanel.add(lbl);
panel.add(jPanel,BorderLayout.SOUTH);
dialog.add(scroll);
dialog.setVisible(true);
}
在这段代码中,我添加了一个 JLabel(“图像将在这里。”)而不是图像来表示这种情况。