2011-04-02 77 views
0

我想製作工作按鈕,並將其放在下面的代碼中。我的問題是,我把任何地方,我把行動者或其他東西,它總是給我奇怪的錯誤。我想讓按鈕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

雖然看起來像Java,你應該標記問題澄清你正在使用 – user634618 2011-04-02 16:27:27

+2

什麼平臺,它給你什麼錯誤? – MByD 2011-04-02 16:38:31

+1

爲了更快提供更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)(&將包括導入)。爲了您未來的理智,避免'null'佈局。 – 2011-04-02 17:20:18

回答

-1

嘗試

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

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

     } 
    }); 

編輯:

似乎爲我工作的罰款。下面是完整的main()方法

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 2011-04-02 16:36:43

+2

爲什麼不呢?什麼是錯誤信息? – 2011-04-02 16:38:10

+0

我知道它不會給我錯誤,但然後我的按鈕不顯示在框架中,它只是消失。我不能使用setBounds到該按鈕,也不會將它添加到框架與f1.add(b1); – Ramist 2011-04-02 16:42:32

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

你似乎沒有解決OP關於使用'ActionListener'作爲'b1'的問題。 – 2011-04-02 18:05:39

+0

@Bala R Yeah ..我完全**錯過了**關於ActionListener的一點。 - 這很有諷刺意味,因爲這是問題的癥結所在!我認爲現在沒有多少意義了,因爲你的代碼覆蓋了AL,我會離開我的代碼,因爲它可能會向OP展示如何佈局可能會實現。 – 2011-04-02 18:25:09