【问题标题】:Java program wont react till the window gets resizedJava程序在窗口调整大小之前不会做出反应
【发布时间】:2017-12-11 22:55:30
【问题描述】:

我有一个简单的 java 程序,当我使用 eclipse 运行它时,它会显示我为布局设置的 3 个 JButton。按钮设置为更改布局的对齐方式。所以你按左对齐左对齐右对齐,居中对齐居中。

当按钮执行此操作时,窗口中的对齐方式不会改变,直到您调整它的大小。

我尝试更新 jdk 和 eclipse 并没有产生任何影响,我看不出代码本身有问题。

有人知道这是为什么吗?

导入 javax.swing.JFrame;

public class Main {
    public static void main(String []args){

        Layout_buttonsAndActionEvents layout = new Layout_buttonsAndActionEvents();

        layout.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        layout.setSize(300,300);
        layout.setVisible(true);



    }

}



import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Layout_buttonsAndActionEvents extends JFrame {

    private static final long serialVersionUID = 1L;
    private JButton leftButton;
    private JButton rightButton;
    private JButton centerButton;

    private FlowLayout layout;
    private Container container;

    public Layout_buttonsAndActionEvents(){
        super("The Title");
        layout = new FlowLayout();
        container = new Container();
        setLayout(layout);


        leftButton = new JButton("Left");
        add(leftButton);
    //Align to the left
        leftButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent event){
                layout.setAlignment(FlowLayout.LEFT);
                layout.layoutContainer(container);
            }
        });


    centerButton = new JButton("Center");
    add(centerButton);

    //Align to the right
         centerButton.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent event){
                    layout.setAlignment(FlowLayout.CENTER);
                    layout.layoutContainer(container);
                }
            });

         rightButton = new JButton("Right");
        add(rightButton);

        //Align to the right
            rightButton.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent event){
                    layout.setAlignment(FlowLayout.RIGHT);
                    layout.layoutContainer(container);
                }
            });



    }


}

【问题讨论】:

  • 更改对齐后是否重新打包窗口?
  • 你能发布你的代码吗?
  • 请不在 cmets 中。您可以编辑您的问题并将其添加到那里。
  • 抱歉第一次在堆栈上问任何东西

标签: java layout jbutton


【解决方案1】:

因为您正在将按钮添加到 JFrame 的内容中 窗格(通过 add() 方法,这实际上是对 getContentPane().add() 在幕后)你需要调用 revalidate() 内容窗格。

在三个动作监听器中,改变:

    layout.setAlignment(FlowLayout.XXX);
    layout.layoutContainer(container);

到:

    layout.setAlignment(FlowLayout.XXX);
    getContentPane().revalidate();

此外,您可以删除对名为“容器”的变量的所有引用,因为它在您的示例中没有任何作用。

【讨论】:

  • 谢谢,这已经解决了
【解决方案2】:

当您调整 JFrame 的大小时,它会使用新的大小和布局重新验证。同样,您必须在添加更改后的布局后验证并重新绘制框架以应用视觉差异。

确保在更改布局后按此顺序调用这些方法:

setLayout(layout);
repaint();
revalidate();

【讨论】:

  • 这会被添加到主类或布局类中吗?
  • @Yzma 它将进入动作监听器。确保将“frame”替换为 JFrame 对象的名称。
  • 它仍然只是在调整窗口大小后更改对齐方式,我确定我已经将其更改为您所说的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 2014-09-03
  • 2013-07-15
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多