簡體   English   中英

如何稱油漆(g)?

[英]How to call Paint(g)?

我想調用我的第一個方法,但是它不起作用。 更多,我沒有印刷品。 但是我想畫,什么也沒顯示。

我根據您的回答進行了編輯...

import javax.swing.*;
import java.awt.*;

public class Main extends JPanel {

    private static JPanel pan = new JPanel();
    private static JButton valider = new JButton("Valider");

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); 
        System.out.println("ok"); //It doesn't display ok
        g.drawRect(10, 10, 50, 50);
        g.setColor(Color.GREEN);
    }


    public static void fenetre() {
        JFrame fenetre = new JFrame("Fenetre");
        fenetre.setVisible(true);
        fenetre.setSize(480,272);
        fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pan.add(valider);
        fenetre.add(pan);
        fenetre.revalidate();
        //fenetre.repaint();
        fenetre.setContentPane(pan);
        fenetre.getContentPane().setBackground(Color.BLUE);

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Lancement du programme");
        fenetre();

    }

}

我有“公共類Main擴展JPanel”,但是什么也沒發生。

好吧,您永遠不會將面板添加到框架中。

有關定制繪畫的工作示例,請先閱讀Swing教程“ 定制繪畫”中的部分 它將向您展示如何覆蓋paintComponent()方法,如何覆蓋getPreferredSize()方法以及如何將面板添加到框架。

本教程中的示例還將向您展示如何更好地構建代碼。 因此,基本上擺脫您當前的代碼,並從教程中下載示例,然后對該代碼進行更改。 我將從第三個示例開始,因為它是最完整的。

問題是添加面板之前調用setVisible 這使組件層次結構無效。

從API文檔獲取invalidate()

任何與布局有關的信息發生更改(例如,設置組件的邊界或將組件添加到容器 )時, 自動調用此方法

只需添加面板調用setVisible ,或調用revalidate() (我建議在添加所有內容后調用setVisible


pan應該聲明為:

JPanel pan = new Main();

不像您當前擁有的JPanel pan = new JPanel()那樣。

現在,您當前使用的是普通的JPanel ,而不是Main的實例, Main的實例包含繪畫代碼。

您在Main類中定義了paintComponent方法,但是沒有將Main的任何實例添加到框架中。 使用自定義paintComponent方法編寫JPanel的子類不會導致所有普通JPanel(例如, JPanel pan = new JPanel() )都具有該方法。 只有您的Main類實例才具有它。

簡而言之,更改此:

private static JPanel pan = new JPanel();

對此:

private static JPanel pan = new Main();

我不明白您要做什么,但是您根本不需要自己調用paintComponent 當您顯示方法所在的組件時,將自動調用它。

您需要創建一個JFrame ,將Component放在其上,然后顯示JFrame 嘗試一些Swing入門教程。

您不會直接打電話給paint請看這個答案

您需要@Override paintComponent方法(看起來沒有將您的方法標記為重寫),然后每當由Swing重新繪制組件(在這種情況下,框架的內容窗格)時,它將自動調用paintComponent

要出於任何原因(例如,如果要渲染動畫)自己觸發此事件,只需在組件上調用repaint()即可。 但是,我不會這樣做。

編輯 :忘記了paintComponent()一個關鍵部分。

public void paintComponent(Graphics g) {
    super.paintComponent(g); // you need to be calling this to ensure the hierarchy is respected
    System.out.println("ok");
    g.drawRect(10, 10, 50, 50);
    g.setColor(Color.GREEN);
}

所以,我將向您展示我的修改

 package proj1; import javax.swing.*; import java.awt.*; public class Main extends JPanel { private static JPanel pan = new JPanel(); private static JButton valider = new JButton("Valider"); @Override public void paintComponent(Graphics g) { super.paintComponent(g); System.out.println("ok"); //It doesn't display ok g.drawRect(10, 10, 50, 50); g.setColor(Color.GREEN); } public static void fenetre() { JFrame fenetre = new JFrame("Fenetre"); fenetre.setVisible(true); fenetre.setSize(480,272); fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pan.add(valider); fenetre.add(pan); fenetre.revalidate(); //fenetre.repaint(); fenetre.setContentPane(pan); fenetre.getContentPane().setBackground(Color.BLUE); } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Lancement du programme"); fenetre(); } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM