在全屏模式下调整框架组件的大小

2024-03-06

我希望我的框架组件始终在屏幕中进行调整,无论屏幕尺寸如何。我在笔记本电脑(小屏幕)中绘制框架,当我在另一台具有大屏幕的机器(jar 文件)中运行应用程序时,框架组件不会重新调整大小!

当我在任何机器上将框架置于全屏模式时,如何使框架调整其组件的大小?

目前布局

Code

package package_MSM;

import java.awt.Color;

public class MSMGui extends JFrame 
{
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextArea textArea;
    private JPanel pnlLogo1;
    private JLabel lblLogo1;;
    private static JButton btnSmpd1;
    private static JButton btnSmpd2;
    private static JButton btnSmpd3;
    private static JButton btnSmpd4;
    private static JButton btnSmpd5;
    private static JButton btnSmpd6;
    private static JButton btnSmpd7;

    /**
     * Launch the application.
     */

    public static void main(String[] args) 
    {
        EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                try 
                {
                    MSMGui frame = new MSMGui();
                    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
                    frame.setVisible(true);
                    
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MSMGui() 
    {
        setResizable(true);
        setTitle("MSM");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 841, 334);
        contentPane = new JPanel();
        contentPane.setBackground(Color.BLACK);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JPanel panelSmpd = new JPanel();
        panelSmpd.setBounds(10, 69, 804, 105);
        panelSmpd.setBackground(Color.BLACK);
        contentPane.add(panelSmpd);
        panelSmpd.setLayout(null);
        
        JMenuBar menuBar = new JMenuBar();
        menuBar.setBackground(UIManager.getColor("MenuBar.background"));
        menuBar.setFont(new Font("Segoe UI", Font.PLAIN, 12));
        setJMenuBar(menuBar);
        
        JMenu mnMenu = new JMenu("Menu");
        menuBar.add(mnMenu);
                    
        JMenuItem mntmAlarmStop = new JMenuItem("Stop alarm sound");
        mntmAlarmStop.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                stopAlarm();
                textArea.append(dateTime()+ " Alarm cleared by the Operator from the Menu Bar\n");
                writeTofile(dateTime()+ " Alarm cleared by the Operator from the Menu Bar\n");
            }
        });
        mnMenu.add(mntmAlarmStop);

        JMenuItem mntmExit = new JMenuItem("Exit");
        mntmExit.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                // display the showOptionDialog
                Object[] options = { "OK", "Cancel" };
                int choice = JOptionPane.showOptionDialog(null, 
                                        "You really want to quit?", 
                                        "Quit?", 
                                        JOptionPane.YES_NO_OPTION, 
                                        JOptionPane.QUESTION_MESSAGE, 
                                        null, 
                                        options, 
                                        options[0]);

                // interpret the user's choice
                if (choice == JOptionPane.YES_OPTION)
                {
                    System.exit(0);
                }
            }
        });
        mnMenu.add(mntmExit);       
        
        btnSmpd1 = new JButton("SPD 1");
        btnSmpd1.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {               
                btnSmpd1.setBackground(Color.YELLOW);
                stopAlarm();
                btnSmpd1.setEnabled(false);
                textArea.append(dateTime() + " Alarm Stampede 1 cleared by the Operator\n");
                writeTofile(dateTime()  + " Alarm Stampede 1 cleared by the Operator\n");
            }
        });
        btnSmpd1.setFont(new Font("Tahoma", Font.BOLD, 15));
        btnSmpd1.setBounds(23, 13, 100, 74);
        panelSmpd.add(btnSmpd1);
        btnSmpd1.setOpaque(true);
        
        btnSmpd2 = new JButton("SPD 2");
        btnSmpd2.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {               
                btnSmpd2.setBackground(Color.YELLOW);
                stopAlarm();
                btnSmpd2.setEnabled(false);
                textArea.append(dateTime() + " Alarm Stampede 2 cleared by the Operator\n");
                writeTofile(dateTime()  + " Alarm Stampede 2 cleared by the Operator\n");
            }
        });
        btnSmpd2.setFont(new Font("Tahoma", Font.BOLD, 15));
        btnSmpd2.setBounds(133, 13, 100, 74);
        panelSmpd.add(btnSmpd2);
        btnSmpd2.setOpaque(true);
        
        btnSmpd3 = new JButton("SPD 3");
        btnSmpd3.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                btnSmpd3.setBackground(Color.YELLOW);
                stopAlarm();
                btnSmpd3.setEnabled(false);
                textArea.append(dateTime() + " Alarm Stampede 3 cleared by the Operator\n");
                writeTofile(dateTime()  + " Alarm Stampede 3 cleared by the Operator\n");
            }
        });
        btnSmpd3.setFont(new Font("Tahoma", Font.BOLD, 15));
        btnSmpd3.setBounds(243, 13, 100, 74);
        panelSmpd.add(btnSmpd3);
        btnSmpd3.setOpaque(true);
        
        btnSmpd4 = new JButton("SPD 4");
        btnSmpd4.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                btnSmpd4.setBackground(Color.YELLOW);
                stopAlarm();
                btnSmpd4.setEnabled(false);
                textArea.append(dateTime() + " Alarm Stampede 4 cleared by the Operator\n");
                writeTofile(dateTime()  + " Alarm Stampede 4 cleared by the Operator\n");
            }
        });
        btnSmpd4.setFont(new Font("Tahoma", Font.BOLD, 15));
        btnSmpd4.setBounds(353, 13, 100, 74);
        panelSmpd.add(btnSmpd4);
        btnSmpd4.setOpaque(true);
        
        btnSmpd5 = new JButton("SPD 5");
        btnSmpd5.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {               
                btnSmpd5.setBackground(Color.YELLOW);
                stopAlarm();
                btnSmpd5.setEnabled(false);
                textArea.append(dateTime() + " Alarm Stampede 5 cleared by the Operator\n");
                writeTofile(dateTime()  + " Alarm Stampede 5 cleared by the Operator\n");
            }
        });
        btnSmpd5.setFont(new Font("Tahoma", Font.BOLD, 15));
        btnSmpd5.setBounds(463, 11, 100, 76);
        panelSmpd.add(btnSmpd5);
        btnSmpd5.setOpaque(true);
        
        btnSmpd6 = new JButton("SPD 6");
        btnSmpd6.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {               
                btnSmpd6.setBackground(Color.YELLOW);
                stopAlarm();
                btnSmpd6.setEnabled(false);
                textArea.append(dateTime() + " Alarm Stampede 6 cleared by the Operator\n");
                writeTofile(dateTime()  + " Alarm Stampede 6 cleared by the Operator\n");
            }
        });
        btnSmpd6.setFont(new Font("Tahoma", Font.BOLD, 15));
        btnSmpd6.setBounds(573, 11, 100, 76);
        panelSmpd.add(btnSmpd6);
        btnSmpd6.setOpaque(true);
        
        btnSmpd7 = new JButton("SPD 7");
        btnSmpd7.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {               
                btnSmpd7.setBackground(Color.YELLOW);
                stopAlarm();
                btnSmpd7.setEnabled(false);
                textArea.append(dateTime() + " Alarm Stampede 7 cleared by the Operator\n");
                writeTofile(dateTime()  + " Alarm Stampede 7 cleared by the Operator\n");
            }
        });
        btnSmpd7.setFont(new Font("Tahoma", Font.BOLD, 15));
        btnSmpd7.setBounds(683, 11, 100, 76);
        panelSmpd.add(btnSmpd7);
        btnSmpd7.setOpaque(true);
        
        JLabel lblTitle = new JLabel("MNOC Stampede Monitor");
        lblTitle.setBounds(200, 11, 423, 27);
        lblTitle.setHorizontalTextPosition(SwingConstants.CENTER);
        lblTitle.setForeground(Color.GRAY);
        lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
        lblTitle.setFont(new Font("Tahoma", Font.BOLD, 32));
        contentPane.add(lblTitle);
        
        scrollPane = new JScrollPane();
        scrollPane.setBounds(40, 172, 754, 91);
        contentPane.add(scrollPane);
        
        textArea = new JTextArea();
        scrollPane.setViewportView(textArea);
        
        ImageIcon image = new ImageIcon("logo.jpg");
        
        pnlLogo1 = new JPanel();
        pnlLogo1.setBounds(50, 11, 70, 47);
        contentPane.add(pnlLogo1);
        pnlLogo1.setLayout(null);
        lblLogo1 = new JLabel();
        lblLogo1.setBounds(0, 0, 70, 47);
        pnlLogo1.add(lblLogo1);
        lblLogo1.setOpaque(true);
        lblLogo1.setVisible(true);
        lblLogo1.setIcon(image);
        pnlLogo1.setVisible(true);
    }
}

See 在容器内布置组件 http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html.


好的,我按照原始 GUI 的形式编写了一些代码,这些代码使用布局并会调整大小。由于我不清楚额外的高度应该分配在哪里,所以我把它分配给了文本区域。颜色、字体大小等留给用户练习。

截图

Small

Wide

Tall

SSCCE

package test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class StampedeMonitor {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame("MSM");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                
                JPanel gui = new JPanel(new BorderLayout(5,5));
                gui.setBorder(new EmptyBorder(10,10,10,10));
                
                JPanel logos = new JPanel(new BorderLayout(5,5));
                gui.add(logos, BorderLayout.NORTH);
                JPanel icon = new JPanel();
                // this should be set by the image
                icon.setPreferredSize(new Dimension(50,50));
                icon.setBackground(Color.RED);
                icon.setOpaque(true);
                logos.add(icon, BorderLayout.WEST);
                logos.add(new JLabel("MNOC Stampede Monitor", JLabel.CENTER));
                
                JPanel controls = new JPanel(new BorderLayout(5,5));
                gui.add(controls, BorderLayout.CENTER);
                JPanel buttons = new JPanel(new GridLayout(1,0,5,5));
                controls.add(buttons, BorderLayout.NORTH);
                for (int ii=1; ii<8; ii++) {
                    JButton button = new JButton("SPD " + ii);
                    Dimension d = button.getPreferredSize();
                    // make the buttons twice as tall as default
                    Dimension big = new Dimension(
                            (int)d.getWidth(),
                            (int)d.getHeight()*2);
                    button.setPreferredSize(big);
                    buttons.add(button);
                }
                controls.add(new JScrollPane( new JTextArea(5,10) ));
                                
                f.setContentPane(gui);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在全屏模式下调整框架组件的大小 的相关文章

随机推荐

  • 如何将 JSON 转换为 CSV 格式并存储在变量中

    我有一个可以在浏览器中打开 JSON 数据的链接 但不幸的是我不知道如何阅读它 有没有办法使用 JavaScript 将这些数据转换为 CSV 格式并将其保存在 JavaScript 文件中 数据如下 count 2 items title
  • 纹理不绘制

    我想在屏幕上绘制图像 但我得到的是黑色方块 但上面没有纹理 图像路径正确并已加载 因为矩形具有正确的大小 我有一个单独的类用于加载名为 Texture 的纹理 还有一个用于绘制名为 Sprite 的纹理的类 这是代码 Class Textu
  • 如何向用户请求联系人访问权限,然后建立并打开联系人?

    好的 我联系了我的手机 除了两件事外 它运行得很好 首先 它第一次失败 因为它要求我访问联系人 我怎样才能使它在用户授予对联系人的访问权限后添加它 其次 有没有办法打开联系人 以便用户在建立后可以查看它 这就是我所做的 IBAction a
  • 使 Horizo​​ntalScrollView 的子级与屏幕一样大?

    我解决这个问题的方法是为子视图创建一个自定义视图 然后为自定义视图重写 onMeasure 新的 onMeasure 将宽度和高度设置为尽可能大 问题是当你显示软键盘时and旋转手机 随着方向的改变和键盘的显示 onMeasure 将 最大
  • 如何读取用户的单个字符?

    有没有一种方法可以从用户输入中读取单个字符 例如 他们在终端按下一个键 然后返回 有点像getch 我知道 Windows 中有一个功能 但我想要跨平台的功能 以下是 ActiveState Recipes 站点的链接 其中介绍了如何在 W
  • Hadoop/Hive Collect_list 不包含重复项

    根据该帖子 Hive 0 12 Collect list https stackoverflow com questions 6445339 collect set in hive keep duplicates 我试图找到 Java 代码
  • 项目在 Visual Studio 2010 解决方案中不可用

    我创建了下面的工具来使用我们的非 Microsoft 版本控制中的现有项目文件夹创建 VS2010 解决方案 当代码运行时 您告诉它您的代码位于哪个文件夹中 您的解决方案需要转到哪个文件夹 然后接受要在您选择的文件夹中创建的解决方案名称 它
  • Android - 应用程序已被拒绝,因为它主要面向儿童

    我在 Adob e Animate for Kids 中制作了一个应用程序 该应用程序第一次被接受 几天后我在我的应用程序中添加了 Admob 广告 除此之外没有任何更改 但现在我的应用程序不接受 Play 商店 并且第一个版本仍然在线 谁
  • Python 中的常见陷阱[重复]

    这个问题在这里已经有答案了 时隔多年 今天我再次被可变的默认参数所困扰 除非需要 我通常不会使用可变的默认参数 但我想随着时间的推移我忘记了这一点 今天在应用程序中 我在 PDF 生成函数的参数列表中添加了 tocElements 现在每次
  • 如何在 Android 布局文件夹中定位 1280x720 WXGA720 分辨率(如新的 Galaxy Nexus)?

    在运行 4 0 3 Ice Cream Sandwich 的 Android 模拟器上以 WXGA720 分辨率测试我的应用程序时 我的应用程序从横向视图中的 layout normal land 854x480 文件夹和纵向视图中的 la
  • 使用 Flask 和 Python 的网页中的网络摄像头

    我使用创建了一个人脸识别模型keras and tensorflow 现在我尝试使用 Flask 和 Python 将其转换为 Web 应用程序 我的要求是 我需要在网页上显示一个实时网络摄像头 通过单击按钮 它应该拍摄照片并将其保存到指定
  • 如何清除HTML数据?

    我们可以使用查询选择器来清除 CSS 样式表缓存 如下所示 每当我们更改样式表并需要浏览器清除我们的 CSS 样式表时 我们可以更改版本号 例如 1 1 1 2 等 有没有类似的东西可以清除HTML数据 我的 HTML 标记是这样的 img
  • 如何转义“#”,使其不会在 GitHub 评论中引用问题?

    相对于参考一个问题 https stackoverflow com questions 1687262 link to the issue number on github within a commit message 我只想输出文本 5
  • 驱动器号和冒号后没有斜杠的 Windows 路径 - 它指向什么?

    我输错了路径而不是c foo txt wrote c foo txt 我预计它要么会失败 要么会解决c foo txt 但它似乎解决了foo txt在当前用户的主文件夹中 Powershell 返回 PS C gt System IO Pa
  • 如何从我的本地计算机和其他区域的 EC2 实例访问我的 AWS MSK 托管 kafka 队列

    我正在 AWS MSK 上设置托管 kafka 队列 从本地计算机连接时 我似乎无法使安全性发挥作用 并且无法确定是否可以使用从一个区域到另一个区域的安全组 我已经浏览了主要文档中有关设置安全组的信息here https docs aws
  • 从 Travis 上的 Protractor 在 Sauce Labs 上运行 e2e 测试

    所以我有我的开源项目 https github com ahmednuaman radian https github com ahmednuaman radian 并且我有一些可以在本地运行良好的 e2e 测试量角器 https gith
  • 检查是否安装了 Windows 映像组件(wic 注册表)

    如何检查64位XP机器上是否安装了Windows映像组件 检查是否存在C Windows System32 WindowsCodecs dll
  • 连接 3 个字符串并返回指向新字符串 C 的指针

    我想知道是否有人可以帮助我 我正在尝试连接 3 个字符串并返回指向新字符串的指针 我似乎不知道如何使用strncat代替strcat and strncpy代替strcpy 我只学习 C 所以任何帮助将不胜感激 char concatena
  • android Google Play 警告:SSL 错误处理程序漏洞

    我在我的应用程序中使用 gorbin ASNE SDK 我最近收到一封来自 Google 的电子邮件 主题如下 Google Play 警告 SSL 错误处理程序漏洞 在这封电子邮件中 Google 解释说我的应用程序有一个 WebView
  • 在全屏模式下调整框架组件的大小

    我希望我的框架组件始终在屏幕中进行调整 无论屏幕尺寸如何 我在笔记本电脑 小屏幕 中绘制框架 当我在另一台具有大屏幕的机器 jar 文件 中运行应用程序时 框架组件不会重新调整大小 当我在任何机器上将框架置于全屏模式时 如何使框架调整其组件