布局高手,SWING高手,快来!!
在一个JFrame内,先加一个面板A1,然后再A1上添加B1,B2,B3 B1面板背景通过
public void paintComponent(Graphics g) {
g.drawImage(img, 0 , 0 , null );
重画
然后在这个B1面板上添加其他组件
问题是,现在B1组件过多,超过JFrame大小,于是我把它换成了一个滚面板,然后添加C1面板,把原B1上所有组件拉到C1
求两种解决方案,一:定义4个按钮,点击的时候,滚动面板向4个方向拉一定距离,请问这个如何实现?
二:类似于如下代码形成的 只不过我需要把下面的标签换成面板,并且面板上面的组件也随着移动,就像那种游戏中屏幕的移动似的,只不过我需要把它固定在一个窗口中。
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.*;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JViewport;
import javax.swing.border.EmptyBorder;
public class MouseScroll extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
protected JViewport m_viewport;
protected int m_pgVert;
protected int m_pgHorz;
public MouseScroll() {
super("Scrolling Automatically");
initialize();
setSize(400, 300);
getContentPane().setLayout(new BorderLayout());
URL url = MouseScroll.class.getResource("/image/背景.jpg"); //你随便找张图片试试
final ImageIcon shuttle = new ImageIcon(url);
m_pgVert = shuttle.getIconHeight() / 50;
m_pgHorz = shuttle.getIconWidth() / 50;
JLabel lbl = new JLabel(shuttle); //这个要换
final JPanel pnl = new JPanel(new BorderLayout());
getContentPane().add(pnl, BorderLayout.CENTER);
pnl.setBorder(new EmptyBorder(20, 20, 20, 20));
m_viewport = new JViewport();
m_viewport.setView(lbl);
pnl.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent e) {
Rectangle rect = m_viewport.getBounds();
int x = e.getX();
int y = e.getY();
if ((int) rect.getX() >= x) {
movePanel(-1, 0);
}
if ((int) (rect.getX() + rect.getWidth()) <= x) {
movePanel(1, 0);
}
if ((int) rect.getY() >= y) {
movePanel(0, -1);
}
if ((int) (rect.getY() + rect.getHeight()) <= y) {
movePanel(0, 1);
}
}
});
pnl.add(m_viewport, BorderLayout.CENTER);
JPanel pv = new JPanel(new BorderLayout());
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
movePanel(0, -1);
}
};
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
movePanel(0, 1);
}
};
getContentPane().add(pv, BorderLayout.EAST);
JPanel ph = new JPanel(new BorderLayout());
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
movePanel(-1, 0);
}
};
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
movePanel(1, 0);
}
};
getContentPane().add(ph, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
movePanel(0, 0);
}
private void initialize() {
this.setBounds(new Rectangle(0, 0, 500, 500));
this.setVisible(false);
}
protected void movePanel(int xmove, int ymove) {
Point pt = m_viewport.getViewPosition();
pt.x += m_pgHorz * xmove;
pt.y += m_pgVert * ymove;
pt.x = Math.max(0, pt.x);
pt.x = Math.min(getMaxXExtent(), pt.x);
pt.y = Math.max(0, pt.y);
pt.y = Math.min(getMaxYExtent(), pt.y);
m_viewport.setViewPosition(pt);
}
protected void enableComponent(JComponent c, boolean b) {
if (c.isEnabled() != b)
c.setEnabled(b);
}
protected int getMaxXExtent() {
return m_viewport.getView().getWidth() - m_viewport.getWidth();
}
protected int getMaxYExtent() {
return m_viewport.getView().getHeight() - m_viewport.getHeight();
}
public static void main(String argv[]) {
new MouseScroll();
}
}
高手在哪里?我两眼闪光。。。