简体   繁体   English

如何使用Swing在JFrame中添加面板

[英]How to add panels in a JFrame using Swing

Components are not displayed in my JFrame using Swing. 使用Swing在我的JFrame中不显示组件。 Actually my aim is: 实际上,我的目标是:

  1. Add Frame 添加框架
  2. In the frame add panel 在框架添加面板中
  3. Panel cantains 3 buttons 面板可容纳3个按钮

But it didn't show. 但是没有显示。

Here is my code 这是我的代码

public class Panels
{
    JFrame frame;
    JPanel panel;
    private JButton addButton;
    private JButton modifyButton;
    private JButton deleteButton;

    Panels()
    {
        initGUI();
        launchFrame();
    }

    public void initGUI()
    {
        frame = new JFrame();
        panel = new JPanel();
        addButton = new JButton("Add");
        modifyButton = new JButton("Modify");
        deleteButton = new JButton("Delete");
    }

    public void launchFrame()
    {
        addButton.setBounds(130,50,225,25);
        addButton.setBounds(150,50,225,25);
        addButton.setBounds(170,50,225,25);
        addButton.setBounds(190,50,225,25);
        panel.add(addButton);
        panel.add(modifyButton);
        panel.add(deleteButton);
        panel.setLayout(null);
        panel.setBackground(Color.RED);

        frame.add(panel);
        frame.setTitle("My Frame with Panel");
        frame.setSize(600,400);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Here is main for calling Panels class 这是调用Panels类的主要方法

When run on main function Frame is shown without controllers (ie 3 buttons not shown) 在主功能上运行时,显示没有控制器的框架(即未显示3个按钮)

public class Main
{
    public static void main(String[] args)
    {
        Panels obj_panel=new Panels();
    }
}

This is the main problem 这是主要问题

frame.setLayout(null);

When you set the layout to null, that means that all of its components must have boundaries set. 如果将布局设置为null,则意味着其所有组件都必须设置边界。 You try to add the panel, without any boundaries. 您尝试添加面板,没有任何边界。 You only set the boundaries for the buttons in the panel. 您仅可以设置面板中按钮的边界。 If you remove the above line, it works. 如果删除上面的行,它将起作用。

Other Issues I'd really take a look at: 我还要看的其他问题:

  • Don't use null layouts at all. 根本不要使用空布局。 Instead make use of layout managers, and let them handle the sizing and positioning for you. 而是使用布局管理器,让他们为您处理大小和位置。 This results in aa much more manageable and flexible UI. 这将导致更加易于管理和灵活的UI。 Please take some time to learn the different layout managers. 请花一些时间来学习不同的布局管理器。 Start at Laying out Components Within a Container 布置容器中的组件开始

  • All Swing apps should run on a special thread known as the Event Dispatch Thread (EDT). 所有Swing应用程序都应在称为事件调度线程(EDT)的特殊线程上运行。 Please take some time to read Initial Threads to learn how you can accomplish this. 请花一些时间阅读初始线程,以了解如何完成此任务。

Here is a refactor (fixing the "Other issues" ) with no null layout, just using layout managers, margins, and borders, and the code in the main shows how to run the program on the Event Dispatch Thread 这是一个没有空布局的重构(解决了“其他问题” ),仅使用布局管理器,边距和边框,并且主代码显示了如何在事件分发线程上运行程序

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                Panels obj_panel = new Panels();
            }
        });   
    }
}

class Panels {

    private JFrame frame;
    private JPanel panel;
    private JButton addButton;
    private JButton modifyButton;
    private JButton deleteButton;

    Panels() {
        initGUI();
        launchFrame();
    }

    private void initGUI() {
        frame = new JFrame();       // default layout manager is BorderLayout
        panel = new JPanel();       // default layout manager is FlowLayout
        addButton = new JButton("Add");
        modifyButton = new JButton("Modify");
        deleteButton = new JButton("Delete");
    }

    private void launchFrame() {
        JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 10, 10));
        buttonPanel.setBackground(Color.RED);
        buttonPanel.add(addButton);
        buttonPanel.add(modifyButton);
        // add margin to left and right of delete button
        // other buttons will follow suit because of GridLayout
        deleteButton.setMargin(new Insets(0, 50, 0, 50));
        buttonPanel.add(deleteButton);
        // create some space at the top for the buttonPanel
        buttonPanel.setBorder(new EmptyBorder(20, 0, 0, 0));

        panel.add(buttonPanel);
        panel.setBackground(Color.RED);

        frame.add(panel);
        frame.setTitle("My Frame with Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM