2011-04-02 7 views
0

作業ボタンを作成してこのコードに適合させたいのですが、私は以下のとおりです。私の問題はどこでも私はいつも私に奇妙なエラーを与えるactionlistenersや他のものを置くことです。私はそれを押すとテキストを印刷するために私のボタンをb1にしたい。私は本当に助けに感謝します。イム初心者です。コード内にボタンを挿入できません

public class Simulator { 

public static void main(String[] args) { 

    boolean suc1,suc2,suc3,suc4,suc5,suc6,suc7; 

     JFrame f = new JFrame("Simulator"); 
     f.setSize(500, 400); 
     f.setResizable(false); 
     f.setLayout(null); 
     f.setVisible(true); 
     JButton b1 = new JButton("Start"); 
     JButton b2 = new JButton("Clear"); 
     JButton b3 = new JButton("Find"); 
     JButton b4 = new JButton("Stop"); 
     b1.setBounds(20,335,80,25); 
     b2.setBounds(110,335,80,25); 
     b3.setBounds(200,335,80,25); 
     b4.setBounds(395,335,80,25); 
     f.add(b1); 
     f.add(b2); 
     f.add(b3); 
     f.add(b4); 

} 
} 
+0

だ、あなたはそれはあなたをどのようなエラーを与えない – user634618

+2

を使用しているプラ​​ットフォームを明確にするために、質問にタグを付けなければなりません? – MByD

+1

すぐに役立つように、[SSCCE](http://pscode.org/sscce.html)(輸入品を含む)を投稿してください。あなたの将来の正気のために、 'ヌル'レイアウトは避けてください。 –

答えて

-1

JButton b1 = new JButton("Start"); 
    b1.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("Button b1 was pressed"); 

     } 
    }); 

EDITしてみてください:

は私のために正常に動作するようです。ここではフルmain()メソッドのJavaのように見えますが

public static void main(String[] args) { 

     boolean suc1,suc2,suc3,suc4,suc5,suc6,suc7; 

     JFrame f = new JFrame("Simulator"); 
     f.setSize(500, 400); 
     f.setResizable(false); 
     f.setLayout(null); 
     f.setVisible(true); 
     JButton b1 = new JButton("Start"); 
     b1.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("Button b1 was pressed"); 

      } 
     }); 
     JButton b2 = new JButton("Clear"); 
     JButton b3 = new JButton("Find"); 
     JButton b4 = new JButton("Stop"); 
     b1.setBounds(20,335,80,25); 
     b2.setBounds(110,335,80,25); 
     b3.setBounds(200,335,80,25); 
     b4.setBounds(395,335,80,25); 
     f.add(b1); 
     f.add(b2); 
     f.add(b3); 
     f.add(b4); 
    } 

enter image description here

+0

私はあなたのメソッドを使用すると、私はフレームにそのボタンを置くことができません – Ramist

+2

どうしてですか?エラーメッセージは何ですか? –

+0

私はエラーを出さないようにしましたが、私のボタンはフレームに表示されません、それはちょうど消えます。私はそのボタンにsetBoundsを使用することも、フレームにf1.add(b1)を追加することもできません。 – Ramist

0
import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class Simulator { 

    public static void main(String[] args) { 

     Runnable r = new Runnable() { 
      public void run() { 
       JFrame f = new JFrame("Simulator"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       // Unnecessary, just set the preferred size of the 
       // custom(?) component that renders the simulation, 
       // use layouts, & pack() the frame when done. 
       //f.setSize(500, 400); 

       f.setResizable(false); 

       // Bad idea. Will cause trouble until fixed. 
       //f.setLayout(null); 

       // Play with the numbers for different effects. 
       // If the edge of the buttons is not supposed to align 
       // with the 'simulation' panel, borders can be added to 
       // the elements to create that effect. 

       JPanel gui = new JPanel(new BorderLayout(5,5)); 
       gui.setBorder(new EmptyBorder(10,30,10,30)); 

       JPanel simulation = new JPanel(); 
       simulation.setPreferredSize(new Dimension(450,300)); 
       simulation.setBackground(Color.WHITE); 
       gui.add(simulation, BorderLayout.CENTER); 

       JPanel buttonPanel = new JPanel(new BorderLayout(50,5)); 
       gui.add(buttonPanel, BorderLayout.SOUTH); 

       JPanel westButtons = new JPanel(new GridLayout(1,0,10,10)); 

       JButton b1 = new JButton("Start"); 
       JButton b2 = new JButton("Clear"); 
       JButton b3 = new JButton("Find"); 
       JButton b4 = new JButton("Stop"); 

       westButtons.add(b1); 
       westButtons.add(b2); 
       westButtons.add(b3); 

       buttonPanel.add(westButtons, BorderLayout.WEST); 

       buttonPanel.add(b4, BorderLayout.EAST); 

       f.setContentPane(gui); 

       f.pack(); 
       f.setVisible(true); 
      } 
     }; 
     // ensure the UI is built on the EDT. 
     SwingUtilities.invokeLater(r); 
    } 
} 

Sceenshot of GUI

+0

そして、あなたは 'b1'に' ActionListener'を使用することに関するOPの質問には対処していないようです。 –

+0

@Bala R Yeah ..私は '** ActionListener'あなたのコードがALをカバーしているので、私はコードを残しておきます。レイアウトが達成されるかもしれない。 –

関連する問題