额外按钮不断出现

时间:2012-04-06 23:49:00

标签: java swing

我是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

左上角总是有额外的按钮,我的问题是,如何删除它?按钮以随机间隔出现,有时则不然。

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:1)

我认为缺少contentPane.

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

此外,内容窗格还有一个多余的设置。

    //setContentPane(contentPane);

“nope:”之后

困难。setVisible(true)移到最后,仅调用一个布局。 在内容窗格中没有两个面板(只有一个)的情况下尝试:

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