1

In my source code,

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Object6 extends JFrame {
    JButton p = new JButton("Y");
    JButton n = new JButton("N");

    public Object6(){
        setSize(1280,800);
        setVisible(true);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void paint(Graphics g){
        p.setLocation(590,500);
        n.setLocation(590,550);
        add(p);
        add(n);
        p.setSize(100,50);
        n.setSize(100,50);
        g.drawString("Does statement 6 apply?", 100, 100);
    }
    public static void main(String[]args){
        new Object6();
    }
}

Button "p" and the string appears; however, button "n" appears only if i click the space where it is supposed to be. When i remove g.drawString("Does statement 6 apply?", 100, 100); , both buttons appear simultaneously.

How can I add g.drawString("Does statement 6 apply?", 100, 100); while both buttons appear simultaneously?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
J.W.
  • 9
  • 2
  • Both buttons appear when I run the program with this `drawString`. – Maroun Oct 27 '13 at 09:07
  • 4
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Oct 27 '13 at 09:11
  • 2
    Also: **1.** Swing components must be created and accessed in the [event dispatch thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). **2.** Don't override `paint()`. If you need [custom painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/), override `paintComponent()` of a contained component instead. **3.** Don't modify components in painting code. – kiheru Oct 27 '13 at 09:39

3 Answers3

3
  1. You should never modify the UI from within any paint method, this could cause the paint method to be called again and again and again ... Until it sucks up the CPU cycles
  2. You should add your buttons within the constructor
  3. You should make appropriate use of a layout manager
  4. Painting in a Swing is made of a number of chained calls which build up to paint the final output. In order to ensure that painting is performed correctly, you must call super.paint
  5. You should avoid overriding paint of top level containers, you should avoid overriding paint generally. Instead use something like a JPanel and override its paintComponent method

Take a look at

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

your paint method should like this...

public void paint(Graphics g){
    super.paint(g);
    g.drawString("Does statement 6 apply?", 100, 100);
}

i think it solve your problem

subash
  • 3,116
  • 3
  • 18
  • 22
-1

Thanks for everyone who answered. This is the final(now working) code for anyone who has the same problem:

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Object6 extends JFrame {
    JPanel a = new JPanel();
    JButton p = new JButton("Y");
    JButton n = new JButton("N");
    Color c = new Color(0x4BBCF8);
    JLabel b = new JLabel("Does statement 6 apply?");

    public Object6(){
        setVisible(true);                               
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        setSize(1280,800);
        setBackground(c);
        panel();
    }
    public void panel(){
        a.setLocation(540,600);
        a.setSize(200,50);
        add(a);
        a.setLayout(null);

        p.setLocation(0,0);
        n.setLocation(100,0);
        p.setSize(100,50);
        n.setSize(100,50);
        a.add(p);
        a.add(n);

        int e = b.getText().length();
        b.setLocation((1280-e*8)/2,100);
        b.setSize(8*e,16);
        add(b);
    }
    public static void main(String[]args){
        new Object6();
    }
}
J.W.
  • 9
  • 2