JButton事件处理不起作用

时间:2015-05-06 23:13:28

标签: java events

我有3个类(App,Controller,View),其中显示一个窗口,上面有3个JButtons,控制器用于设置布局,具体取决于按下哪个按钮但它不起作用我不喜欢#39;不明白为什么。我想这是一个愚蠢的事情,但我是处理事件的新手。

应用

public class App {

    //fields
    Controller controller;

    //constructor
    public App (){
        new Controller();
    }

    //main method
    public static void main(String[] args) {
        new App();
    }
}

控制器

public class Controller implements ActionListener {

    //fields
    private MyFrame window;
    private JButton leftJButton;
    private JButton centralJButton;
    private JButton rightJButton;

    //constructor
    public Controller (){
        window = new MyFrame();
        window.setSize(400, 100);
        center();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        leftJButton = window.getLeftJButton();
        centralJButton = window.getCentralJButton();
        rightJButton = window.getRightJButton();
        leftJButton.addActionListener(this);
        centralJButton.addActionListener(this);
        rightJButton.addActionListener(this);
        window.setVisible(true);
    }

    //methods

    public void actionPerformed(ActionEvent e){
        if (e.getSource().equals(leftJButton)){
            window.setTitle("Left alignment");
            window.mySetAlignment("left");
        }
        else if (e.getSource().equals(centralJButton)){
            window.setTitle("Central alignment");
            window.mySetAlignment("center");
        }
        else if (e.getSource().equals(rightJButton)){
            window.setTitle("Right alignment");
            window.mySetAlignment("right");
        }
    }

    void center(){
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = window.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        window.setLocation( ( screenSize.width - frameSize.width ) / 2, ( screenSize.height - frameSize.height ) / 2 );
    }

}

窗口

public class MyFrame extends JFrame {

    //fields
    private static final long serialVersionUID = -1741328465427003178L;
    private JButton leftJButton, centralJButton, rightJButton;
    private static String title = "Default alignment";
    private FlowLayout layout;

    //constructor
    public MyFrame(){
    super(title);
    Container container = getContentPane();
    layout = new FlowLayout();
    container.setLayout(layout);
    leftJButton = new JButton("Left");
    centralJButton = new JButton("Center");
    rightJButton = new JButton("Right");
    container.add(leftJButton);
    container.add(centralJButton);
    container.add(rightJButton);
    }

    //methods

    public JButton getLeftJButton() {
        return leftJButton;
    }

    public JButton getCentralJButton() {
        return centralJButton;
    }

    public JButton getRightJButton() {
        return rightJButton;
    }

    public void setTitle(String title) {
        MyFrame.title = title;
    }

    public void mySetAlignment(String ali){
        if (ali.equals("left")){
            layout.setAlignment(FlowLayout.LEFT);
        }
        if (ali.equals("center")){
            layout.setAlignment(FlowLayout.CENTER);
        }
        if (ali.equals("right")){
            layout.setAlignment(FlowLayout.RIGHT);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

  

根据按下的按钮设置布局但不起作用

您必须在包含正在更改的布局的Panel上调用revalidate / repaint。

container.setLayout(layout);
....
    if (ali.equals("left")){
        layout.setAlignment(FlowLayout.LEFT);
    }
    ...
    container.revalidate();
    container.repaint();