2013-11-15 88 views
2

對於我的任務,我必須編寫一個程序,該程序可以打印一些文本,橢圓形或矩形,具體取決於頂部按下哪個按鈕然而,當我按下按鈕時什麼也沒有發生,我該如何解決這個問題?這是我的第一個GUI,我會很感激任何幫助!我最終需要的程序是:從一個矩形開始,使窗口在調整大小時在屏幕上停留在繪圖區域的中心,而我的橢圓和矩形必須有一半的寬度並且顯示區域的高度。我一次只做這一步,所以我會試圖找出一旦我真的可以在屏幕上獲得一個形狀,感謝:-)。按下按鈕時繪製字符串,矩形或橢圓形的GUI

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

public class firstGUI extends JFrame 
    implements ActionListener 
{ 
    private boolean showText = false; 
    private boolean showRect = false; 
    private boolean showOval = false; 
    private JButton text; 
    private JButton oval; 
    private JButton rectangle; 
    private JPanel buttonPanel; 

    public firstGUI() 
    { 
     super("First GUI"); 
     setSize(512, 512); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new GridLayout(1,3)); 

     text = new JButton("Text"); 
     text.addActionListener(this); 
     buttonPanel.add(text); 

     oval = new JButton("Oval"); 
     oval.addActionListener(this); 
     buttonPanel.add(oval); 

     rectangle = new JButton("Rectangle"); 
     rectangle.addActionListener(this); 
     buttonPanel.add(rectangle); 

     //JComponent drawArea = new JComponent(); 
     drawStuff d = new drawStuff(); 

     Container contentPane = this.getContentPane(); 
     contentPane.add(buttonPanel, BorderLayout.NORTH); 
     contentPane.add(d); 
    } 

    public void actionPerformed(ActionEvent event) 
    { 
     Object source = event.getSource(); 

     if (source == text) 
     { 
      showText = true; 
     } 
     else if (source == oval) 
     { 
      showOval = true; 
     } 
     else if (source == rectangle) 
     { 
      showRect = true; 
     } 
    } 


    public void draw(Graphics g) 
    { 
     if(showText) 
     { 
      g.drawString("Hello", 0, 0); 
     } 
     else if (showOval) 
     { 
      g.drawOval(0, 0, 100, 100); 
     } 
     else if (showRect) 
     { 
      g.drawRect(0, 0, 100, 100); 
     } 
    } 

    public static void main(String [] args) 
    { 
     firstGUI myTest = new firstGUI(); 
     myTest.setVisible(true); 
    } 
} 

class drawStuff extends JPanel 
{ 
    public void paint(Graphics g) 
    { 
     super.paint(g); 
    } 
} 
+3

什麼題? – Reimeus

+0

@Reimeus編輯問題,以便實際提出問題,謝謝指出! – gdhc21

回答

1

試試這個。我添加了一些repaint() s,並幫助您將繪製的對象居中。我也將draw更改爲paintComponent。這是JComponents

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

public class firstGUI extends JFrame 
    implements ActionListener { 

    private boolean showText = false; 
    private boolean showRect = true; 
    private boolean showOval = false; 
    private JButton text; 
    private JButton oval; 
    private JButton rectangle; 
    private JPanel buttonPanel; 
    private DrawStuff drawPanel = new DrawStuff(); 

    public firstGUI() { 
     super("First GUI"); 
     setSize(512, 512); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new GridLayout(1, 3)); 

     text = new JButton("Text"); 
     text.addActionListener(this); 
     buttonPanel.add(text); 

     oval = new JButton("Oval"); 
     oval.addActionListener(this); 
     buttonPanel.add(oval); 

     rectangle = new JButton("Rectangle"); 
     rectangle.addActionListener(this); 
     buttonPanel.add(rectangle); 


     Container contentPane = this.getContentPane(); 
     contentPane.add(buttonPanel, BorderLayout.NORTH); 
     add(drawPanel); 
    } 

    @Override 
    public void actionPerformed(ActionEvent event) { 
     Object source = event.getSource(); 

     if (source == text) { 
      showText = true; 
      repaint(); 
     } else if (source == oval) { 
      showOval = true; 
      repaint(); 
     } else if (source == rectangle) { 
      showRect = true; 
      repaint(); 
     } 
    } 

    public static void main(String[] args) { 
     firstGUI myTest = new firstGUI(); 
     myTest.setVisible(true); 
    } 

    class DrawStuff extends JPanel { 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      if (showText) { 
       g.drawString("Hello", getHeight()/2, getWidth()/2); 
       showText = false; 
      } else if (showOval) { 
       g.drawOval(getWidth()/4, getHeight()/4, getWidth()/2, getHeight()/2); 
       showOval = false; 
      } else if (showRect) { 
       g.drawRect(getWidth()/4, getHeight()/4, getWidth()/2, getHeight()/2); 
       showRect = false; 
      } 
     } 
    } 
} 
+0

當我嘗試編譯時,它給了我這些錯誤:'firstGUI.java:74:錯誤:找不到符號 super.paintComponent(g);'和 'firstGUI.java:71:error:方法不覆蓋或實現一個超類型的方法' – gdhc21

+0

對不起,你應該在'Jpanel'中有'paintComponent'。等待更新 –

+0

@ gdhc21我有一個正在運行的版本。 –

0

你有所有的積木,但按下按鈕後,draw()方法不會被調用。一旦你設置你的狀態(繪製哪個項目),你需要重繪()

0
  1. 繪圖時看看Performing Custom Painting你應該使用什麼。
  2. 請勿覆蓋paint,而應改用paintComponent
  3. 以您的draw方法並將其移動到您的drawStuff類。從paintComponent方法
  4. 建立某種標誌(在drawStuff)決定當你處理按鈕事件應該怎樣畫
  5. 呼叫drawStuff,改變面板上的狀態標誌,並重新繪製面板....

這需要你的框架必須drawStuff

參考您可能還需要看一看,並使用Code Conventions for the Java Programming Language