如何建立1個形狀然後控制他跑來跑去? 我想建立1個形狀,然後上面有4個按鈕。。可以按他跑去左邊 ,右邊 ,上面,下面..要如何寫丫 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我只會弄 4個按鈕轉背覲顏色....可是要弄形狀,然後控制他我不會.... 剛學不久,可是有點急需..沒有什麼時間研究QQ 有人能幫幫忙嗎...拜託以下的是個按鈕轉背覲顏色的code...import java.awt.Color;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class ButtonSettingBackground extends JPanel implements ActionListener { private JButton yellowButton = new JButton("Yellow"); private JButton blueButton = new JButton("Blue"); private JButton redButton = new JButton("Red"); private JButton greenButton = new JButton("Green"); public ButtonSettingBackground() { add(yellowButton); add(blueButton); add(redButton); add(greenButton); yellowButton.addActionListener(this); blueButton.addActionListener(this); redButton.addActionListener(this); greenButton.addActionListener(this); } public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); Color color = getBackground(); if (source == yellowButton) color = Color.yellow; else if (source == blueButton) color = Color.blue; else if (source == redButton) color = Color.red; else if (source == greenButton) color =Color.green; setBackground(color); repaint(); } public static void main(String[] args) { JFrame frame = new JFrame("ButtonTest"); frame.setSize(300, 200); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = frame.getContentPane(); contentPane.add(new ButtonSettingBackground()); frame.show(); }} 我只知道需要 + import java.awt.event.MouseListener;然後改成 private Button leftB = new Button("Move Left"); private Button rightB = new Button("Move Right"); private Button upB = new Button("Move Up"); private Button downB = new Button("Move Down"); upB.addMouseListener(this); downB.addMouseListener(this); leftB.addMouseListener(this); rightB.addMouseListener(this);之後不會了...有人能幫幫忙嗎QQ 用按钮还是通过上下左右键来控制都无所谓,添加对应的listener,捕捉事件就可以需要有一个坐标变量point来指示方块的位置每次点击按钮,就改变一下point的值,并且repaint界面界面在paintComponent里绘制小方块,位置在point这个思路就可以实现关键问题是需要实现双缓冲来消除闪烁你可以搜一下,很容易--------------------------来上海吧http://topic.csdn.net/u/20080429/18/2ef7e7f7-7a00-4838-b4a7-ed8104b756ed.html?seed=1556996959 java多线程求解 java打包成jar可执行文件 跪求帮忙!关于ByteArrayTest 我的PhotoShop作业要求 软件由语言编好之后是怎么转换成一般看到的那种一点就可以运行的形式的? 大家看下我这个程序:同是抛出和捕获多个自定义异常 关于for循环的弱弱的问题! 向各位大侠求助一本书 一个使用java包的简单问题 问一个问题? 只有main()函数的类如何调用 能够给简略的介绍一下static在java中到底起什么作用?怎么用呢?
可是要弄形狀,然後控制他我不會.... 剛學不久,可是有點急需..沒有什麼時間研究QQ 有人能幫幫忙嗎...拜託以下的是個按鈕轉背覲顏色的code...
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class ButtonSettingBackground extends JPanel implements ActionListener { private JButton yellowButton = new JButton("Yellow"); private JButton blueButton = new JButton("Blue"); private JButton redButton = new JButton("Red"); private JButton greenButton = new JButton("Green"); public ButtonSettingBackground() {
add(yellowButton);
add(blueButton);
add(redButton);
add(greenButton); yellowButton.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
greenButton.addActionListener(this);
} public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
Color color = getBackground();
if (source == yellowButton)
color = Color.yellow;
else if (source == blueButton)
color = Color.blue;
else if (source == redButton)
color = Color.red;
else if (source == greenButton)
color =Color.green;
setBackground(color);
repaint();
} public static void main(String[] args) {
JFrame frame = new JFrame("ButtonTest");
frame.setSize(300, 200);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); Container contentPane = frame.getContentPane();
contentPane.add(new ButtonSettingBackground()); frame.show();
}
}
upB.addMouseListener(this);
downB.addMouseListener(this);
leftB.addMouseListener(this);
rightB.addMouseListener(this);之後不會了...有人能幫幫忙嗎QQ
需要有一个坐标变量point来指示方块的位置
每次点击按钮,就改变一下point的值,并且repaint界面
界面在paintComponent里绘制小方块,位置在point
这个思路就可以实现
关键问题是需要实现双缓冲来消除闪烁
你可以搜一下,很容易--------------------------
来上海吧
http://topic.csdn.net/u/20080429/18/2ef7e7f7-7a00-4838-b4a7-ed8104b756ed.html?seed=1556996959