Java swing - redrawing are duplicate components (JButtons and JLabels) on resize

I have such a strange problem. My professor cannot duplicate it and the help he offers is minimal at best. So to the point. This code creates a JButton in the panel and adds a JFrame to the content panel:

public myPanel extends JPanel {
    static final long serialVersionUID = 1;
    public myPanel() {
        this.setPreferredSize(new Dimension(600, 40));
    }
    public void paintComponent(Graphics graphicsDrawer) {
        super.paintComponent(graphicsDrawer);
        this.add(new JButton("A Button"));
    }
}

      

Here is the GUI code

public class myGUI {
    public myGUI() {
    }
    public void showGUI() {
        JFrame frame = new JFrame("Issues!!!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new myPanel());
        frame.pack();
        frame.setVisible(true);
    }
}

      

Then the GUI is started by this class

// It is unnecessary to show here but I made a class that
// implements runnable that creates a myGUI object and calls
// showGUI
public static void main(String[] args) {
    RunnableGUI runGUI = new RunnableGUI();
    javax.swing.SwingUtilities.invokeLater(runGUI);
}

      

This is my code naked . However, I have highlighted this and this issue still persists even with minimal requirements. Below is the photo I am running into.

Start frame with button 1

Vertical resizing creates additional buttons

I tried literally everything I could find and think.

I think the frame redraws the button every time it is changed. However, the frame must be resizable due to the assignment requirements.

Running Windows 8 with JRE 7, 8 (I will download 6, but I don't think it helps)

+3


source to share


2 answers


public myPanel extends JPanel {
    static final long serialVersionUID = 1;
    public myPanel() {

        this.setPreferredSize(new Dimension(600, 40));
        this.add(new JButton("A Button"));
    }
    public void paintComponent(Graphics graphicsDrawer) {
        super.paintComponent(graphicsDrawer);

    }

}

      



+4


source


NEVER do this:

public void paintComponent(Graphics graphicsDrawer) {
    super.paintComponent(graphicsDrawer);
    this.add(new JButton("A Button"));  // ***** yikes! ****
}

      



The paintComponent method is for painting only . You don't have full control over whether it gets called or even if it gets called, and it can be called multiple times, which means adding a lot of buttons. In addition, you want to avoid code generation component, or any code that slows the flow of the program within the drawing method, because these methods are partly determined by the perceived responsiveness of your GUI, so you will need the drawing methods ( paint

and paintComponent

) to be a lean, mean and fast .

Add your button to your constructor or initialization method so that you can control how often this code is called.

+6


source







All Articles