如何在窗口中移动3个按钮 [英] How to move 3 buttons inside a window

查看:84
本文介绍了如何在窗口中移动3个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,当您单击左按钮时,我试图将三个按钮向左移动.当我单击它时;目前没有任何反应.有人可以向我解释我在这里做错了什么吗?另外,由于某种原因,它已经停止正确地编译,我不确定为什么,但是我相信这是由于尝试单击按钮时将按钮向左移动时我的代码错误.我不希望窗口移动.只是窗口中的按钮.有人看到我在做什么错吗,您能解释一下吗?

In the below code I am attempting to move the three buttons to the left when you click the left button. When I click it; nothing happens currently. Can anyone explain to me what I am doing wrong here? Also, for some reason it has stopped compiling correctly and I am unsure why but I BELIEVE it is because of a mistake in my code while attempting to get the buttons to move to the left when you click the button. I do NOT want the window to move. Just the buttons within the window. Does any one see what I am doing wrong and can you explain it?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Buttons extends JFrame {
//Control Definitions

    JButton resetButton;
    JButton leftButton;
    JButton colorButton;
    JPanel buttonPanel;
// Layout Definiton
    eventHandle evt;
    FlowLayout flt;
    Point point; //to Hold Previous Window Position
    Color color; //to Hold Previous Color

    public Buttons() {
        super("Buttons Window");
        flt = new FlowLayout();//inialize the Flow Layout
        buttonPanel = new JPanel(flt);
        //inialize the buttonPanel With Flow Layout
        //initialize buttons
        resetButton = new JButton("Reset");
        leftButton = new JButton("Left");
        colorButton = new JButton("Blue");
        evt = new eventHandle(); //initiate the eventhandle class
        buttonPanel.add(leftButton); //add leftButton
        buttonPanel.add(colorButton);//add colorButton
        buttonPanel.add(resetButton);//add colorButton
        getContentPane().add(buttonPanel);//buttonPanel
        //add actionlistners
        leftButton.addActionListener(evt);
        colorButton.addActionListener(evt);
        resetButton.addActionListener(evt);
        setBounds(20, 120, 250, 70);
        //following Initate the point with Center of Scren
        point = new Point((Toolkit.getDefaultToolkit().
                getScreenSize().width - getWidth()) / 2, 
                (Toolkit.getDefaultToolkit().getScreenSize().height
                - getHeight()) / 2);
        setLocation(point); //locates the window in center
        color = buttonPanel.getBackground();//stores the initial color
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    class eventHandle implements ActionListener { //Event Handler

        public void actionPerformed(ActionEvent e) {
            {
                if (e.getSource() == leftButton) ///if its from leftButton
                {
                    leftButton.setAlignmentX(Component.LEFT_ALIGNMENT);
                    colorButton.setAlignmentX(Component.LEFT_ALIGNMENT);
                    resetButton.setAlignmentX(Component.LEFT_ALIGNMENT);
    //setLocation( (point.x -150), point.y);//shift the window 150 pixels left
                } else if (e.getSource() == colorButton) {
                    buttonPanel.setBackground(color.BLUE); 
                    //sets the backgorund to Blue
                } else {
                    leftButton.setAlignmentX(Component.CENTER_ALIGNMENT); 
                    //sets the location to previous location
                    colorButton.setAlignmentX(Component.CENTER_ALIGNMENT);
                    resetButton.setAlignmentX(Component.CENTER_ALIGNMENT);
                }

            }
        }
    }

    public static void main(String[] args) {
        Buttons buttonwindow = new Buttons();
    }
}

推荐答案

它已停止编译,因为您删除了一个赞,因此将一个赞}"放在方法上方:

It has stopped compiling, because you deleted one accolade, so put one accolade "}" just above the method:

public static  void main(String[] args)

并且代码应编译.请提供反馈.

and the code should compile. pls feedback.

也可以这样重写您的主要方法:

Also rewrite your main method like this:

public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Buttons buttonwindow = new Buttons();
                    }
                }
            );           
    }

必须通过事件分发线程(缩写 EDT ),否则您可能会得到不想要的视觉效果.请参阅此处以获取解释.

Every usage of Swing components must be done thorugh the Event Dispatch Thread (abbreviated EDT) or you will probably get unwanted visual effects. See here for explanation.

EDIT ^ 2:

要实现所需的行为,请重写动作监听器,如下所示:

To achieve the desired behavior, rewrite the the action listener like this:

if (e.getSource() == leftButton)  {
    ((FlowLayout)buttonPanel.getLayout()).setAlignment(FlowLayout.LEFT); //1
    buttonPanel.revalidate(); //2
}
else if (e.getSource() == colorButton) {
    buttonPanel.setBackground(color.BLUE); 
} 
else {
    ((FlowLayout)buttonPanel.getLayout()).setAlignment(FlowLayout.CENTER);
    buttonPanel.revalidate();
}

必须通过分配的布局管理器对Swing组件的外观进行任何更改,在这种情况下,请

Any change to the visual appereance to the Swing component must be done through the assigned layout manager, in this case FlowLayout - in line 1.

要查看更改,您必须通知Swing组件布局管理器以重新排列组件-第2行中的

To see the change you must notify the Swing components layout manager to rearrange the components - in line 2 the revalidate() method "notifies" the layout manager to recalculate the new positions and eventually "notifies" the EDT to draw it on the screen.

这篇关于如何在窗口中移动3个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆