2017-04-06 3 views
1

これを別のJPanelに追加しますが、そこには表示されません。私の他のJpanelはbottomPanelと呼ばれています。 paintComponentは下のパネルに表示されるはずですpaintComponentが表示されないjava

bottomPanel.setLayout(null); 
TestPane tp = new TestPane(); 
bottomPanel.add(tp); 

私はJpanelを拡張しました。私にとって

public class TestPane extends JPanel { 
    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(200, 200); 
    } 


    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g.create(); 
     int width = getWidth() - 100; 
     int height = getHeight() - 100; 
     int x = (getWidth() - width)/2; 
     int y = (getHeight() - height)/2; 
     g2d.setColor(Color.RED); 
     g2d.drawRect(x, y, width, height); 
     g2d.dispose(); 
    } 

} 
+1

どのようにあなたが親コンテナにパネルを追加していますか?これは画面上でどのように表示されていますか? – MadProgrammer

+0

bottomPanelは、これを表示したい別のパネルです。bottomPanel.add(new TestPane());ではありません。それには十分ですか?申し訳ありませんが、Javaの初心者です – hello12345678

+1

スクリーンショットが必要ないため、問題を再現するコードが必要です。 –

答えて

3

問題で始まるあなたの問題を示しrunnable example提供を検討:

bottomPanel.setLayout(null); 

Java GUIは、異なるロケールの異なるPLAFを使用して、異なるOS、画面サイズ、画面解像度などで動作する必要があります。したがって、ピクセルの完全なレイアウトには役立ちません。その代わりに、レイアウトマネージャーを使用するか、white spaceのレイアウトパディングとボーダーとともに、combinations of themを使用します。

、将来的には、むしろ、ファイルI/Oのような無関係な添加物を使用してコードの300行以上よりMCVEを投稿し、テーブル、行ソーターなど

2

作品...

Working Evidence

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       JPanel outer = new JPanel(new BorderLayout()); 
       outer.add(new TestPane()); 
       frame.add(outer); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      int width = getWidth() - 100; 
      int height = getHeight() - 100; 
      int x = (getWidth() - width)/2; 
      int y = (getHeight() - height)/2; 
      g2d.setColor(Color.RED); 
      g2d.drawRect(x, y, width, height); 
      g2d.dispose(); 
     } 

    } 

} 

+0

動作しますが、jPanelに表示されません – hello12345678

+0

@ hello12345678それはうまく動作し、動作していないことを表示します - 別のパネル内に埋め込まれていても動作します – MadProgrammer

+0

ねえ質問の説明に画像を追加しました – hello12345678

関連する問題