So I am trying to put an image on a JButton
by the means of the JButton
constructor
JButton button = new JButton(ImageIcon image)
I have a few icons are defined as
ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
ImageIcon SaveIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\save.jpeg");
ImageIcon CutIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\cut.jpeg");
ImageIcon CopyIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\copy.jpeg");
ImageIcon PasteIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\paste.jpeg");
The icon files are in a folder called
C:\Users\Wonseok\Documents\CompSciClass\Homework Files\icons.
The buttons that these icons are on are defined as
JButton OpenButton = new JButton(OpenIcon);
JButton SaveButton = new JButton(SaveIcon);
JButton CutButton = new JButton(CutIcon);
JButton CopyButton = new JButton(CopyIcon);
JButton PasteButton = new JButton(PasteIcon);
I have these in a JPanel
.
My problem is that the icons aren't showing properly on the JButtons
, and I have no idea why. The buttons show up as small, thin, blank rectangles without an image on it. If you want a picture, please tell me how to post one from my computer. Is this my computer being annoying and glitchy? Or is there something wrong with my coding?
The pertinent code is
JPanel ButtonBar = new JPanel(new FlowLayout());
JPanel FileActions = new JPanel(new GridLayout(1, 2));
ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
ImageIcon SaveIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\save.jpeg");
JButton OpenButton = new JButton(OpenIcon);
JButton SaveButton = new JButton(SaveIcon);
JPanel TextActions = new JPanel(new GridLayout(1, 3));
ImageIcon CutIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\cut.jpeg");
ImageIcon CopyIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\copy.jpeg");
ImageIcon PasteIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\paste.jpeg");
JButton CutButton = new JButton(CutIcon);
JButton CopyButton = new JButton(CopyIcon);
JButton PasteButton = new JButton(PasteIcon);
public basic_text_editor()
{
super("Text Editor");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.WHITE);
setLayout(new FlowLayout(FlowLayout.LEFT));
FileActions.add(OpenButton);
FileActions.add(SaveButton);
TextActions.add(CutButton);
TextActions.add(CopyButton);
TextActions.add(PasteButton);
ButtonBar.add(FileActions);
ButtonBar.add(TextActions);
add(ButtonBar);
}