JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區 » Swing/AWT/SWT/JFace  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 GUI-程式剛執行時如何能呈現paintComponent(Graphics g)之內容
LonelyBack





發文: 1
積分: 0
於 2019-01-02 13:28 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這是我的code
我想要在程式一開始就顯示我的這個畫面
但下面那個藍色的 IamBACK只有再按按鈕的時候才會出現

不知道要怎麼做
只知道在按完按鈕後才顯示

期望是這樣~~


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//有個全域變數
//boolean isFirst = true;
//這方法在Panel2裡面
 
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (isFirst) {
      g.setColor(new Color(0, 210, 210));
      g.drawLine(0, 0, 600, 0);
      int[] xPoints = { 210, 350, 350, 280, 210 };
      int[] yPoints = { 100, 100, 200, 280, 200 };
      g.fillPolygon(xPoints, yPoints, xPoints.length);
      g.setFont(new Font(Font.SERIF, Font.PLAIN, 80));
      g.setColor(new Color(0, 254, 254));
      g.drawString("IamBACK", 130, 70);
      isFirst = false;
    }
  }


這是我的Frame

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JFrame;
 
public class Frame extends JFrame {
  private Panel2 p2;
  private Panel1 p1;
 
  Frame() {
    init();
  }
 
  void init() {
    setLayout(new BorderLayout());
    
    p2 = new Panel2();
    p1 = new Panel1();
    add(p1, "North");
    add(p2, "Center");
    p1.btnClean.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        p2.isFirst = true;
        p2.repaint();
      }
    });
 
    p1.btnDraw.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        Graphics g = p2.getGraphics();
        String num = p1.tfSize.getText();
        int type = 0;
        if (p1.radSin.isSelected())
          type = 1;
        else if ((p1.radCos.isSelected()))
          type = 2;
        else if ((p1.radTan.isSelected()))
          type = 3;
        else if ((p1.radCsc.isSelected()))
          type = 4;
        else if ((p1.radSec.isSelected()))
          type = 5;
        else if ((p1.radCot.isSelected()))
          type = 6;
 
        p2.paintComponent(g, type, Integer.parseInt(num));
      }
    });
  }
}


程式進入點是這裡

1
2
3
4
5
6
7
8
9
10
11
12
13
import javax.swing.JFrame;
 
public class Client {
  public static void main(String[] args) {
 
    Frame Frame = new Frame();
    Frame.setSize(600, 900);
    Frame.setLocationRelativeTo(null);
    Frame.setTitle("GUI Demo for Object-Oriented Progamming");
    Frame.setVisible(true);
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}


LonelyBack edited on 2019-01-02 14:02
reply to postreply to post
» JWorld@TW »  Java SE 討論區 » Swing/AWT/SWT/JFace

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8