Reputation: 161
I'm having troubles calling a JPanel I made into a JFrame.
The JPanel is called "SubnetPanel" ==>
SubnetPanel panel = new SubnetPanel(String a, String b, String c);
In my JFrame, I made a button.
private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String a = JOptionPane.showInputDialog(null, "Geef de naam in : ",
"Naam", 1);
String b = JOptionPane.showInputDialog(null, "Geef het netwerkadres in : ",
"Netwerkadres", 1);
String c = JOptionPane.showInputDialog(null, "Geef het subnetmask in : ",
"Subnetmask", 1);
this.add(new SubnetPanel(a,b,c) {
@Override
public void paintComponent( Graphics g ) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Line2D line = new Line2D.Double(10, 10, 40, 40);
g2.setColor(Color.blue);
g2.setStroke(new BasicStroke(10));
g2.draw(line);
}
});
this.setVisible( true );
}
But when I execute my JFrame and click the button, the JFrame does not appear. Can anyone help me with this?
Thanks!
Upvotes: 0
Views: 291
Reputation: 46841
May be you forget to add the ActionListner
on the JButton
.
Try in this way
JButton jButton = new JButton("Click"); // Your actual button is here
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jButtonActionPerformed(e);
}
});
Upvotes: 0
Reputation: 324098
The basic code for adding (or removing) a component from a visible frame is:
panel.add(...);
panel.revalidate();
panel.repaint();
In your case the "panel" would be the content pane of your JFrame.
Also, when you do custom painting, you also need to override the getPreferredSize()
method of the panel, so the layout manager can use this information to set the size/location of the panel.
Upvotes: 1
Reputation: 50021
Although you've added the panel to the frame, it will initially be located at (0,0) with a size of 0×0, so you won't be able to see it. You'll need to size and position it, such as by calling pack()
on the frame.
Upvotes: 0