简体   繁体   English

将JPanel添加到JFrame时不显示

[英]JPanel not showing when added to JFrame

I am creating a GUI in java. 我正在用Java创建GUI。 Currently i have an empty JFrame and am trying to add a JPanel to it. 目前,我有一个空的JFrame,正在尝试向其添加一个JPanel。 The JPanel contains buttons, text etc. However none of this is being displayed. JPanel包含按钮,文本等。但是,这些都没有显示。 My code is as follows: 我的代码如下:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class memoDisplayUI { 


JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea jTextBox = new JTextArea();
JScrollPane scroll = new JScrollPane();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                memoDisplayUI frame = new memoDisplayUI();
                frame.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public memoDisplayUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    frame.getContentPane().setBackground(new Color(255, 255, 255));
    frame.getContentPane().setLayout(null);
    frame.setBounds(100, 100, 270, 400);
    frame.setUndecorated(true); //REMOVES MENU BAR
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel lblMemos = new JLabel("MEMOS");
    lblMemos.setForeground(new Color(100, 149, 237));
    lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
    lblMemos.setBounds(16, 16, 234, 37);
    panel.add(lblMemos);

    JButton button = new JButton("");
    button.setBackground(new Color(100, 149, 237));
    button.setBounds(7, 350, 40, 40);
    panel.add(button);
    button.setIcon(new ImageIcon("back.png"));

    JButton button_1 = new JButton("");
    button_1.setBackground(new Color(100, 149, 237));
    button_1.setBounds(113, 350, 40, 40);
    panel.add(button_1);
    button_1.setIcon(new ImageIcon("Edit.png"));

    JButton button_2 = new JButton("");
    button_2.setBackground(new Color(100, 149, 237));
    button_2.setBounds(220, 350, 40, 40);
    panel.add(button_2);
    button_2.setIcon(new ImageIcon("memo.png"));

    JButton btnExit = new JButton("");
    btnExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    });
    btnExit.setBorder(null);
    btnExit.setIcon(new ImageIcon("Exit.jpg"));
    btnExit.setBounds(216, 19, 40, 40);
    panel.add(btnExit);

    jTextBox = new JTextArea();
    scroll.setViewportView(jTextBox); // add scroll panel
    jTextBox.setTabSize(4);
    jTextBox.setLineWrap(true);
    jTextBox.setBackground(new Color(192, 192, 192));
    jTextBox.setBounds(8, 60, 255, 286);
    panel.add(jTextBox);

    frame.getContentPane().add(panel);
}
}

Could someone please advise as to why this is? 有人可以建议这是为什么吗?

Thanks very much :) 非常感谢 :)

Edit 编辑

From a few tweaks to the code, it appears this is the desired layout (in a non-resizable GUI). 从几处调整到代码,看来这是所需的布局(在不可调整大小的GUI中)。

备忘录GUI

I think you used null to get a "place it wherever fits"? 我认为您使用null来获得“将其放置在适当的位置”吗? Then use a FlowLayout 然后使用FlowLayout

frame.setLayout(new FlowLayout());

That should fix it :) 那应该解决它:)

Could someone please advise as to why this is? 有人可以建议这是为什么吗?

Using null layouts and not calling pack() . 使用null布局,而不调用pack()

The image I edited into the question was obtained as a screenshot of the GUI after I had commented out the call to setUndecorated(true) and dragged it a little bigger. 在我注释掉对setUndecorated(true)的调用并将其拖动到更大一点之后,获得了我在问题中编辑过的图像作为GUI的屏幕截图。 Doing so causes the JRE to validate the component structure (what pack() would do) and thereby make the components appear. 这样做会使JRE验证组件结构( pack()会做什么),从而使组件出现。


As I mentioned in a comment: 正如我在评论中提到的:

..a better question would be "How to layout this GUI?" ..一个更好的问题是“如何布局此GUI?” (so long as you provide an attempt) (只要您提供尝试)

And that leads me to my first comment. 这就是我的第一个评论。 (Now in longer form) (现在较长)

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. Java GUI可能必须在多种平台上,不同的屏幕分辨率和使用不同的PLAF上运行。 As such they are not conducive to exact placement of components. 因此,它们不利于组件的精确放置。 To organize the components for a robust GUI, instead use layout managers, or combinations of them 1 , along with layout padding & borders for white space 2 . 要为健壮的GUI组织组件,请改用布局管理器或它们的组合 1 ,以及空白 2的布局填充和边框。




So coming back to: 所以回到:

(so long as you provide an attempt) (只要您提供尝试)

Look over those two examples to see how they work, then attempt to combine some layouts and padding to create a frame that can then be packed to reduce to the natural size. 查看这两个示例以了解它们的工作原理,然后尝试结合一些布局和填充以创建一个框架,然后将其打包以减小为自然尺寸。

And a tip the the JTextArea . 并提示JTextArea Suggest a size in columns x rows combined with the Font size. 建议在列x行中结合Font大小的大小。

Just remove/comment this line from the above code at line number 46. 只需从上面的代码第46行删除/注释该行。

// frame.getContentPane().setLayout(null);

It should work fine.. 它应该工作正常。

Replace 更换

frame.getContentPane().setLayout(null);

with

frame.getContentPane().setLayout(new BorderLayout());

Good luck. 祝好运。

Edit: For future reference, to decide which LayoutManager should be used in your case, you should refer to this Visual Guide to LayoutManagers . 编辑:为了将来参考,要确定在您的情况下应该使用哪个LayoutManager ,您应该参考此LayoutManagers可视指南

1: You should never call setLayout(null). 1:永远不要调用setLayout(null)。 2: Try frame.validate() to layout the components with your layout. 2:尝试使用frame.validate()使用布局来布局组件。

Maybe you shoul replace : 也许您应该替换:

frame.getContentPane().add(panel);

by 通过

frame.setContentPane(panel);

Hope it helped 希望能有所帮助

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

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