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

左上角总是有多余的按钮,我的问题是,如何删除它?该按钮以随机间隔出现,有时不会。

4

2 回答 2

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);
    }
}
于 2012-04-07T07:04:17.030 回答
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);
于 2012-04-07T01:22:20.480 回答