MigLayout:尝试全屏时仅显示窗口

发布于 2024-12-11 13:40:16 字数 1462 浏览 1 评论 0原文

下面是代码:

ScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height,
ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;

JFrame MainFrame = new JFrame();
MainFrame.setSize(ScreenWidth, ScreenHeight);
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
MainFrame.setVisible(true);

/* When set to false, all buttons and boxes are displayed, 
   otherwise only the main window appears */ 
MainFrame.setUndecorated(true); 

Container Pane = Frame.getContentPane();
Pane.setLayout(new MigLayout());
initLoginPanel(Pane); 

布局控件的函数:

private void initLoginPanel(Container Obj) 
{
    JPanel LoginContainer = new JPanel();
    LoginContainer.setLayout(new MigLayout());

    Obj.add(LoginContainer, "pos 0.5al 0.5al");

    JLabel  uNameLabel = new JLabel("Username");
    JTextField uNameBox = new JTextField();

    JLabel uPassLabel = new JLabel("Password");
    JTextField uPassBox = new JTextField();

    JButton LoginButton = new JButton("Login", 90, 26);
    LoginContainer.add(uNameLabel, "wrap");
    LoginContainer.add(uNameBox, "span");
    LoginContainer.add(uPassLabel, "wrap");
    LoginContainer.add(uPassBox, "span");
    LoginContainer.add(LoginButton, "");
}

如果在上面的代码中使用 MainFrame.setUndecorated(false),它可以正常工作,但不能全屏。那就是标题栏,显示关闭、最小化和最大化按钮。

问题: 1. 如何让组件在全屏模式下工作。

Here is code:

ScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height,
ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;

JFrame MainFrame = new JFrame();
MainFrame.setSize(ScreenWidth, ScreenHeight);
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
MainFrame.setVisible(true);

/* When set to false, all buttons and boxes are displayed, 
   otherwise only the main window appears */ 
MainFrame.setUndecorated(true); 

Container Pane = Frame.getContentPane();
Pane.setLayout(new MigLayout());
initLoginPanel(Pane); 

The function that lays out the controls:

private void initLoginPanel(Container Obj) 
{
    JPanel LoginContainer = new JPanel();
    LoginContainer.setLayout(new MigLayout());

    Obj.add(LoginContainer, "pos 0.5al 0.5al");

    JLabel  uNameLabel = new JLabel("Username");
    JTextField uNameBox = new JTextField();

    JLabel uPassLabel = new JLabel("Password");
    JTextField uPassBox = new JTextField();

    JButton LoginButton = new JButton("Login", 90, 26);
    LoginContainer.add(uNameLabel, "wrap");
    LoginContainer.add(uNameBox, "span");
    LoginContainer.add(uPassLabel, "wrap");
    LoginContainer.add(uPassBox, "span");
    LoginContainer.add(LoginButton, "");
}

If, in the above code, MainFrame.setUndecorated(false) is used, it works fine but no full screen. That is the title bar, close, minimize and maximize buttons are displayed.

Question:
1. How can I get the components working in fullscreen mode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2024-12-18 13:40:16

有两个问题:

  1. setVisible的位置
    显示框架必须是最后一步
    首先,您必须设置您的框架并添加他的内容。
  2. 调用initLoginPanel
    您的代码正在做不正确的事情。为什么不直接将组件添加到框架中?即

    initLoginPanel( YourJFrame );
    

固定、简化的代码:

JFrame frame = new JFrame("Main window");

frame.setSize( Toolkit.getDefaultToolkit().getScreenSize() );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(false);

initLoginPanel(frame);

frame.setVisible(true); //FINALLY show the JFrame!

There are two problems:

  1. The position of setVisible
    Showing the frame must be the last step;
    first you must setup your frame and add his content.
  2. The call to initLoginPanel
    Your code is doing incorrect things. Why don't you add the components directly to the frame? i.e.

    initLoginPanel( YourJFrame );
    

Fixed, simplified code:

JFrame frame = new JFrame("Main window");

frame.setSize( Toolkit.getDefaultToolkit().getScreenSize() );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(false);

initLoginPanel(frame);

frame.setVisible(true); //FINALLY show the JFrame!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文