2014-09-25 29 views
1

我想将JPanels垂直添加到JFrame。每个面板应该覆盖jframe的整个宽度。即使重新调整框架尺寸,这些面板也应该覆盖宽度。我想要使​​用任何布局。线程不应该被使用。从包含的图像中抓取我想要的图像。 在此先感谢。帮助任何尝试将不胜感激......将JPanels垂直添加到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

+0

您是否尝试过这个自己做?你有可以共享的可运行代码示例吗? – Gorbles 2014-09-25 08:24:29

+0

@AndrewThompson快来看看我试过的代码。 – 2014-09-25 10:40:09

+0

@Gorb兄弟看到我的代码。 – 2014-09-25 10:40:37

回答

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(); 
    } 
} 
3

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

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

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); 

然后声明整型字段Ÿ和约束对象是这样的:

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

然后遍历ÿ每次你在您的actionPerformed(..)方法中添加面板。

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