GUI: JButtons aren't visible until clicked
I am making a simple GUI that will graph one of three parent functions (linear, quadratic, cubic) onto a graph based on the user's selection.
Looking at my code, I don't really have an issue with anything showing up except for the quadratic and cubic buttons. For some reason, they are invisible until you click the exact spot that they are located. I've never had this issue... Any help is greatly appreciated!
Code Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class main extends JFrame{
functions functions = new functions();
int z = 11;
int y = 11;
JPanel panel = new JPanel();
JButton linear, quadratic, cubic;
public main(){
setLayout(new FlowLayout());
panel = new JPanel();
panel.setLayout(new GridLayout(1,3));
add(panel);
linear = new JButton("Linear");
panel.add(linear);
quadratic = new JButton("Quadratic");
panel.add(quadratic);
cubic = new JButton("Cubic");
panel.add(cubic);
}
public void paint(Graphics g){
for(int x = 0; x<20;x++){
g.setColor(Color.DARK_GRAY);
g.drawLine(z, 0, z, 500);
z += 20;
}
for(int x = 0; x<20;x++){
g.setColor(Color.DARK_GRAY);
g.drawLine(0, y, 500, y);
y += 20;
}
g.setColor(Color.BLACK);
g.drawLine(250, 0, 250, 500);
g.setColor(Color.BLACK);
g.drawLine(0, 250, 500, 250);
}
public static void main(String args[]){
main frame = new main();
frame.setBackground(Color.WHITE);
frame.setSize(500,500);
frame.setVisible(true);
frame.setResizable(false);
frame.setTitle("Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Re: GUI: JButtons aren't visible until clicked
What does the functions class do?
The code works for me when I comment out the functions line
Just recompiled and now it doesn't work the same way.
If I rename the paint method to paintXXX the buttons display OK.
What do you want the paint method to do? Where is it to draw? Where do you want the buttons to show?
Re: GUI: JButtons aren't visible until clicked
Quote:
Originally Posted by
Norm
What does the functions class do?
The code works for me when I comment out the functions line
The functions class draws the parent functions onto the graph.. It can be removed for this example, but it still shows up with the quadratic and cubic buttons hidden.
Quote:
Originally Posted by Norm
If I rename the paint method to paintXXX the buttons display OK.
It takes away the graph for me, but reveals the buttons okay as well.
Quote:
Originally Posted by Norm
What do you want the paint method to do? Where is it to draw? Where do you want the buttons to show?
The paint method is supposed to create the graph, by creating 40 dark gray lines.. It is also supposed to draw 2 lines of origin in the color black..
Apparently the paint method hides the two buttons. If it's commented out, then the buttons are revealed correctly. What does this mean?
Re: GUI: JButtons aren't visible until clicked
Quote:
Apparently the paint method hides the two buttons. If it's commented out, then the buttons are revealed correctly. What does this mean?
Either you paint on the frame or you layout components on the frame. Not both
Create a JPanel extension class, put your paintComponent method in that and add that to the frame.
Re: GUI: JButtons aren't visible until clicked
Quote:
Originally Posted by
Norm
Either you paint on the frame or you layout components on the frame. Not both
Create a JPanel extension class, put your paintComponent method in that and add that to the frame.
Alright, I'm going to do that now. Can you explain why you can't both layout components and paint components on a JFrame?
Re: GUI: JButtons aren't visible until clicked
Quote:
why you can't both layout components and paint components on a JFrame?
I suppose it is a question of who paints last. If the layout of the components is done first, and the painting after, the painter will win. If the painter goes first then the layout will win.
Doing both on the same component doesn't make any sense to me.