- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 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);
}
}
左上角总是有一个多余的按钮,我的问题是,我该如何删除它?按钮随机出现,有时不出现。
最佳答案
试试这个变体。评论很多,最重要的是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/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!