简体   繁体   中英

GUI programming, FlowLayout blocking other things on JFrame(?)

The idea of the program is that I have some buttons and an icon SOMEWHERE on the frame. I want the buttons to change the color. I'm only worried about making all the elements show up right now. If I comment out lines 11-13, I see "hello," printed out, with a red circle on top of it. Otherwise, I just have the button "red" without "hello" or my red circle. So here's my code:

import javax.swing.*; 
import java.awt.*;
import java.awt.geom.*;

public class ButtonTester 

{
    public static void main (String[] args) 
    {  
        JFrame frame = new ButtonFrame(); 
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton redButton = new JButton("Red");
        frame.add(redButton);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true); 
    }
}

class ButtonFrame extends JFrame 
{  
    public static final int DEFAULT_WIDTH = 300;  
    public static final int DEFAULT_HEIGHT = 200;  

    public ButtonFrame() 
    {   
        setTitle("Hello"); 
       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
       ButtonPanel panel = new ButtonPanel(); 
       add(panel); 
    } 
} 

class ButtonPanel extends JPanel 
{ 
    public void paintComponent(Graphics g) 
    {   
        super.paintComponent(g); 
        Graphics2D g2 = (Graphics2D) g;  
        g2.drawString("Hello !", 100, 100);
        Icon ico = new ColorIcon(32);
        ico.paintIcon(null, g, 75, 75);
    } 
} 

I'm 90% sure the problem is lines 11-13, but I'm not sure what to change to make everything visible.

Your problem is that your ButtonPanel's size is 0. Have it override getPreferredSize() and you will see what I mean:

class ButtonPanel extends JPanel {
   private static final int PREF_W = 150;
   private static final int PREF_H = PREF_W;

   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.drawString("Hello !", 100, 100);
      // !! Icon ico = new ColorIcon(32);
      // Icon ico = new ImageIcon();
      // ico.paintIcon(null, g, 75, 75);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }
}

Also as an unrelated aside, why are you creating an Icon inside of the paintComponent method? This doesn't make sense to me and would only serve to needlessly slow your graphics down.

Edit
You state:

Ok, I see the difference after overriding getPreferredSize() But what would be the "better" or "correct" way to create the icon? I'm just trying to follow the directions for an exercise out of a Java textbook: Exercise 4.14. Write a program that shows a frame with three buttons labeled "Red", "Green", and "Blue", and a label containing an icon showing a circle that is initially red. As the user clicks the buttons, the fill color of the circle should change. When you change the color, you need to invoke the repaint method on the label. The call to repaint ensures that the paintIcon method is called so that the icon can be repainted with the new color.

You need to think on this a different way. Myself I'd create three ImageIcons one for a blue circle, one for red, and one for green. I'd then display the ImageIcon in a JLabel on my JFrame. I'd change the color by simply swapping the label's icons via its setIcon(...) method. I wouldn't worry about futzing with paintComponent(...) but rather would try to solve this in as simple a fashion as possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM