我几个月前一直在重建一个旧的MS-paint复制猫,Swing再一次给了我一些老问题.其中一个让我和其他几个人现在难过了几天.我有一个自定义JFrame,我用来放置我的所有组件,以及自定义JButtons,用于表示用户可以选择的各种颜色和工具.现在,问题是我的程序没有显示我的大多数按钮.这是我的ColorButton类:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class ColorButton extends JButton implements ActionListener
{
private static final long serialVersionUID = 1L;
Color color;
public ColorButton(Color color, String name)
{
this.color = color;
setButtonIcon(name);
}
private void setButtonIcon(String name)
{
ImageIcon icon = new ImageIcon("images/" + name);
setIcon(icon);
System.out.println("Icon set.");
}
@Override
public void actionPerformed(ActionEvent event)
{
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,这个类只是一个更好的按钮,我可以重用并动态放置在主框架上.我设置它,以便它需要一个颜色(设置用户的光标颜色)和一个字符串(为了从资源文件夹中获取ImageIcon).这是我必须添加所有内容的JFrame.
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class PaintFrame extends JFrame // Frame to place all items on
{
private static final long serialVersionUID = 1L;
public PaintFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the program to close if the user wishes
setVisible(true); // Set screen to visible
setSize(1150, 650); // Set screen size
setLocationRelativeTo(null); // Set frame to start in middle of screen
setResizable(false);
setLayout(new BorderLayout()); // Set a suitable layout for the panel
setTitle("Paint 2.0");
addComponents(); // Add everything to the JFrame
}
// List to hold all of the color buttons
ArrayList<ColorButton> colorButtons;
private void createColorButtons() // Create and add all color buttons
{
colorButtons = new ArrayList<ColorButton>();
colorButtons.add(new ColorButton(Color.BLACK, "black_paint.png"));
colorButtons.add(new ColorButton(Color.WHITE, "white_paint.png"));
// colorButtons.add(new ColorButton(new Color(22, 10, 255), "blue_paint.png"));
// colorButtons.add(new ColorButton(new Color(163, 92, 45), "brown_paint.png"));
// colorButtons.add(new ColorButton(new Color(19, 175, 50), "dark_green_paint.png"));
// colorButtons.add(new ColorButton(new Color(22, 255, 34), "green_paint.png"));
// colorButtons.add(new ColorButton(new Color(58, 209, 255), "light_blue_paint.png"));
// colorButtons.add(new ColorButton(new Color(255, 84, 33), "orange_paint.png"));
// colorButtons.add(new ColorButton(new Color(255, 86, 243), "pink_paint.png"));
// colorButtons.add(new ColorButton(new Color(168, 11, 121), "purple_paint.png"));
// colorButtons.add(new ColorButton(new Color(255, 0, 0), "red_paint.png"));
// colorButtons.add(new ColorButton(new Color(255, 241, 45), "yellow_paint.png"));
colorButtons.add(new ColorButton(Color.WHITE, "eraser.png"));
}
JPanel colorButtonPanel;
private void addComponents() // Add all the components to the screen
{
createColorButtons();
colorButtonPanel = new JPanel();
colorButtonPanel.setLayout(new BoxLayout(colorButtonPanel,
BoxLayout.X_AXIS));
colorButtonPanel.setBorder(new TitledBorder("Colors & Tools"));
for (ColorButton button : colorButtons)
{
colorButtonPanel.add(button);
}
// Add Panels
add(BorderLayout.SOUTH, colorButtonPanel);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,正如您所看到的,该类继承自JFrame,并且所有组件都添加到框架中addComponents().我认为问题所在的方法是createColorButtons().现在,所有未显示的按钮都被注释掉,而其他按钮是唯一有效的按钮.这里的问题非常具体.如果我用任何不起作用的按钮启动程序(即任何colorButtons.add(foo)被注释掉的按钮),那么框架就会显示为空.没有任何按钮出现,只是一个空白框架.但是,如果我将所有这些按钮注释掉的程序启动程序,那么我仍然可以获得三个按钮.
与这些按钮相比,唯一不同的是,不显示的按钮使用自定义创建的颜色,而其他按钮使用Java API中包含的预设颜色.我想不出有什么理由会导致任何问题,但如果你不这么认真,请告诉我.如果您需要更多详细信息,代码或其他任何您可以想到的可能有助于您回答问题的信息,请与我们联系.谢谢.
添加了增加措施的主要方法:
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Start // Start the program
{
public static void main(String[] args)
{
try // Get default system look & feel
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (
UnsupportedLookAndFeelException |
ClassNotFoundException |
InstantiationException |
IllegalAccessException e)
{
e.printStackTrace();
}
new PaintFrame();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:是的,所有图像都在正确的文件夹中,并正确命名.不用担心.
public PaintFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // ***** telling Java to render the GUI
setSize(1150, 650);
setLocationRelativeTo(null);
setResizable(false);
setLayout(new BorderLayout());
setTitle("Paint 2.0");
addComponents(); // Add everything to the JFrame **after** rendering it! **
}
Run Code Online (Sandbox Code Playgroud)
不要叫setVisible(true),直到之后将所有组件的GUI.否则,无法保证在调用后添加的组件setVisible(true)将被呈现.
| 归档时间: |
|
| 查看次数: |
349 次 |
| 最近记录: |