【问题标题】:How to move 3 buttons inside a window如何在窗口内移动 3 个按钮
【发布时间】:2012-10-13 11:42:30
【问题描述】:

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

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

【问题讨论】:

  • 您的代码不可读。请正确缩进。
  • “在下面的代码中,当您单击左键时,我试图将三个按钮向左移动。” 为什么?
  • @AndrewThompson 这是一个家庭作业。我会这样标记它,但系统说该标签现在已弃用,我们不应该将其添加到帖子中。此外,我对我的帖子进行了大量编辑,以发布额外的信息,例如我为什么要做某事,所以我把它省略了。您将在下面找到完整的标签信息——该标签已过时,正在被删除。请不要将此标签添加到问题中。但不要在不查看问题是否需要清理的情况下将其删除。

标签: java swing layout jframe jbutton


【解决方案1】:

已经停止编译了,因为你删除了一个accolade,所以在方法的上面加上一个accolade "}":

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 组件的每次使用都必须通过 Event Dispatch Thread(缩写为 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 将其绘制在上面屏幕。

【讨论】:

  • 你和 Reimeus 给了我修复程序所需的所有信息。我不确定授予绿色复选框的协议是什么。请指教?
  • 这完全由您自行决定,请参阅here - 简而言之:“底线是接受您认为对您个人最有帮助的答案。”
  • 我已经接受以上作为答案,同时也投票给 Reimeus,因为这个答案更完整。它包括修复编译失败所需的修复以及帮助我​​根据需要移动按钮。
【解决方案2】:

您应该更新布局管理器以将组件向左或向右对齐。试试类似的东西;

((FlowLayout)getLayout()).setAlignment(FlowLayout.LEFT);

相反

【讨论】:

    【解决方案3】:

    您的代码无法编译,因为静态 main 方法出现在内部类 eventHandle 中。您只需将其移动到外部类Buttons 的类主体中即可修复。

    由于您拥有类级别的所有对象引用,因此您可以使用以下方法进行按钮对齐:

    flt.setAlignment(FlowLayout.RIGHT);
    buttonPanel.revalidate();
    ...
    

    在这里,您正在调整 FlowLayout 的布局对齐方式并重新验证以直观地反映面板上的更新更改。

    【讨论】:

    • 这个答案非常好,但没有包括编译失败的修复。感谢您的帮助,并希望我微薄的支持是某种安慰,因为我无法单击此选项上的绿色复选框以及 Linski 的帖子。再次感谢您!
    • @aaron 那不是问题 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多