Note that there are some explanatory texts on larger screens.

plurals
  1. POJava GUI, painting images on button click
    primarykey
    data
    text
    <p>I'm having some problems creating a GUI. I want it so what when a button is pushed, the two images will appear on the screen side by side. There will be many more images than just two in the future, possibly hundreds (many very small images) I can add in a button in the Main class but then I can't add an action listener. And when I try and add a button in my GUI class nothing shows up. Also in the action listener I cant call the paint function. Any help would be great, thanks!</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import java.util.Random; import javax.imageio.*; import javax.swing.*; public class Gui extends JFrame { private static JButton startButton; BufferedImage img; BufferedImage img2; public void paint(Graphics g) { g.drawImage(img, 0, 0, null); g.drawImage(img2, 30, 0, null); } public Gui() { try { img = ImageIO.read(new File("res/a.png")); img2 = ImageIO.read(new File("res/b.png")); } catch (IOException e) { } startButton = new JButton("tessttt"); getContentPane().add(startButton); //this doesn't show up Events e = new Events(); startButton.addActionListener(e); } public Dimension getPreferredSize() { //sets size of screen if (img == null) { return new Dimension(100,100); } else { // return new Dimension(img.getWidth(null), img.getHeight(null)); //sets size to one image //// change to all images return new Dimension(500,500); } } public static void main(String[] args) { JFrame f = new JFrame("Load Image Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(640, 480); f.add(new Gui()); //this is the only time I can call this and the images will show up JPanel panel = new JPanel(); panel.add(new JLabel("Hello")); f.add(panel); //this will show up but i can't add an action listener if this was a button f.setLayout(new FlowLayout()); f.setVisible(true); } public class Events implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { getContentPane.add(new Gui()); //f.add(newGui()); //this also gives errors } } } } </code></pre> <p>I got the button to work. In the main class I did this</p> <pre><code> startButton = new JButton("tessttt"); f.add(startButton); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { f.add(new Gui()); //f.repaint(); } }); </code></pre> <p>SO now the question is how do I call paint? either of those do not work.</p> <p>first few lines of the error I get</p> <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container at java.awt.Container.checkNotAWindow(Unknown Source) at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at javax.swing.JFrame.addImpl(Unknown Source) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload