Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何布局这个GUI?_Java_Swing_User Interface_Jframe_Jpanel - Fatal编程技术网

Java 如何布局这个GUI?

Java 如何布局这个GUI?,java,swing,user-interface,jframe,jpanel,Java,Swing,User Interface,Jframe,Jpanel,我正在用java创建一个GUI。目前我有一个空的JFrame,正在尝试向它添加一个JPanel。JPanel包含按钮、文本等。但是这些都没有正确显示。我的代码如下: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class memoDisplayUI { JFrame frame = new JFrame(); JPanel panel = new JPanel(); JTextAre

我正在用java创建一个GUI。目前我有一个空的JFrame,正在尝试向它添加一个JPanel。JPanel包含按钮、文本等。但是这些都没有正确显示。我的代码如下:

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.setContentPane(panel);
}
}
我想要的布局与此非常相似:

有人能告诉我这是为什么吗


谢谢你的帮助:

你说的。。。所有这些都没有正确显示?是否未将元素添加到框架中?我看到了BorderLayout,可能是GridBagLayout和/或GridLayout。我在上一篇文章中给了你一个提示。基本上,您是将组件添加到具有默认flowlayout的面板中。您正在尝试定位所有无法使用布局管理器的组件。将面板布局设置为空。不过,我要再次重申,查看布局管理器通过查看和查看问题的答案来进行一些练习,查看我看到2个BorderLayout实例,一个作为其他组件和布局的“外部”布局,另一个在页面顶部的标签和x按钮。文本区域最好用JScrollPane包装,位于外部BorderLayout的中心。B/E/M按钮将进入外部布局页面末端的单行网格布局。在外部布局面板中添加一个清空命令,在布局中添加一些填充,就完成了。@mKorbel不是为了回答这个问题而玩的