Reputation: 137
I setup a panel through a class and would like to be able to add components to the panel within the actual class. For simplification, in the following code I am just trying to add a label. The following is what I have so far, it doesn't like me using this though.
package testframe2;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestFrame2 {
public static void main(String[] args) {
new TestFrame2();
}
public TestFrame2() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label = new JLabel("hello world");
this.add(label);
}
}
Upvotes: 0
Views: 75
Reputation: 776
Try this. I did not try to build it, you may still have some more errors.
package testframe2;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestFrame2 {
public static void main(String[] args) {
new TestFrame2();
}
public TestFrame2() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label = new JLabel("hello world");
public TestPane() {
super();
this.add(label);
}
}
}
Upvotes: 0
Reputation: 4039
Of course you can do this. But write correct Java! Extend your TestPane from JPanel and then create and add your components to your custom Panel in the constuctor.
public class TestPane extends JPanel {
private JLabel label;
public TestPane(){
super();
label = new JLabel("hello world");
add(label);
}
}
}
Upvotes: 0
Reputation: 8354
public class TestPane extends JPanel {
private JLabel label = new JLabel("hello world");
this.add(label);
}
this is invalid syntax , put this.add(label);
in a method(or TestPane
constructor)
Upvotes: 1