2016-10-28 5 views
0

さて、私はしばらくこの作業をしていました。私のプログラムでは、特定のJComponentを継承し、paintComponentメソッドをオーバーライドし、それらのクラスをオブジェクトに使用する新しいクラスを作成することで、カスタムUIのルックアンドフィールに近づいています。これは私が失われたところです。私は、ユーザーにそのディレクトリを与えるように求める簡単なウィンドウを持っていて、ディレクトリをチェックし、その場所にディレクトリを作成できない場合はテキストフィールドの色を(拡張JTextField) 。私はgitとクロスオーバーを使用して2台のコンピュータでこれを開発しています。このはWindowsではで動作しますが、Linuxでは失敗します。ここでは、コードは次のとおりです。JTextField super.paintComponent()は、Linuxの場合、背景色を無視します

class DraconicTextField extends JTextField { 

    private static final long serialVersionUID = 1L; 

    private static final int arcSize = 13; 

    final Color textColor = new Color(31, 31, 31); 
    final Color boxColor = new Color(250, 250, 250); 
    final Color borderColor = new Color(250, 250, 250, 0); 

    public DraconicTextField() { 
     this.setOpaque(false); //true gives the same result, but corners aren't rounded if set as such 
     this.setForeground(textColor); //Text color 
     this.setBackground(boxColor); //BG color 
     this.putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, null); 

     this.setFont(new Font("Arial", Font.PLAIN, 18)); 
     this.setFont(GUIUtils.getDefaultFont(this).deriveFont(Font.PLAIN, 18f)); //GUIUtils is imported 

     this.setBorder(new DraconicRoundBorder(arcSize, borderColor)); 

    } 

    @Override 
    public void paintComponent(Graphics graphics) { 
     Graphics2D graphics2d = (Graphics2D) graphics; 

     graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 

     graphics2d.setColor(this.getBackground()); 
     graphics2d.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), arcSize, arcSize); 

     super.paintComponent(graphics2d); //I am almost certain this is the problem-causer 

    } 

} 

いくつかのスペースを節約するために、メインフレームクラスは単にinvaldするディレクトリを見つけるとgamedirBox.setBackground(/*some color*/)を呼び出します。

私のテストでは、実際に私が色を変えることができるかどうかを調べるために、短くて(ひどくフォーマットされた)プログラムを作ったが、これはpaintComponentのメソッドをオーバーライドしていない。 このコードはではなく、上記のコードの一部であるです。ここはそのコードです:

class GuiBox extends JFrame { 

    public JLabel thisIsTheLabel = this.label("Hello again, world!"); 
    public JTextField testBox = new JTextField(); 
    public JButton testButton = new JButton("Change the color!"); 

    private Random randy = new Random(); 

    public GuiBox(String title) { 
     super(title); 

     this.setSize(300, 400); 
     this.setLayout(new FlowLayout()); 

     testBox.setMinimumSize(new Dimension(200, 40)); 
     testBox.setPreferredSize(new Dimension(200, 40)); 
     testBox.setText("This is some really long string so that flow layout stops being a ****."); 

     testBox.setBackground(new Color(240, 240, 240)); 

     testButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       testBox.setBackground(new Color(randy.nextInt(255), randy.nextInt(255), randy.nextInt(255))); 
       testBox.repaint(); //Notice, I do not override paintComponent() 

      } 


     }); 

     this.add(testBox); 
     this.add(testButton); 

     this.setVisible(true); 

    } 

    public static void createBox() { 
     GuiBox window = new GuiBox("test box"); 

    } 

} 

は、君たちの助けのためのトンをありがとうございました!

編集 - スクリーンショット:

のWindows: Windows Functionality

のLinux:あなたも、あなたのカスタムテキストフィールドを使用していない投稿 Linux Functionality

+0

super.paintComponent()は背景をクリアします。そのステートメントの前に行われたすべてのカスタムペインティングは失われます。 – camickr

+0

@camickrこれはLinux上でスイングするのが特有ですか?私が言及したように、それはウィンドウで働くので、super.paintComponent()がその場合はgetBackground()を使用しますが、ここでは何か他のものが使用されると思います。さらに、ボックスはまだ四角い丸みを帯びているので、完全にクリアされているとは思いません。また、私は言及することを忘れてしまった、これはSE 8でも違いがあれば。しかし、ありがとう! – Drayux

+0

私の最初のコメントを無視して間違っています。 – camickr

答えて

1
public JTextField testBox = new JTextField(); 

コード。

カスタムテキストフィールドを使用しても、それでもWindowsでは動作しません。

this.setOpaque(false); 

上記の文は、コンポーネントが独自の背景を描画しないことを示しています。したがって、親コンポーネントのバックグラウンドのみが表示されます。

カスタムボーダーが原因でコードが動作するように見えるかもしれませんが、テキストコンポーネント自体が背景をペイントしません。

+0

残念ながら、これはまだ動作しません。投稿に記載されているように(申し訳ありませんが十分ではない場合)、コードの2番目のブロックは*機能している*別のプログラムでしたので、問題は私のカスタムテキストフィールドのどこかにあると判断できます。私はすぐにここにいくつかのスクリーンショットを追加しますが、setOpaque(false)を使ってWindows上にまだ色付けがあります。私は間違っているかもしれませんが、スーパークラスのペイントコンポーネントに自分自身の背景をペイントしないように設定すると、まさに私が望むものなので、自分のペイントすることができます。 – Drayux

関連する問題