如何解决使用Java创建GUI时容器引起的错误

发布于 2025-01-22 10:46:33 字数 1399 浏览 4 评论 0原文

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

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

发布评论

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

评论(1

旧时光的容颜 2025-01-29 10:46:33

Oracle有一个有用的教程,创建带有swing 的GUI。使用Netbeans IDE部分跳过学习秋千。

我修改了您的代码以创建以下GUI。

所有摆动应用程序必须以swingutilities InvokeLater方法开始调用。 This method ensures that the Swing components are created and executed on the Event Dispatch Thread

我分开了jframejpanels的创建。您可以通过填充每个jpanel的摇摆组件来大小jpanels。您打包jframe,让它采用jpanels的大小。请密切注意在容器中放置组件部分。

这是完整的可运行代码。

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUIWindow implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GUIWindow());
    }

    @Override
    public void run() {
        JFrame theGUI = new JFrame();
        theGUI.setTitle("Visual studio code");
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        theGUI.add(createPanel(Color.red), BorderLayout.NORTH);
        theGUI.add(createPanel(Color.blue), BorderLayout.EAST);
        theGUI.add(createPanel(Color.blue), BorderLayout.WEST);
        theGUI.add(createPanel(Color.red), BorderLayout.SOUTH);
        theGUI.add(createPanel(Color.green), BorderLayout.CENTER);
        
        theGUI.pack();
        theGUI.setLocationByPlatform(true);
        theGUI.setVisible(true);
    }
    
    private JPanel createPanel(Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        panel.setPreferredSize(new Dimension(100, 100));
        
        return panel;
    }

}

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

I modified your code to create the following GUI.

enter image description here

All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

I separated the creation of the JFrame and the JPanels. You size each of the JPanels, ideally by filling each JPanel with Swing components. You pack the JFrame, letting it take the size of the JPanels. Pay close attention to the Laying Out Components Within a Container section.

Here's the complete runnable code.

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUIWindow implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GUIWindow());
    }

    @Override
    public void run() {
        JFrame theGUI = new JFrame();
        theGUI.setTitle("Visual studio code");
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        theGUI.add(createPanel(Color.red), BorderLayout.NORTH);
        theGUI.add(createPanel(Color.blue), BorderLayout.EAST);
        theGUI.add(createPanel(Color.blue), BorderLayout.WEST);
        theGUI.add(createPanel(Color.red), BorderLayout.SOUTH);
        theGUI.add(createPanel(Color.green), BorderLayout.CENTER);
        
        theGUI.pack();
        theGUI.setLocationByPlatform(true);
        theGUI.setVisible(true);
    }
    
    private JPanel createPanel(Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        panel.setPreferredSize(new Dimension(100, 100));
        
        return panel;
    }

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