jbutton不在正确的位置上有两个课程
不知道如何过度简化这一点,但是当我尝试将按钮放在其他位置时,它不会移动。我想这是因为边界景点,我看着并且找不到太多适用于此的。感谢您的帮助!我是编码的新手,这是我正在学习的新事物。
public class MazeMouse extends JPanel implements ActionListener {
JButton d,k;
int button = 0;
MazeMouse(){
setBackground(Color.black);
k = new JButton("Press To Finish");
d = new JButton("Press To Begin");
add(d);
add(k);
k.addActionListener(this);
d.addActionListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(button == 1) {
g2.setColor(Color.red);
g2.fillRect(300,0,200,50);
g2.fillRect(375, 50, 50, 200);
g2.fillRect(375,525,50,200);
g2.fillRect(300, 725, 200, 50);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==d)
{
button++;
d.setEnabled(false);
repaint();
}
if(e.getSource()==k) {
setBackground(Color.green);
}
}
}
和班级
public class MM implements MouseMotionListener,ActionListener {
JFrame f;
MazeMouse p;
int button = 0;
public MM(){
f = new JFrame();
Container c = f.getContentPane();
c.setLayout(new BorderLayout());
p = new MazeMouse();
c.add(p);
f.setSize(800,800);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[ ]){
MM t = new MM();
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Don't know how to over simplify this but when I tried to put the buttons in a different location it wouldn't move. I suppose its because of the BorderLayout, I looked and couldn't find much that applied to this. Thanks for the help! Im new to coding and this is a new thing that I'm learning.
public class MazeMouse extends JPanel implements ActionListener {
JButton d,k;
int button = 0;
MazeMouse(){
setBackground(Color.black);
k = new JButton("Press To Finish");
d = new JButton("Press To Begin");
add(d);
add(k);
k.addActionListener(this);
d.addActionListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(button == 1) {
g2.setColor(Color.red);
g2.fillRect(300,0,200,50);
g2.fillRect(375, 50, 50, 200);
g2.fillRect(375,525,50,200);
g2.fillRect(300, 725, 200, 50);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==d)
{
button++;
d.setEnabled(false);
repaint();
}
if(e.getSource()==k) {
setBackground(Color.green);
}
}
}
And class MM
public class MM implements MouseMotionListener,ActionListener {
JFrame f;
MazeMouse p;
int button = 0;
public MM(){
f = new JFrame();
Container c = f.getContentPane();
c.setLayout(new BorderLayout());
p = new MazeMouse();
c.add(p);
f.setSize(800,800);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[ ]){
MM t = new MM();
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论