java - 如何在窗口内移动 3 个按钮

标签 java swing layout jframe jbutton

在下面的代码中,当您单击左侧按钮时,我尝试将三个按钮向左移动。当我点击它时;目前什么也没有发生。谁能向我解释我在这里做错了什么?另外,由于某种原因,它已停止正确编译,我不确定为什么,但我相信这是因为我的代码在单击按钮时尝试让按钮向左移动时出现错误。我不想让 window 移动。只是窗口内的按钮。有人看出我做错了什么吗?你能解释一下吗?

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();
    }
}

最佳答案

它已停止编译,因为您删除了一项荣誉,因此将一项荣誉“}”放在方法上方:

public static  void main(String[] args)

并且代码应该可以编译。请反馈。

编辑:

同时重写你的 main 方法,如下所示:

public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

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

Swing 组件的每次使用都必须通过事件调度线程(缩写 EDT )完成,否则您可能会得到不需要的视觉效果。请参阅here进行解释。

编辑^2:

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

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 组件视觉外观的任何更改都必须通过指定的布局管理器完成,在本例中为 FlowLayout - 第 1 行。

要查看更改,您必须通知 Swing 组件布局管理器重新排列组件 - 第 2 行中的 revalidate()方法“通知”布局管理器重新计算新位置,并最终“通知”EDT 将其绘制在屏幕上。

关于java - 如何在窗口内移动 3 个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872775/

相关文章:

java - 在游戏中使用图形的最佳方式

java - Jtable1 header 提供给 Jtable2,具有排序和边距调整等属性

java - 如何在 Scala Swing 应用程序中使用 scala.swing.Applet?

magento - 在 Magento 中使用布局

android - TabWidget不会显示,即使它在ADT编辑器中显示

Android 布局 - ListView 占用太多空间

java - 使用类名动态命名索引

java - eBay 沙箱用户无法出售元素

java - 信息隐藏和封装有什么区别?

java - ORA-28040 : No Authentication protocol