我想将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();
}
}
您是否尝试过这个自己做?你有可以共享的可运行代码示例吗? – Gorbles 2014-09-25 08:24:29
@AndrewThompson快来看看我试过的代码。 – 2014-09-25 10:40:09
@Gorb兄弟看到我的代码。 – 2014-09-25 10:40:37