0

我的查询可能很简单,但它让我陷入了困境。我正在 Windows 8 pc 上使用 netbeans7.4 和 java 开发软件,我有一个 MainForm 全屏显示,仅显示 MenuBar(顶部)和标签(用于背景图像)。我使用以下代码使其成为用户的屏幕大小

this.setExtendedState(Main_Form.MAXIMIZED_BOTH);

现在我想添加一个状态栏,它应该出现在窗口的底部,并且每次在不同尺寸的显示器上运行时都应该识别屏幕底部的位置。

4

2 回答 2

1

使用 a BorderLayout,将组件添加到SOUTH位置,这将自动水平调整大小。将您的标签保持在CENTER原位

查看如何使用 BorderLayout了解更多详情

地位

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

}
于 2014-02-03T10:38:17.393 回答
0

您可以使用以下命令使元素的宽度等于客户端监视器的宽度:yourelement.Width=ClientRectangle.Width;

于 2014-02-02T19:59:41.003 回答