Note : This answers has been edited as it was showing a bad way on how the painting in Java Swing is done.
As pointed by camickr, you don't need to pass the Graphics
Object to your drawLetter()
function as your don't even need a drawLetter()
function.
All you need to do is to create a JLabel
, add it to the frame and use the setText(String)
function from your JLabel Object.
So your code should look like this :
//Main class
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class AlphabetLetter {
public static void main(String[] args) {
String [] buttons = {"Default", "Choose Letter", "Choose Colour", "Quit"};
int option = JOptionPane.showOptionDialog(null, "Choose your option:", "ALPHABET DRAWING", JOptionPane.INFORMATION_MESSAGE, 0, null, buttons, null);
Letter letter = new Letter();
JLabel label = new JLabel();
//Add the Label to the JFrame
letter.add(label);
if (option == 0)
{
//Change the text of the label
label.setText("A");
}
}
}
//letter class
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class Letter extends JFrame{
public Letter()
{
setTitle("Alphabet Drawing");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…