2017-06-20 4 views
-1

私はまだJavaでコード化する方法を学んでいます。何かをしている複数のボタン

これは私が書いた現在のコードです。ご覧のように、ボタンとスライダーを備えたシンプルなパネルです。別のボタンを押すたびに別のコンソール出力を作りたい。だから私がBackを押すと、コンソールに戻ると書かれているはずです。スライダーで少しスクロールすると、コンソールに新しい値を書き込むはずです。そのようなもの。私はそれがactionListenerとactionPerformedで行われなければならないことを知っていますが、いくつかの実験の後には動作させることができませんでした。

import javax.swing.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Gui implements ActionListener { 

// Adding all the goods 
JFrame frame; 
JPanel panel; 
JButton endButton; 
JButton backButton; 
JButton calcButton; 
JSlider maxIterations; 
JLabel view; 

Gui() { 

// General 
this.frame = new JFrame("Trying my best, I swear"); 
this.frame.setSize(500, 500); 
this.frame.setVisible(true); 
this.panel = new JPanel(); 

// Buttons 
this.backButton = new JButton("Back"); 
this.calcButton = new JButton("Calc"); 
this.endButton = new JButton("End"); 
this.panel.add(this.endButton); 
this.panel.add(this.calcButton); 
this.panel.add(this.backButton); 
this.frame.add(this.panel); 

// Label 
JLabel label1 = new JLabel(); 
label1.setText("Space Holer"); 
panel.add(label1); 

// Slider 
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15); 
panel.add(slider); 
slider.setMinorTickSpacing(2); 
slider.setMajorTickSpacing(5); 
slider.setPaintTicks(true); 
slider.setPaintLabels(true); 

// Make the buttons do something 
this.endButton.addActionListener(this); 
} 

public void actionPerformed(ActionEvent ae) { 
System.out.println("End"); 
} 


public static void main(String[] args) { 
    @SuppressWarnings("unused") 
    Gui m = new Gui(); 
    } 
} 

答えて

0

その後、uは異なるコンソール出力を得る

calcButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("calcButton"); 
      // calculation for slider. 
      } 
     }); 

     backButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("backButton"); 
      } 
     }); 

、すべてのボタンにActionListenerを追加する必要があります。

0

すべてのコンポーネントを配置した後、jframeのsetVisibleを呼び出します。

各ボタンにActionListenerを追加します。 ActionListenerを持てないのでChangeListenerをスライダに追加します。

は、完全なコードを参照してください:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 

public class Gui implements ActionListener { 

    // Adding all the goods 
    JFrame frame; 
    JPanel panel; 
    JButton endButton; 
    JButton backButton; 
    JButton calcButton; 
    JSlider maxIterations; 
    JLabel view; 

    Gui() { 

     // General 
     this.frame = new JFrame("Trying my best, I swear"); 
     this.frame.setSize(500, 500); 
     this.panel = new JPanel(); 

     // Buttons 
     this.backButton = new JButton("Back"); 
     this.calcButton = new JButton("Calc"); 
     this.endButton = new JButton("End"); 
     this.panel.add(this.endButton); 
     this.panel.add(this.calcButton); 
     this.panel.add(this.backButton); 
     this.frame.add(this.panel); 

     // Label 
     JLabel label1 = new JLabel(); 
     label1.setText("Space Holer"); 
     panel.add(label1); 

     // Slider 
     JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15); 
     panel.add(slider); 
     slider.setMinorTickSpacing(2); 
     slider.setMajorTickSpacing(5); 
     slider.setPaintTicks(true); 
     slider.setPaintLabels(true); 

     // Make the buttons do something 
     this.endButton.addActionListener(this); 
     this.backButton.addActionListener(this); 
     this.calcButton.addActionListener(this); 
     slider.addChangeListener(e -> { 
      Object source = e.getSource(); 
      if (source instanceof JSlider) { 
       int value = ((JSlider) source).getValue(); 
       System.out.println(value); 
      } 
     }); 
     frame.pack(); 
     this.frame.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     Object source = ae.getSource(); 
     if (source instanceof JButton) { 
      String text = ((JButton) source).getText(); 
      System.out.println(text); 
     } 
    } 

    public static void main(String[] args) { 
     @SuppressWarnings("unused") 
     Gui m = new Gui(); 
    } 
} 
1

あなたは...

は、それが作成されますときActionEventに設定されているボタンのactionCommand財産を活用できます。あなたがボタンに自分自身をactionCommandを指定しない場合、それはあなたが

public class ButtonActionHandler implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     switch (e.getActionCommand()) { 
      case "Back": 
       System.out.println("Back"); 
       break; 
      case "Calc": 
       System.out.println("Calc"); 
       break; 
      case "End": 
       System.out.println("End"); 
       break; 
     } 
    } 

} 

ような何かを行うことができるようにActionListenerボタンが定義されているクラスの外部にある場合、これは良いですが、text値にデフォルト設定されますこれは、ボタンの参照にアクセスできないためです。あなたが同じことを行う(ツールバーボタンやメニュー項目を含む)のボタンの数を持っている可能性があるため、それは、また良いことだあなたは可能性が

... ActionListenersourceプロパティ

メイクの使用

public class ButtonActionHandler implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == backButton) { 
      System.out.println("Back"); 
     } else if (e.getSource() == calcButton) { 
      System.out.println("Calc"); 
     } else if (e.getSource() == endButton) { 
      System.out.println("End"); 
     } 
    } 

} 

ActionListenerあなたはボタンが

を定義しているところから親クラスへの内部クラスとして可能性が定義されている場合は...

これは便利です

ボタンは、あなたが

単一、隔離作業をできないところこれは良いです

endButton.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     System.out.println("End"); 
    } 
}); 

...ボタンに対して直接登録匿名クラスを使用して...

をご利用くださいAction APIを使用すると、自己完結型の作業単位を定義することができます。この作業単位は、ボタンを使用して完全に構成することができます。これは、メニューバー、ツールバー、およびいくつかのウィザードに含まれる「ファイルを開く」アクションなど、UIのさまざまな場所から実行できる反復アクションがある場合に便利です。拡張機能のためにキーバインディングAPIで使用することもできます

詳細はHow to use actionsを参照してください。

関連する問題