2015-05-06 4 views
0

У меня есть 3 класса (приложение, контроллер, вид), где отображается окно с 3 JButtons на нем, контроллер предназначен для установки макета в зависимости от того, какая кнопка нажата, но это не работать, и я не понимаю, почему. Я думаю, это что-то глупое, но я новичок в обработке событий.Обработка событий JButton не работает

App

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

} 

Window

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

} 
+0

Что происходит, когда вы нажимаете кнопку? Выполняется ли actionPerformed fire? Получается ли название? –

+0

Ничего не происходит, даже название не устанавливается –

ответ

0

установить расположение в зависимости от нажатия кнопки, но он не работает

Вы должны вызвать revalidate/repaint на панели, которая содержит изменяемый макет.

container.setLayout(layout); 
.... 
    if (ali.equals("left")){ 
     layout.setAlignment(FlowLayout.LEFT); 
    } 
    ... 
    container.revalidate(); 
    container.repaint(); 
Смежные вопросы