简体   繁体   English

JButton无法出现在JFrame中

[英]JButtons fail to appear in the JFrame

I have code that displays a menu and 4 JButtons in a JFrame. 我有显示一个菜单和一个JFrame中的4个JButton的代码。 I tested the code last night and everything was working fine. 昨晚我测试了代码,一切正常。 Now the JButtons do not appear in the JFrame today in the morning. 现在,今天早上JButton不会出现在JFrame中。 I tried doing in Eclipse and still I got the same result. 我尝试在Eclipse中执行操作,但仍然得到了相同的结果。

The output I am getting : 我得到的输出:

替代文字


My code: 我的代码:


import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;

public class Control  {

//JFrame
JFrame main = new JFrame();

//MenuBar
JMenuBar menuBar = new JMenuBar();

//Adding the menu
JMenu fileMenu = new JMenu("File");
JMenu functionMenu = new JMenu("Function");
JMenu helpMenu = new JMenu("Help");



//Adding the Menu Item
JMenuItem addFlight = new JMenuItem("Add Flight");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem landFlight = new JMenuItem("Land Flight");
JMenuItem virtualPath = new JMenuItem("Virtual Path");
JMenuItem flightDetails = new JMenuItem("Flight Details");
JMenuItem about = new JMenuItem("About ...");







//JPanel
JPanel pnlButton = new JPanel();

//Buttons
JButton btnAddFlight = new JButton("Add Flight");
JButton btnLandFlight = new JButton("Land Flight");
JButton btnVirtualPath = new JButton("Virtual Path");
JButton btnFlightDetails = new JButton("Flight Details");



public Control() {
    //Adding to the file menu
    fileMenu.add(addFlight);
    fileMenu.add(exit);


    //Adding to the function menu
    functionMenu.add(landFlight);
    functionMenu.add(virtualPath);
    functionMenu.add(flightDetails);



    //Adding to the help menu
    helpMenu.add(about);


    exit.add(new JSeparator());
    flightDetails.add(new JSeparator());

    //Adding the Menus to the Menu Bar
    menuBar.add(fileMenu);
    menuBar.add(functionMenu);
    menuBar.add(helpMenu);






    //FlightInfo setbounds
    btnAddFlight.setBounds(30, 30, 120, 30);
    btnLandFlight.setBounds(30, 80, 120, 30);
    btnVirtualPath.setBounds(30, 130, 120, 30);
    btnFlightDetails.setBounds(30, 180, 120, 30);



    //JPanel bounds
    pnlButton.setLayout(null);



    //Adding to JFrame
    pnlButton.add(btnAddFlight);
    pnlButton.add(btnLandFlight);
    pnlButton.add(btnVirtualPath);
    pnlButton.add(btnFlightDetails);


    main.add(pnlButton);

    // JFrame properties
    main.setJMenuBar(menuBar);
    main.setLayout(null);
    main.setBackground(Color.red);
    main.setSize(800, 300);


    main.setTitle("Air Traffic Control");

    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setVisible(true);

    //Adding the actionlistener
    //btnAddFlight.addActionListener(new AddFlight());
    //btnLandFlight.addActionListener(new LandFlight());







}

public static void main(String[] args) {

    new Control();

}
}

I want to make the JButtons appear on the JFrame. 我想让JButtons出现在JFrame上。

Many Thanks 非常感谢

You shouldn't add widgets (pnlButton) directly to the JFrame, you need to add them to a sub panel that is automatically created for you called the content pane. 您不应该将小部件(pnlButton)直接添加到JFrame,而是需要将它们添加到为您自动创建的名为内容窗格的子面板中。 To get the content pane do 要获取内容窗格,请执行以下操作

Container cp = main.getContentPane();

so then do 所以做

cp.add(pnlButton);

It's typically a bad idea to use a null layout with absolute positioning, btw. 通常,将空布局用于绝对定位是个坏主意。

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

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