2013-05-28 118 views
0

我試圖在JPanel上繪製一個簡單的矩形,但是它並不工作。有人可以給我一些建議嗎?如果有人能......我會很高興,我真的很努力,但我不知道該怎麼做。這段代碼是我的「蛇」程序的一部分。除此之外,其餘所有工作都是有效的。這裏是我的代碼:在JPanel上繪製圖形(Graphics gr)

package initialising_Package; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowListener; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class Initialising extends JPanel implements WindowListener, 
     ItemListener, ChangeListener, Runnable { 

    /** 
    * @param args 
    */ 
    private String name; 
    private JFrame frame; 
    private boolean fall = false; 
    private JSlider slider; 
    private JLabel lbl1, lbl2, lbl3, lbl4; 
    private JComboBox box1, box2, box3; 
    private String[] positionsx, positionsy, laenge; 
    private JPanel pnl1, pnl2, pnl3, pnlpnl; 
    private JButton btn; 
    private int beginningLenght, posX, posY; 
    private boolean start = false; 

    public Initialising() { 
     initStartFrame("Initialising"); 
    } 

    public void initStartFrame(String name) { 
     frame = new JFrame(name); 
     frame.setLocation(300, 300); 
     frame.setSize(500, 350); 
     frame.setUndecorated(false); 
     frame.getContentPane().setLayout(new GridLayout(5, 1)); 
     frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     frame.setVisible(true); 
     frame.addWindowListener(this); 

     // GUI 
     slider = new JSlider(1, 100, 50); 
     slider.addChangeListener(this); 
     slider.setPaintTicks(false); 
     slider.setPaintTrack(true); 

     positionsx = new String[50]; 
     positionsy = new String[50]; 
     for (int i = 0, j = 0; i < 50; i++, j = j + 10) { 
      positionsx[i] = new String("" + j); 
      positionsy[i] = new String("" + j); 
     } 

     laenge = new String[45]; 
     for (int i = 0; i < 45; i++) { 
      laenge[i] = "" + (i + 1); 
     } 

     box1 = new JComboBox(positionsx); 
     box1.addItemListener(this); 
     box2 = new JComboBox(positionsy); 
     box2.addItemListener(this); 
     box3 = new JComboBox(laenge); 
     box3.addItemListener(this); 

     lbl1 = new JLabel("Geschwindigkeit: "); 
     lbl2 = new JLabel("Startlaenge: "); 
     lbl3 = new JLabel("Position: "); 
     pnlpnl = new JPanel(); 
     pnlpnl.setPreferredSize(new Dimension(95, 490)); 
     pnlpnl.setBackground(Color.BLACK); 

     pnl1 = new JPanel(); 
     pnl1.setLayout(new GridLayout(1, 2)); 
     pnl2 = new JPanel(); 
     pnl2.setLayout(new GridLayout(1, 2)); 
     pnl3 = new JPanel(); 
     pnl3.setLayout(new GridLayout(1, 3)); 

     btn = new JButton("Snake!"); 
     btn.setPreferredSize(new Dimension(80, 490)); 

     pnl1.add(lbl1); 
     pnl1.add(slider); 
     pnl2.add(lbl2); 
     pnl2.add(box3); 
     pnl3.add(lbl3); 
     pnl3.add(box1); 
     pnl3.add(box2); 

     frame.add(pnlpnl); 
     frame.add(pnl1); 
     frame.add(pnl2); 
     frame.add(pnl3); 
     frame.add(btn); 

     // GUI 

     frame.validate(); 
     frame.repaint(); 

    } 

    public int getSpeed() { 
     return slider.getValue(); 
    } 

    public int getBeginningLenght() { 
     return beginningLenght; 
    } 

    public int getPosX() { 
     return posX; 
    } 

    public int getPosY() { 
     return posY; 
    } 

    @Override 
    public void windowActivated(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowClosed(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowClosing(WindowEvent arg0) { 
     int z = JOptionPane.showConfirmDialog(frame, 
       "Willst du wirklich benden?", "Warnung", 0); 
     if (z == JOptionPane.YES_OPTION) { 
      System.exit(0); 
     } 

    } 

    @Override 
    public void windowDeactivated(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowDeiconified(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowIconified(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowOpened(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Initialising init = new Initialising(); 
     new Thread(init).start(); 

    } 

    @Override 
    public void itemStateChanged(ItemEvent e) { 
     // TODO Auto-generated method stub 
     if (e.getSource().equals(box1)) { 
      posX = Integer.parseInt((String) box1.getSelectedItem()); 
      System.out.println("PosX: " + posX); 

     } else if (e.getSource().equals(box2)) { 
      posY = Integer.parseInt((String) box2.getSelectedItem()); 
      System.out.println("PosY: " + posY); 

     } else if (e.getSource().equals(box3)) { 
      beginningLenght = Integer.parseInt((String) box3.getSelectedItem()); 
      System.out.println("Beginning Lenght: " + beginningLenght); 

      System.out.println("H: " + pnlpnl.getHeight() + " B: " 
        + pnlpnl.getWidth()); 

     } 

    } 

    @Override 
    public void stateChanged(ChangeEvent e) { 
     // TODO Auto-generated method stub 
     if (e.getSource().equals(slider)) { 
      System.out.println("Speed: " + slider.getValue()); 
     } 

    } 

    @Override 
    public void paintComponent(Graphics gr) { 
     super.paint(gr); 
     gr.setColor(Color.CYAN); 
     gr.fillRect(0, 0, 200, 200); 
    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     while (true) { 
      System.out.println("It works!!"); 
      try { 
       Thread.sleep(800); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      if (!start) { 

      } 
      repaint(); 
      validate(); 
     } 

    } 

} 
+0

如果這是不是有問題(我沒有看過它),那麼你將有更好的運氣得到一個答案最短的代碼示例一個簡短的例子。 – LJNielsenDk

回答

0

從JFrame中移除paintComponent。那也叫做super.paint i.o. super.paintComponent方法。

pnlpnl = new JPanel() { 

     @Override 
     public void paintComponent(Graphics gr) { 
      super.paintComponent(gr); 
      gr.setColor(Color.CYAN); 
      gr.fillRect(0, 0, 200, 200); 
     } 
    }; 

在此的paintComponent可以使用Jframe的領域。

由於initStartFrame在構造函數中被調用,所以它不應該被覆蓋(子類的版本會被調用,而沒有完全初始化子類)。

//Sometimes: public final void initStartFrame(String name) { 
private void initStartFrame(String name) { 

啓動幀這樣的:

// GUI 

    frame.validate(); 
    //frame.repaint(); 

    frame.pack(); 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      frame.setVisible(true); 
     } 
    }); 
+0

謝謝,但不幸的是,這並不適用於我...如果我做你所做的事情,我只能得到一個普通的窗口... – user2428115

+0

編輯: 我改變了一些東西,玩了一些東西,現在你的想法有效!非常感謝! – user2428115