2017-12-12 12 views
0

こんにちは、私は新しい午前Java GUIで動作していません。あなたは私を、私は何をすべき案内してくださいすることができますし、GUIは以下の通りです:GUIは基本的にコンストラクタを呼び出すために、誰か場合、私は間違っている場合や何かをやっているものを、画面上のアニメーションを披露れるありアニメーションコンストラクタは、私は以下の通りですsecond.javaを作ったJavaのGUIに

package theproject; 

import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JButton; 
import java.awt.BorderLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.SwingConstants; 
import javax.swing.JTextField; 

    public class Sav { 

private JFrame frame; 
private JTextField textField; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Sav window = new Sav(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public Sav() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    textField = new JTextField(); 
    textField.setBounds(10, 0, 261, 20); 
    frame.getContentPane().add(textField); 
    textField.setColumns(10); 

    JButton btnNewButton = new JButton("Submit"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     second s= new second(); 
     frame.add(s); 
     } 
    }); 
    btnNewButton.setBounds(273, -1, 89, 23); 
    frame.getContentPane().add(btnNewButton); 

} 
} 

それは私に知らせてやる必要があります。

答えて

1

最初に、paintComponentメソッド内の状態を更新しないでください。ペイントは、主にあなたのやり取りなしに、さまざまな理由でいつでも発生することがあります。ペイントは、現在の状態を簡単にペイントする必要があります。 ActionListenerでは、フレームを進めて何が起こるべきかを決める必要があります(フレーム値のリセットなど)

第2に、決して実際にsecondを追加することはないので、決して表示されません。レイアウトマネージャはコンポーネントはすべきであり、単に0x0が割り当てられますどのようなサイズ見当がつかないますので、違いはありませんよう

第三に、あなたは目に見えないように、それはのように良い作り、secondgetPreferredSizeをオーバーライドしない

第4に、nullレイアウトを使用しています。これはあなたの人生を不可能にするでしょう。スウィングは、レイアウトマネージャの使用を中心に設計され、最適化されており、さまざまなレンダリングシステム/パイプライン間のフォントメトリックの違いをどのように処理するかを決定する重要な作業を行います。は、第五に、paintComponentは何のビジネスpublicされていないそれら

、誰もあなたのコードもwoのではない例

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.Timer; 

public class Sav { 

    private JFrame frame; 
    private JTextField textField; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Sav window = new Sav(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public Sav() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 

     textField = new JTextField(20); 
     frame.getContentPane().add(textField, gbc); 

     JButton btnNewButton = new JButton("Submit"); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       second s = new second(); 
       frame.add(s, gbc); 
       frame.getContentPane().revalidate(); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
      } 
     }); 
     frame.getContentPane().add(btnNewButton, gbc); 

     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public class second extends JPanel implements ActionListener { 

     private Timer animator; 
     private ImageIcon imageArray[]; 
     private int delay = 50, totalFrames = 8, currentFreames = 1; 

     public second() { 
      imageArray = new ImageIcon[totalFrames]; 
      for (int i = 0; i < imageArray.length; i++) { 
       imageArray[i] = new ImageIcon(getImage(i)); 

      } 
      animator = new Timer(delay, this); 
      animator.start(); 

     } 

     protected Image getImage(int index) { 
      BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); 
      Graphics2D g2d = img.createGraphics(); 
      FontMetrics fm = g2d.getFontMetrics(); 
      g2d.dispose(); 
      String text = Integer.toString(index); 
      int height = fm.getHeight(); 
      int width = fm.stringWidth(text); 

      img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
      g2d = img.createGraphics(); 
      g2d.setColor(getForeground()); 
      g2d.drawString(text, 0, fm.getAscent()); 
      g2d.dispose(); 
      return img; 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(imageArray[0].getIconWidth(), imageArray[1].getIconHeight()); 
     } 

     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      imageArray[currentFreames].paintIcon(this, g, 0, 0); 
     } 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      currentFreames++; 
      if (currentFreames >= imageArray.length) { 
       currentFreames = 0; 
      } 
      repaint(); 
     } 

    } 
} 

直接

それを呼び出すべきではありませんrking。これは、設定された画像の値が、画像を表示していない

イメージがロードできない場合は私のためだけで正常に動作...

Example

imageArray[i]=new ImageIcon(i+1+".png");がエラーを生成しませんをインクリメント何らかの理由で(バックグラウンドスレッドでイメージをロードすることになりますが、これは別の問題です)。

代わりにを使用することをお勧めします。画像が何らかの理由で読み取れない場合は、IOExceptionが返されますが、これは無限に便利です。詳細については、Reading/Loading an Imageを参照してください。

+0

コードを実行していただきありがとうございます。あなたは私に何か他の理由を教えてもらえませんか? –

+0

私はまだ 'null'レイアウトを疑っています – MadProgrammer

+0

私は絶対layout.inプログラムを使用しましたがレイアウトがヌルで正しい場合はどうすれば正しいことができますか? –

関連する問題