Set image at proper position in Border layout in java swing
I'm using a JDialog inside which I have added a JPanel. Layout of Jpanel is BorderLayout. Now I have some information and an image to be shown in that panel. So I have added all information in Border.Center and add image in Border.south. But the image is not properly positioned. The following may help to understand:
The image is displayed at bottom and at horizontal center. But what I want is that image should be displayed at Top-Left corder of Border.south. How can I do this? Is it possible ?
Edit:-
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.to开发者_Python百科String());
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);
}
In this code I have added a JLabel ("Image will be here.") instead of image to represent the situation.
Add a JPanel
with a FlowLayout(FlowLayout.LEFT)
to the SOUTH
. Add the image to the JPanel and it should end up in the upper left of the SOUTH
area.
If someone knows TableLayout
, it is easy to do with that.
double p = TableLayout.PREFERRED;
double f = TableLayout.FILL;
double[][] sizes = {
{p,f},
{p,p,f}
};
panel.setLayout(new TableLayout(sizes));
panel.add(textPane, "0,0, 1,0");
panel.add(jPanel, "0,1");
精彩评论