Reputation: 11
My query might be so simple but it made me stuck badly. I'm developing a software using netbeans7.4 and java on windows 8 pc, I have a MainForm which appears on full screen and only shows MenuBar(on top) and label (for background image). I have used the following code to make it strach as screen size of the user
this.setExtendedState(Main_Form.MAXIMIZED_BOTH);
now i want to add a status bar which should appear at the bottom of the windows and should identify the location of bottom of the screen itself every time it runs on different size monitor.
Upvotes: 0
Views: 2052
Reputation: 347314
Use a BorderLayout
, add a component to the SOUTH
position, this will be resized horizontally automatically. Keep your label in the CENTER
position
Check out How to use BorderLayout for more details
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class StatusBarExample {
public static void main(String[] args) {
new StatusBarExample();
}
public StatusBarExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
statusBar.setBorder(
new CompoundBorder(
new LineBorder(Color.DARK_GRAY),
new EmptyBorder(4, 4, 4, 4)));
final JLabel status = new JLabel();
statusBar.add(status);
JLabel content = new JLabel("Content in the middle");
content.setHorizontalAlignment(JLabel.CENTER);
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(content);
frame.add(statusBar, BorderLayout.SOUTH);
frame.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
status.setText(frame.getWidth() + "x" + frame.getHeight());
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Upvotes: 1
Reputation: 1
You can make the width of your element equal to width of Clients monitor with the command: yourelement.Width=ClientRectangle.Width;
Upvotes: 0