我对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
左上角总是有多余的按钮,我的问题是,如何删除它?该按钮以随机间隔出现,有时不会。