在一个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();
}}高手在哪里?我弄了一天了,就是没弄好。水平菜,好打击
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();
}}高手在哪里?我弄了一天了,就是没弄好。水平菜,好打击
解决方案 »
- eclipse中无法启动tomcat,请教大家!
- 监听器问题
- 如何使用JSTL
- 怎么把jbmp的项目在eclipse3.5伽利略下部署到tomcat上
- ★★★★★请求了一个Hibernate问题★★★★★
- 关于webservice调用,如果调用方(client)和service是同一个Application,怎么能转换成本地调用呢?
- 请教,用spring AOP如何拦截action的execute方法?
- struts的表单提交取值问题?
- 谁有J2EE框架的物流信息系统???学习学习!!!!
- 出个课题请大家给讲一下思路(用CMP2。0)
- 本人做了一个Java代码生成工具
- 帮帮找个资料啦 谢谢。。。
稍看了下,大致认为是组件超过了JFrame,寻找显示方法,楼主可以尝试用JDesktopPane,它既能保持原有的尺寸,又会在JFrame尺寸内部显示。