2012-04-06 45 views
3

我是一種新的Java,並且我在每臺運行我的程序的計算機上遇到這個小問題。 我的代碼是額外的按鈕不斷出現

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

public class FrameTest2 { 


    public static void main(String[] args){ 
    String s = "Catch Torchic"; 

    MainMenu m = new MainMenu(s); 
    } 
} 

class MainMenu extends JFrame { 

    JButton autoa, manualb, createc, exitd; 

    public MainMenu(String s){ 
     super(s); 
     setBackground(new Color(247,247,111)); 
     setSize(640,600); 
     setVisible(true); 
     setResizable(false); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     Container contentPane = getContentPane(); 
     setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 
     CustomPanel panel = new CustomPanel(); 
     panel.setAlignmentX(Component.CENTER_ALIGNMENT); 
     panel.setMinimumSize(new Dimension(600,365)); 
     panel.setPreferredSize(new Dimension(600,365)); 
     panel.setMaximumSize(new Dimension(600,365)); 
     panel.setBackground(new Color(247,247,111)); 
     contentPane.add(panel); 
     JPanel btnPanel = new JPanel(); 
     btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS)); 
     btnPanel.setBackground(new Color(247,247,111)); 
     autoa = new JButton("AutoPlay"); 
     autoa.setAlignmentX(Component.CENTER_ALIGNMENT); 
     btnPanel.add(autoa); 

     manualb = new JButton("Manual Play"); 
     manualb.setAlignmentX(Component.CENTER_ALIGNMENT); 
     btnPanel.add(manualb); 

     createc = new JButton("Create Maze"); 
     createc.setAlignmentX(Component.CENTER_ALIGNMENT); 
     btnPanel.add(createc); 

     exitd = new JButton("Exit"); 
     exitd.setAlignmentX(Component.CENTER_ALIGNMENT); 
     btnPanel.add(exitd); 

     btnPanel.setAlignmentX(Component.CENTER_ALIGNMENT); 
     contentPane.add(btnPanel); 
     setContentPane(contentPane); 

     ButtonHandler handler = new ButtonHandler(this); 
     autoa.addActionListener(handler); 
     manualb.addActionListener(handler); 
     createc.addActionListener(handler); 
     exitd.addActionListener(handler); 
} 

} 

class ButtonHandler implements ActionListener{ 
    MainMenu mm; 

    public ButtonHandler(MainMenu mm){ 
     this.mm = mm; 
    } 

    public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == mm.autoa) 
     System.out.println("Clicked on Auto"); 
     else if(e.getSource() == mm.manualb) 
     System.out.println("Clicked on Manual"); 
     else if(e.getSource() == mm.createc) 
     System.out.println("Clicked on Create"); 
     else 
     System.exit(0); 
    } 

} 

class CustomPanel extends JPanel{ 
    public void paintComponent (Graphics painter){ 

    Image pic = Toolkit.getDefaultToolkit().getImage("logo.png"); 
    if(pic != null) painter.drawImage(pic, 105, 30, this); 

    } 
} 

該代碼產生到這一點:http://a3.sphotos.ak.fbcdn.net/hphotos-ak-ash4/318155_424290814251286_100000111130260_1871937_131988334_n.jpg

那裏總是在左上角的額外按鈕,我的問題是,我該如何去除呢?該按鈕以隨機間隔顯示,有時不顯示。

+0

1)*「This code results into ..」* Compilation errors。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 2)請爲代碼塊使用一致的邏輯縮進。 – 2012-04-07 00:20:50

+0

我試過你的代碼沒有改變,它的工作很好,就像你可以在這張圖中看到的一樣:http://hpics.li/acc7d7d。 – 2012-04-07 00:21:29

+0

我編輯了代碼並放置了所有的類。 我沒有使用Eclipse,netbeans之類的東西。我只用了Notepad ++。 – 2012-04-07 00:59:00

回答

2

試試這個變種。許多評論,最重要的是SHOUTING。請注意,當我把它變成工作代碼(或者更確切地說,代碼在這裏用於加載圖像)時,我從來沒有看到過你描述的問題。

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

import java.net.URL; 
import javax.imageio.ImageIO; 

public class FrameTest2 { 

    public static void main(String[] args) throws Exception { 
     final String s = "Catch Torchic"; 
     URL url = new URL("http://pscode.org/media/stromlo2.jpg"); 
     final Image image = ImageIO.read(url); 

     //Create the frame on the event dispatching thread 
     SwingUtilities.invokeLater(new Runnable(){ 

      @Override 
      public void run() { 
       new MainMenu(s, image); 
      } 

     }); 
    } 
} 

class MainMenu extends JFrame { 

    JButton autoa, manualb, createc, exitd; 

    public MainMenu(String s, Image image) { 
     super(s); 
     // do this after pack (if at all) 
     //setSize(640,600); 
     // do this after pack/setSize 
     //setVisible(true); 
     // don't do this in broken code 
     //setResizable(false); 
     // If you're going to spend space on GUI position.. 
     // setLocationRelativeTo(null); 
     // ..do it like this. 
     setLocationByPlatform(true); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     Container contentPane = getContentPane(); 
     contentPane.setLayout(new BorderLayout()); 
     CustomPanel panel = new CustomPanel(image); 
     panel.setAlignmentX(Component.CENTER_ALIGNMENT); 
     //panel.setMinimumSize(new Dimension(600,365)); 
     //panel.setPreferredSize(new Dimension(600,365)); 
     //panel.setMaximumSize(new Dimension(600,365)); 
     contentPane.add(panel); 
     JPanel btnPanel = new JPanel(); 
     btnPanel.setBorder(new EmptyBorder(5,5,100,5)); 
     btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS)); 
     // not needed for an SSCCE 
     //btnPanel.setBackground(new Color(247,247,111)); 
     autoa = new JButton("AutoPlay"); 
     autoa.setAlignmentX(Component.CENTER_ALIGNMENT); 
     btnPanel.add(autoa); 

     manualb = new JButton("Manual Play"); 
     manualb.setAlignmentX(Component.CENTER_ALIGNMENT); 
     btnPanel.add(manualb); 

     createc = new JButton("Create Maze"); 
     createc.setAlignmentX(Component.CENTER_ALIGNMENT); 
     btnPanel.add(createc); 

     exitd = new JButton("Exit"); 
     exitd.setAlignmentX(Component.CENTER_ALIGNMENT); 
     btnPanel.add(exitd); 

     // you already said that 
     //btnPanel.setAlignmentX(Component.CENTER_ALIGNMENT); 
     contentPane.add(btnPanel, BorderLayout.SOUTH); 
     setContentPane(contentPane); 

     ButtonHandler handler = new ButtonHandler(this); 
     autoa.addActionListener(handler); 
     manualb.addActionListener(handler); 
     createc.addActionListener(handler); 
     exitd.addActionListener(handler); 

     // CAUSE THE COMPONENTS TO BE PROPERLY LAID OUT. 
     pack(); 
     setSize(640,600); 
     setVisible(true); 
    } 

} 

class ButtonHandler implements ActionListener{ 
    MainMenu mm; 

    public ButtonHandler(MainMenu mm){ 
     this.mm = mm; 
    } 

    public void actionPerformed(ActionEvent e){ 
     // for clarity, even single line statements 
     // in these should be enclosed in {} 
     if(e.getSource() == mm.autoa) 
      System.out.println("Clicked on Auto"); 
     else if(e.getSource() == mm.manualb) 
      System.out.println("Clicked on Manual"); 
     else if(e.getSource() == mm.createc) 
      System.out.println("Clicked on Create"); 
     else 
      System.exit(0); 
    } 

} 

class CustomPanel extends JPanel{ 

    Image pic; 

    CustomPanel(Image pic) { 
     this.pic = pic; 
    } 

    public void paintComponent (Graphics painter){ 
     // DO NOT TRY TO READ IMAGES IN PAINT! 
     //Image pic = Toolkit.getDefaultToolkit().getImage("logo.png"); 
     if(pic != null) painter.drawImage(pic, 105, 30, this); 
    } 
} 
+0

另外'Toolkit.getDefaultToolkit()。getImage(「logo.png」);'很可能會在部署時失敗,因爲代碼試圖訪問(大概是什麼)一個'嵌入式'的應用程序。資源就好像它是一個'File'一樣。這些必須由'URL'訪問。 – 2012-04-07 07:06:15

+0

非常感謝您的先生! 問題: 爲什麼要從URL中獲取圖片?我不能和班級一起嗎? pack()獲取要正確佈局的組件? – 2012-04-07 07:25:44

+0

1)'URL'可以指向Jar文件中的條目 - 實際上,它是訪問Jar中的資源(使用Java)的唯一方式。使用File.toURI()。toURL()',你本地文件系統上的任何'File'路徑也可以變成一個URL。簡而言之,URL更加通用。 2)它也應該這樣工作。使用'Class.getResource(String)'獲取URL。 3)是的。我認爲這可能是導致佈局問題,但在繪製方法中讀取圖像從來不是一個好主意。 – 2012-04-07 07:32:24

1

我認爲這是缺少contentPane.

//setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));// 

還有就是內容窗格中的多餘設置。

//setContentPane(contentPane); 

後 「沒了:」

困難。setVisible(true)移動到最後,僅調用一個佈局。 嘗試在內容窗格中沒有兩個面板(僅一個):

Container contentPane0 = getContentPane(); 
    Container contentPane = new JPanel(); 
    contentPane0.add(contentPane); 
+0

nope,額外的按鈕仍然以隨機間隔出現 – 2012-04-07 02:07:12