所以,我想使用PaintComponent在Panel上绘制一些东西。但我想在构造函数中执行此操作。所以,例如:
public class Interfata extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.drawOval(500, 500, 50, 40);
}
public Interfata()
{
// code here
}
public static void main(String[] args)
{
Interfata i = new Interfata();
}
代码应该像这个模型。问题是我不知道如何在构造函数中创建的面板上调用paintComponent方法。 (即使它看起来效率低下,我在构造函数中创建了框架,面板和所有函数,因此,我只需要在main方法中实例化该对象)。如果我不够清楚,请询问您认为合适的任何细节。
编辑:更具体地说,这就是我的构造函数的样子:
public Interfata()
{
// main interface
JFrame frame = new JFrame("Queue");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 450);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((d.getWidth() - frame.getWidth()) / 2);
int y = (int) ((d.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
//simulation interface
JFrame f = new JFrame("Simulare");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(900, 800);
int x1 = (int) ((d.getWidth() - frame.getWidth()) / 2);
int y1 = (int) ((d.getHeight() - frame.getHeight()) / 2);
f.setLocation(x1, y1);
JPanel panel = new JPanel();
panel.setLayout(null);
JPanel panel1 = new JPanel();
panel1.setLayout(null);
JLabel l1 = new JLabel("Intervalul minim de asteptare: ");
JTextArea tf1 = new JTextArea();
panel.add(l1);
l1.setBounds(5,10,200,25);
panel.add(tf1);
tf1.setBounds(200,10,300,20);
JLabel l2 = new JLabel("Intervalul maxim de asteptare: ");
JTextArea tf2 = new JTextArea();
panel.add(l2);
l2.setBounds(5,50,200,25);
panel.add(tf2);
tf2.setBounds(200,50,300,20);
JLabel l3 = new JLabel("Duarata minima pentru serviciu: ");
JTextArea tf3 = new JTextArea();
panel.add(l3);
l3.setBounds(5,90,200,25);
panel.add(tf3);
tf3.setBounds(200,90,300,20);
JLabel l4 = new JLabel("Durata maxima pentru serviciu: ");
JTextArea tf4 = new JTextArea();
panel.add(l4);
l4.setBounds(5,130,200,25);
panel.add(tf4);
tf4.setBounds(200,130,300,20);
JLabel l5 = new JLabel("Numarul de cozi: ");
JTextArea tf5 = new JTextArea();
panel.add(l5);
l5.setBounds(5,170,100,25);
panel.add(tf5);
tf5.setBounds(200,170,300,20);
JLabel l6 = new JLabel("Intervalul de simulare: ");
JTextArea tf6 = new JTextArea();
panel.add(l6);
l6.setBounds(5,210,150,25);
panel.add(tf6);
tf6.setBounds(200,210,300,20);
JButton b1 = new JButton("Simulare");
panel.add(b1);
b1.setBounds(700,100,100,35);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
f.setVisible(true);
}
});
frame.setContentPane(panel);
frame.setVisible(true);
f.setContentPane(panel1);
}
我想要做的就是在新框架的面板上绘制一些东西,或者更具体地说,在框架f中。