为什么我不能改变框架内面板的位置?

why cant i change the position of the panel inside a frame?

我正在尝试创建一个带有几个按钮和绘图区域的 GUI。 除了绘图区域很小而且位置不对之外,它似乎可以正常工作。 这是我的代码:

public class ssGUI extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
protected JButton b1, bConnect, bDisconnect, b2;
protected JPanel canvas;

public ssGUI() {
    // run button
    b1 = new JButton("do something");
    b1.setVerticalTextPosition(AbstractButton.CENTER);
    b1.setHorizontalTextPosition(AbstractButton.LEADING);
    b1.setMnemonic(KeyEvent.VK_D);
    b1.addActionListener(this);
    b1.setEnabled(false);
    // connect button
    bConnect = new JButton("Connect");
    bConnect.setMnemonic(KeyEvent.VK_E);
    bConnect.addActionListener(this);
    bConnect.setEnabled(true);
    // disconnect button
    bDisconnect = new JButton("Disconnect");
    bDisconnect.setMnemonic(KeyEvent.VK_E);
    bDisconnect.addActionListener(this);
    bDisconnect.setEnabled(false);
    // clean nmea data button
    b2 = new JButton("do something else");
    b2.setMnemonic(KeyEvent.VK_E);
    b2.addActionListener(this);
    b2.setEnabled(false);
    // drawing panel
    canvas = new JPanel();
    canvas.setBackground(Color.white);

    add(b1); add(bConnect); add(bDisconnect); add(b2); add(canvas, BorderLayout.CENTER);
}

public static void createAndShowGUI() {
    JFrame frame = new JFrame("Range Adjustment GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ssGUI newContentPane = new ssGUI();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);
    frame.pack();
    frame.setVisible(true);
    frame.setLocation(500, 500);
    frame.setSize(500, 500);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI(); 
        }
    });
}}

这是我的 GUI 的样子:

如您所见,"canvas" 非常小,位于按钮的侧面。 我需要把它放在它们下面并填充框架区域。 谁能帮我解决这个问题? 谢谢你的帮助。

这是布局的另一个问题。

JPanel 有一个默认布局,我没有看到您为 ssGUI class 设置布局。因此将使用默认布局 (FlowLayout)。

添加到 ssGUI 的所有组件将以线性方式排成一排,尽可能多地容纳。当您的组件超过宽度时,它将被放置到下一行。

您可以考虑对主面板使用布局 ssGUIGridBagLayout 可能会给你想要的。

  1. 将主面板的布局设置为 BorderLayout
  2. 为您的按钮创建一个面板,并将此面板添加到主面板的 NORTH 位置。

这是一个工作示例:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ssGUI extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    protected JButton b1, bConnect, bDisconnect, b2;
    protected JPanel canvas;

    public ssGUI() {
        setLayout(new BorderLayout());
        // run button
        b1 = new JButton("do something");
        b1.setVerticalTextPosition(AbstractButton.CENTER);
        b1.setHorizontalTextPosition(AbstractButton.LEADING);
        b1.setMnemonic(KeyEvent.VK_D);
        b1.addActionListener(this);
        b1.setEnabled(false);
        // connect button
        bConnect = new JButton("Connect");
        bConnect.setMnemonic(KeyEvent.VK_E);
        bConnect.addActionListener(this);
        bConnect.setEnabled(true);
        // disconnect button
        bDisconnect = new JButton("Disconnect");
        bDisconnect.setMnemonic(KeyEvent.VK_E);
        bDisconnect.addActionListener(this);
        bDisconnect.setEnabled(false);
        // clean nmea data button
        b2 = new JButton("do something else");
        b2.setMnemonic(KeyEvent.VK_E);
        b2.addActionListener(this);
        b2.setEnabled(false);
        // drawing panel
        canvas = new JPanel();
        canvas.setBackground(Color.white);

        JPanel topPanel = new JPanel();
        topPanel.add(b1);
        topPanel.add(bConnect);
        topPanel.add(bDisconnect);
        topPanel.add(b2);
        add(topPanel, BorderLayout.NORTH);

        add(canvas, BorderLayout.CENTER);
    }

    public static void createAndShowGUI() {
        JFrame frame = new JFrame("Range Adjustment GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ssGUI newContentPane = new ssGUI();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
        frame.setLocation(500, 500);
        frame.setSize(500, 500);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
}