I am a beginner here and I am trying to make a GUI that will display two buttons, addCard and deleteCard. However, the compiler is showing errors, and I cannot seem to find the error. Thank you in advance!
package studyfast;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Scratch{
JFrame projectFrame = new JFrame();
projectFrame.setSize(1000, 600);
projectFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panelEditCard = new JPanel();
JButton addCard = new JButton();
JButton deleteCard = new JButton();
panelEditCard.add(addCard);
panelEditCard.add(deleteCard);
projectFrame.add(panelEditCard);
projectFrame.setVisible(true);
public static void main(String[] args){
new Scratch();
}
}
You need to move your code inside the Scratch
class inside a constructor
package studyfast;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Scratch
{
JFrame projectFrame = new JFrame();
JPanel panelEditCard = new JPanel();
JButton addCard = new JButton();
JButton deleteCard = new JButton();
public Scratch()
{
projectFrame.setSize(1000, 600);
projectFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
panelEditCard.add(addCard);
panelEditCard.add(deleteCard);
projectFrame.add(panelEditCard);
projectFrame.setVisible(true);
}
public static void main(String[] args)
{
new Scratch();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.