さて、私はしばらくこの作業をしていました。私のプログラムでは、特定の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:
のLinux:あなたも、あなたのカスタムテキストフィールドを使用していない投稿
super.paintComponent()は背景をクリアします。そのステートメントの前に行われたすべてのカスタムペインティングは失われます。 – camickr
@camickrこれはLinux上でスイングするのが特有ですか?私が言及したように、それはウィンドウで働くので、super.paintComponent()がその場合はgetBackground()を使用しますが、ここでは何か他のものが使用されると思います。さらに、ボックスはまだ四角い丸みを帯びているので、完全にクリアされているとは思いません。また、私は言及することを忘れてしまった、これはSE 8でも違いがあれば。しかし、ありがとう! – Drayux
私の最初のコメントを無視して間違っています。 – camickr