【问题标题】:How do I position my title on top of the GUI window?如何将我的标题放在 GUI 窗口的顶部?
【发布时间】:2020-09-11 22:24:21
【问题描述】:

我是编程新手,还在学习。我需要帮助在 GUI 界面顶部定位 JLabel“欢迎回来”。尽管我努力,但标签仍然卡在中间。随意更正代码中显示的任何其他错误,我可以做得更好。

public class MainMenu {

public static void main(String[] args) {
    JFrame frame = new JFrame("Main Menu");

    JPanel panel = new JPanel();
    panel.setBounds(30, 80, 400, 570);
    panel.setLayout(new GridLayout(3, 2, 15, 15));
    panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

    JButton meals = new JButton("Meals");  
    meals.setFont(new Font("Helvetica", Font.BOLD, 24));
    meals.setOpaque(true);

    JButton reminder = new JButton ("Reminders");       
    reminder.setFont(new Font("Helvetica", Font.BOLD, 24));
    reminder.setOpaque(true);

    JButton shop = new JButton ("Shop");        
    shop.setFont(new Font("Helvetica", Font.BOLD, 24));
    shop.setOpaque(true);

    JButton sleep = new JButton ("Sleep Timer");       
    sleep.setFont(new Font("Helvetica", Font.BOLD, 24));
    sleep.setOpaque(true);

    JButton account = new JButton ("My Account");        
    account.setFont(new Font("Helvetica", Font.BOLD, 24));
    account.setOpaque(true);

    JButton aboutus = new JButton ("About Us");
    aboutus.setFont(new Font("Helvetica", Font.BOLD, 24));
    aboutus.setOpaque(true);

    JLabel label = new JLabel("Welcome back!");
    label.setLocation(240, 20);

    panel.add(meals);
    panel.add(reminder);
    panel.add(shop);
    panel.add(sleep);
    panel.add(account);
    panel.add(aboutus);

    frame.add(panel);
    frame.add(label);

    frame.setSize(480, 720);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

【问题讨论】:

  • 一般 cmets: 1) panel.setBounds(30, 80, 400, 570); 这都是没有意义的,如果不是这样,那将适得其反,因为这只是一个猜测。相反,添加具有适当插图、边框和填充的组件,然后 pack() 框架将其设置为正确的大小。 2) new Font("Helvetica", Font.BOLD, 24) a) 不要在布局示例中包含自定义字体。但是当你到了使用字体的阶段.. b) 在没有先检查它是否存在的情况下不要使用特定的字体系列(这台 Windows 机器上没有 Helvetica)- 最好使用逻辑字体..
  • ..(例如SANS_SERIF),使用应用程序提供和加载字体。或者检查它是否存在,如果不存在,使用逻辑字体作为备份,并且.. c)只创建字体一次。 3) 源代码中的一个空白行是永远需要的。 { 之后或} 之前的空行通常也是多余的。 4)label.setLocation(240, 20); 再次,毫无意义。使用布局和约束来定位它,或者在这种情况下,只需将TitledBorder 用于带有GridLayout 的面板。
  • 顺便说一句 - this 这样的东西是否符合要求?

标签: java swing user-interface jpanel jlabel


【解决方案1】:

面板在中间布局管理器(边框)上重新排列
基于Layout-used也有各种解决方案,但主旨是一样的

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainMenu {

public static void main(String[] args) {

    JFrame frame = new JFrame("Main Menu");
    //define a new layout
    frame.setLayout(new BorderLayout());
    //panel for label 
    JPanel topPanel = new JPanel();
    JPanel panel = new JPanel();
    panel.setBounds(30, 80, 400, 570);
    panel.setLayout(new GridLayout(3, 2, 15, 15));
    panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

    JButton meals = new JButton("Meals");  
    meals.setFont(new Font("Helvetica", Font.BOLD, 24));
    meals.setOpaque(true);

    JButton reminder = new JButton ("Reminders");       
    reminder.setFont(new Font("Helvetica", Font.BOLD, 24));
    reminder.setOpaque(true);

    JButton shop = new JButton ("Shop");        
    shop.setFont(new Font("Helvetica", Font.BOLD, 24));
    shop.setOpaque(true);

    JButton sleep = new JButton ("Sleep Timer");       
    sleep.setFont(new Font("Helvetica", Font.BOLD, 24));
    sleep.setOpaque(true);

    JButton account = new JButton ("My Account");        
    account.setFont(new Font("Helvetica", Font.BOLD, 24));
    account.setOpaque(true);

    JButton aboutus = new JButton ("About Us");
    aboutus.setFont(new Font("Helvetica", Font.BOLD, 24));
    aboutus.setOpaque(true);

    JLabel label = new JLabel("Welcome back!");

    panel.add(meals);
    panel.add(reminder);
    panel.add(shop);
    panel.add(sleep);
    panel.add(account);
    panel.add(aboutus);

    topPanel.add(label);
    topPanel.setSize(200, 30);
    //rearrange panels on frame
    frame.add(topPanel, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.CENTER);

    frame.setSize(480, 720);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}


输出:

【讨论】:

    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多