gpt4 book ai didi

java - 额外的按钮不断出现

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:30 24 4
gpt4 key购买 nike

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

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

最佳答案

试试这个变体。评论很多,最重要的是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);
}
}

关于java - 额外的按钮不断出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10050491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com