我需要關於此代碼的幫助。我的g.drawLine(0,0,300,300)不起作用。它一直工作到星期一,我不知道什麼是錯的。我使用g.drawLine(0,0,300,300)爲了在使用plota_pixel()方法之前進行測試。 g.drawLine(0,0,300,300)768,16 JPanel中面板上打印從(0,0)的線至(300300)Java drawLine()無法正常工作
的MainView類:
package alexandre.VIEW;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainView {
private JFrame janela;
public JPanel panel;
public MainView()
{
janela = new JFrame("Exercicio 15/09");
janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
this.ShowView();
}
public void ShowView()
{
janela.pack();
janela.setSize(750,600);
janela.setLayout(null);
janela.add(panel);
panel.setBounds(0,0,710,600);
janela.setVisible(true);
System.out.println("OIdsazxc");
Graphics g = panel.getGraphics();
g.setColor(Color.BLACK);
g.drawLine(0,0,300,300);
}
public void plota_pixel(int x, int y)
{
Graphics g = panel.getGraphics();
g.drawLine(x, y, x, y);
}
}
起動類別:
package alexandre.CONTROL;
import alexandre.VIEW.MainView;
public class Starter {
public static void main(String[] args) {
MainView view = new MainView();
view.ShowView();
}
}
我已經tryied設置panel.setBounds(0,0,710,600);然後將其添加到JFrame –
嗨,感謝您的回答,但我仍然得到一個空的灰色面板。 –