2015-06-05 59 views
-1

我正在編程序。點擊按鈕。創建圖紙。並點擊另一個按鈕使其移動。我的編碼有什麼問題?用java - swing

但我有一個錯誤的開始。

1st。雖然我用超級& actionlistener它編譯得很好,但它根本不起作用。

2nd。我應該如何使用線程進行編碼? (這並不意味着教我整個代碼,在我搞砸所有事情之前,我需要一些建議。)

這是我的編碼。

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Hello extends JComponent{ 
//This is main code to create GUI 
public Hello(){ 

EventQueue.invokeLater(new Runnable() { 

public void run(){ 
JFrame frame = new JFrame(); 

frame.setLayout(new BorderLayout()); 
//set Frame & layout 
JPanel south = new JPanel(); 
testpane center = new testpane(); 
JButton b1 = new JButton("Create"); 
b1.setBackground(Color.WHITE); 
b1.setFont(new Font("Arial",Font.ITALIC,20)); 
south.add(b1); 
//setting the first button to create Things. i'll create another for move 

b1.addActionListener(new ActionListener(){ 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     //repaint(); -> this was my trial for not working 
     center.setoutput(); 
    } 

    //create action listener and **Here is the place I have a trouble** 
}); 
frame.getContentPane().add(south, BorderLayout.SOUTH); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setTitle("Robot"); 
frame.setSize(500,500); 
frame.setVisible(true); 
} 
}); 
} 
public class testpane extends JPanel{ 
testpane(){ 

} 
public void setoutput(){ 
repaint(); 
}// this is the fuction to call repaint at Hello class 
    protected void paintComponent(Graphics g){ 
super.paintComponent(g); 
Graphics2D g2 = (Graphics2D)g; 
g2.setColor(Color.BLACK); 
g2.fillRect(100,100,30,30); 
g2.drawRect(100, 100,30, 30); 
g2.dispose(); 
}//this is the Thing that I want to draw 
} 
public static void main(String[] args){ 
new Hello(); 
} 
//main class. 
} 
+0

你可以在究竟是什麼更清晰這是不工作/你需要知道? – Stultuske

+0

ActionListener不在監聽。我想知道如何讓創建的對象移動。 –

+0

就像當我點擊按鈕時。這個矩形不會在 –

回答

0

我改變了一點點你的代碼更具可讀性。 你根本不添加中心的JPanel到

現在油漆出現一次的幀時打開的窗口,你可以修改它,你知道的,如果其他人或東西,或在JFrame中添加的JPanel 中心單擊該按鈕時..

這裏是你的代碼:

@SuppressWarnings("serial") 
public class Hello extends JFrame { 

// This is main code to create GUI 
public Hello() { 

    EventQueue.invokeLater(new Runnable() { 

     public void run() { 

      setLayout(new BorderLayout()); 

      // set Frame & layout 
      JPanel south = new JPanel(); 
      testpane center = new testpane(); 
      JButton b1 = new JButton("Create"); 
      b1.setBackground(Color.WHITE); 
      b1.setFont(new Font("Arial", Font.ITALIC, 20)); 
      south.add(b1); 
      // setting the first button to create Things. i'll create 
      // another for move 

      b1.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        // repaint(); -> this was my trial for not working 
        center.setoutput(); 
       } 

       // create action listener and **Here is the place I have a 
       // trouble** 
      }); 


      getContentPane().add(south, BorderLayout.SOUTH); 
      getContentPane().add(center,BorderLayout.CENTER); //Here you never have added the panel to the frame 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setTitle("Robot"); 
      setSize(500, 500); 
      setVisible(true); 
     } 
    }); 
} 


///JPanel Class 
public class testpane extends JPanel { 

    public void setoutput() { 
     System.out.println("Entered setoutput"); 
     repaint(); 
    } 

    //Paint Method 
    protected void paintComponent(Graphics g) { 
     System.out.println("Entered paint"); 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setColor(Color.BLACK); 
     g2.fillRect(100, 100, 30, 30); 
     g2.drawRect(100, 100, 30, 30); 
     g2.dispose(); 
    } 
} 




//Main Class 
public static void main(String[] args) { 
    new Hello(); 
} 



}//End of center class 
+0

whoa ..單一行給我很多壓力... –

+0

我真的很感謝您的幫助! –

+0

嗯。從這個代碼。這只是創建矩形已經。但是我想要的是當我點擊按鈕時創建矩形。 –

0

首先添加的JPanel您的JFrame這樣

frame.add(south); 

當你打電話的主要方法使用另一類這個方法

public static void main(String[] args) { 
Hello h = new Hello(); 
} 

我已經在你的代碼的變化,現在它正在

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Hello extends JFrame { 

public Hello() { 

    EventQueue.invokeLater(new Runnable() { 

     public void run() { 

      setLayout(new BorderLayout()); 

      JPanel south = new JPanel(); 
      testpane center = new testpane(); 
      JButton b1 = new JButton("Create"); 
      b1.setBackground(Color.WHITE); 
      b1.setFont(new Font("Arial", Font.ITALIC, 20)); 
      south.add(b1); 

      b1.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 

        center.setoutput(); 
       } 

      }); 

      getContentPane().add(south, BorderLayout.SOUTH); 
      getContentPane().add(center, BorderLayout.CENTER); 

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setTitle("Robot"); 
      setSize(500, 500); 
      setVisible(true); 
     } 
    }); 
} 

public class testpane extends JPanel { 

    public void setoutput() { 
     System.out.println("Entered setoutput"); 
     repaint(); 
    } 

    protected void paintComponent(Graphics g) { 
     System.out.println("Entered paint"); 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setColor(Color.BLACK); 
     g2.fillRect(100, 100, 30, 30); 
     g2.drawRect(100, 100, 30, 30); 
     g2.dispose(); 
    } 
} 

public static void main(String[] args) { 
    Hello h = new Hello(); 
} 

}