2015-12-07 44 views
0

所以這是我當前項目的註釋中給出的示例。通常我會運行示例代碼並使用它來查看一切是如何工作的。但是我不確定如何正確調用示例代碼中的函數。使用eclipse的Java中的GUI應用程序

這東西是給我:

import java.awt.Color; 
import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Draw extends JPanel implements ActionListener 
{ 
    JTextField tfInfo; 
    JLabel lblColor, lblShapes; 
    JCheckBox cbRed, cbBlue; 
    ButtonGroup shapes; 
    JRadioButton rbCircle, rbSquare; 
    JButton btnSubmit; 
    public Draw() 
    { 
     setLayout(new FlowLayout(FlowLayout.CENTER,5,5)); 
     tfInfo = new JTextField("Color & Shapes", 15); 
     lblColor = new JLabel("Colors:"); 
     cbRed = new JCheckBox("Red"); 
     cbBlue = new JCheckBox("Blue"); 
     lblShapes = new JLabel("Shapes:"); 
     shapes = new ButtonGroup(); 
     rbCircle = new JRadioButton("Circle"); 
     rbSquare = new JRadioButton("Square"); 
     btnSubmit = new JButton("Submit"); 
     btnSubmit.addActionListener(this); 
     add(tfInfo); 
     add(lblColor); 
     add(cbRed); 
     add(cbBlue); 
     add(lblShapes); 
     add(rbCircle); 
     add(rbSquare); 
     add(btnSubmit); 
     shapes.add(rbCircle); 
     shapes.add(rbSquare); 
    } 
    public static void main(String [] args) 
    { 
     Draw n = new Draw(); 
     n.setVisible(true); 
    } 
    public void actionPerformed(ActionEvent a) 
    { 
     if(a.getSource() == btnSubmit) 
     { 
      if(cbRed.isSelected()&&cbBlue.isSelected()) 
      { 
       if(rbCircle.isSelected()) 
       { 
        tfInfo.setText("urple Circle"); 
       } 
       else if(rbSquare.isSelected()) 
       { 
        tfInfo.setText("Purple Square"); 
       } 
      } 
      else if(cbRed.isSelected()) 
      { 
       if(rbCircle.isSelected()) 
       { 
        tfInfo.setText("Red Circle"); 
       } 
       else if(rbSquare.isSelected()) 
       { 
        tfInfo.setText("Red Square"); 
       }  
      } 
      else if(cbBlue.isSelected()) 
      { 
       if(rbCircle.isSelected()) 
       { 
        tfInfo.setText("Blue Circle"); 
       } 
      } 
      else if(rbSquare.isSelected()) 
      { 
       tfInfo.setText("Blue Square"); 
      } 
     } 
    } 
    public class MApp extends JPanel implements MouseListener 
    { 
     private boolean clicked; 
     private Rectangle r; 
     public MApp() 
     { 
      clicked = false; 
      r = new Rectangle(10, 10, 50, 50); 
      addMouseListener(this); 
     } 
     public void paintComponent(Graphics g) 
     { 
      if(clicked) 
      { 
       g.setColor(Color.BLUE); 
      } 
      else 
      { 
       g.setColor(Color.RED); 
      } 
      g.fillRect((int)r.getX(), (int)r.getY(), 
      (int)r.getWidth(), (int)r.getHeight()); 
     } 
     public void mouseClicked (MouseEvent e) 
     { 
      Point p = new Point(e.getX(),e.getY()); 
      if(r.contains(p)) 
      { 
       clicked = !clicked; 
      } 
      repaint(); 
     } 
     public void mousePressed (MouseEvent evnt) {} 
     public void mouseReleased (MouseEvent evnt) {} 
     public void mouseEntered (MouseEvent evnt) {} 
     public void mouseExited (MouseEvent evnt) {} 
    } 
} 
+0

你應該看看[創建一個GUI與JFC/Swing](http://docs.oracle.com。 com/javase/tutorial/uiswing /),[如何使用按鈕,複選框和單選按鈕](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html),[如何使用按鈕,複選框和單選按鈕]編寫Action Listeners](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html),[在AWT和Swing中繪製](http://www.oracle.com/technetwork/java /painting-140037.html)和[執行自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/) – MadProgrammer

+1

Swing(和大多數GUI)的工作方式與基於控制檯的程序不同,這是因爲它們是事件驅動的,並且有很多正在發生的「黑盒子」代碼(比如實際調用'actionPerformed'方法的方式)。事實上,大多數情況下,你並不在乎。你知道你可以在控件上添加一個'ActionListener',比如'JButton',當發生規定的事件時(比如用戶點擊按鈕),它會調用你的'actionPerformed'方法 – MadProgrammer

回答

1

我想我會分解成以下幾個部分:

1)把MAPP到它自己的Java文件。另外,我改變了mouseClicked中的repaint():paintComponent(getGraphics());你的新的Java文件將是這樣的:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.JPanel; 

public class MApp extends JPanel implements MouseListener 
{ 
    private boolean clicked; 
    private Rectangle r; 
    public MApp() 
    { 
     clicked = false; 
     r = new Rectangle(10, 10, 50, 50); 
     addMouseListener(this); 
    } 
    public void paintComponent(Graphics g) 
    { 
     if(clicked) 
     { 
      g.setColor(Color.BLUE); 
     } 
     else 
     { 
      g.setColor(Color.RED); 
     } 
     g.fillRect((int)r.getX(), (int)r.getY(), 
     (int)r.getWidth(), (int)r.getHeight()); 
    } 
    public void mouseClicked (MouseEvent e) 
    { 
     Point p = new Point(e.getX(),e.getY()); 
     if(r.contains(p)) 
     { 
      clicked = !clicked; 
     } 
     paintComponent(getGraphics()); 
    } 
    public void mousePressed (MouseEvent evnt) {} 
    public void mouseReleased (MouseEvent evnt) {} 
    public void mouseEntered (MouseEvent evnt) {} 
    public void mouseExited (MouseEvent evnt) {} 
} 

下一步:創建一個主文件,像這樣:

import javax.swing.JFrame; 

public class Tester { 

    public static JFrame window = new JFrame("Graphics"); 

    public static void main(String[] args) { 
     window.setBounds(100, 100,800, 800); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setLayout(null); 

     MApp m = new MApp(); 
     m.setBounds(100,100,50,50); 
     window.add(m); 

     Draw d = new Draw(); 
     d.setBounds(0, 0, window.getWidth(), 80); 
     window.add(d); 

     window.setVisible(true); 
    } 

} 

(不要忘記從你的繪圖類中刪除了MAPP類)

你將能夠運行你的代碼並查看正在發生的事情......這只是一個快捷鍵,但你會得到這個想法