将JPanels一个接一个地垂直添加到JFrame,每个都水平触摸边框

时间:2014-09-25 08:21:35

标签: java swing layout jframe

我想一个接一个地垂直添加JPAnels到JFrame。每个面板应覆盖整个jframe宽度。即使框架尺寸较大,这些面板也应覆盖宽度。我希望使用任何布局。不应使用线程。从包含的图像中获取我想要的东西。 提前致谢。任何尝试的帮助将不胜感激......

以下是您要求的代码,然后向我投票赞成......

public class AttachToWalls extends JFrame implements ActionListener {

    JLabel m1;
    JLabel m2;
    JPanel pane;
    JPanel bottom = new JPanel();
    JScrollPane jsp;
    JButton sender = new JButton("Sender");
    JButton receiver = new JButton("Receiver");

    AttachToWalls() {
        setLayout(new FlowLayout());
        jsp = new JScrollPane(pane);
        add(jsp, BorderLayout.CENTER);
        sender.addActionListener(this);
        receiver.addActionListener(this);
        bottom.add(sender);
        bottom.add(receiver);
        add(bottom);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sender) {
            m1 = new JLabel("Message from Sender...");
            remove(sender);
            remove(receiver);
            Component comp = Box.createHorizontalStrut(this.getWidth() - m1.getWidth());
            comp.setBackground(Color.red);
            JPanel pane1 = new JPanel();
            pane1.setBackground(Color.gray);
            pane1.add(m1);
            pane1.add(comp);
            add(pane1);
            this.validate();
        } else if (e.getSource() == receiver) {
            m2 = new JLabel("Messsage from Receiver...");
            Component comp = Box.createHorizontalStrut((int) this.getWidth() - m2.getWidth());
            comp.setBackground(Color.red);
            JPanel pane2 = new JPanel();
            pane2.setBackground(Color.gray);
            pane2.add(m2);
            pane2.add(comp);
            add(pane2);
            this.validate();
        }
    }

    public static void main(String[] args) {
        new AttachToWalls();
    }
}

enter image description here enter image description here

3 个答案:

答案 0 :(得分:3)

您可以使用GridBagLayout。它支持“填充”约束,允许您填充网格中的组件。阅读How to Use GridLayout上Swing教程中的部分,了解有关可以使用的约束的更多信息。

或者,如果您想跳出框,可以使用Relative Layout。它可以像BoxLayout一样工作。它可以在首选高度垂直显示组件,但它有一个参数来自动填充宽度。它不是JDK的一部分,但您不必担心构建约束。

答案 1 :(得分:3)

首先,您不能多次显示标签或任何组件的实例。

而不是在动作监听器中连续添加标签,如下所示:

    pane1.setBackground(Color.gray); 
    pane1.add(m1);

像这样重新实例化:

    pane1.setBackground(Color.gray);
    m1 = new JLabel("Message from Sender...");
    pane1.add(m1);

您的最佳布局如GridBagLayout之前所述。 Check out this tutorial

你应该像这样设置GridBagLayout(填充水平):

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = y;


bottom.add(sender, gbc);

然后声明一个整数字段 y 和约束对象,如下所示:

private int y = 0;
private GridBagConstraints gbc = new GridBagConstraints();

然后每次在 actionPerformed(..)方法中添加面板时迭代y。

        gbc.gridy = y++;
        add(pane1, gbc);

答案 2 :(得分:1)

经过多方努力,我已经解决了您的问题,希望您能接受我的解决方案:)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ChatPane extends JFrame {

    JPanel msg = null;
    JLabel sub = null;
    Box center = Box.createVerticalBox();
    JScrollPane jsp = new JScrollPane(center);
    JPanel ctrl = new JPanel(new FlowLayout());
    JButton send = new JButton("Send");
    JButton rec = new JButton("Recieve");

    public ChatPane() {
        ctrl.add(send);
        ctrl.add(rec);
        Container cnt = getContentPane();

        cnt.add(jsp, BorderLayout.CENTER);
        cnt.add(ctrl, BorderLayout.SOUTH);
        send.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                msg = new JPanel(new BorderLayout());
                msg.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3) );
                sub = new JLabel("Sender. . . . Message");
                sub.setBorder(BorderFactory.createLineBorder(Color.red));
                msg.add(sub, BorderLayout.WEST);
                msg.setMaximumSize(new Dimension(Integer.MAX_VALUE, (int)msg.getPreferredSize().getHeight()));
                center.add(msg);
                validate();
            }
        });
        rec.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                msg = new JPanel(new BorderLayout());
                msg.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3) );
                sub = new JLabel("Reciver. . . . Message");
                sub.setBorder(BorderFactory.createLineBorder(Color.black));
                msg.add(sub, BorderLayout.EAST);
                msg.setMaximumSize(new Dimension(Integer.MAX_VALUE, (int)msg.getPreferredSize().getHeight()));
                center.add(msg);
                validate();
            }
        });
        setPreferredSize(new Dimension(300, 300));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        pack();

    }

    public static void main(String[] args) {
        new ChatPane();
    }
}