4

これは、すでに投稿されている多くの質問のように聞こえると思いますが、読み進めてください。これには一見同様の問題がありますが、すでに提供されている多くのソリューションでは機能しませんでした。

私は現在、OS X と Windows の両方で Eclipse を使用して Java で書いています。OS X では、.setEnabled(true) から .setEnabled(false) になる JButton があります。これが発生すると、無効になるため、.setBackground(someColor) を介して背景色を変更します。これが発生している間ずっと、前景 (フォント) の色は変化せず、黒のままです。これは私が欲しいものです。このままで完璧です。

次に、Windows の問題が発生します。上記と同じように、.setEnabled(true) から .setEnabled(false) になる JButton があります。また、.setBackground(someColor) を使用して背景を変更します。ただし、これが発生すると、前景色 (フォント) の色は一定に保たれず、黒から薄い灰色に変わります。これは非常に不便で、新しい背景色では非常に読みにくくなります。

問題は、無効になっている JButton の前景色を変更するにはどうすればよいかということです。

私はすでに次のことを試しました:

button.setForeground(Color.BLACK);

button.setText(<html><font color = black>BUTTON</font></html>);

UIManager.put("Button.disabledText", Color.BLACK);

UIManager.getDefaults().put("Button.disabledText", Color.BLACK);

UIManager.put("Button.foreground", Color.BLACK);

UIManager.getDefaults().put("Button.foreground", Color.BLACK);

どれもうまくいきませんでした。また、次のことも試しました。

  • OS X を .jar ファイルにエクスポートし、それを Windows で使用します。Windows のフォントの色は変わりません。
  • OS X 用の .app ファイルと Windows 用の .exe ファイルをコンパイルします。問題はまだ残っています。

私が見落とした他の解決策はありますか?

現時点では、フォントを現在の見苦しい灰色のままにして、背景を変更することに頼っています (何らかの理由で、.setBackground() を介して簡単に変更できます)。それに対応できる他の色に変更します。

では、なぜ OS X と Windows でこのような色の違いがあるように見えるのでしょうか? 私は OS X の配色を好みますが、本質的に同じプログラムに対して 2 セットのコードを使用したくはありません。

私は何をすべきか?

4

2 に答える 2

4

ここに画像の説明を入力 . . . . . .ここに画像の説明を入力

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class HtmlAndJButton {

    final String buttonText = " Whatever words, <br> but nothing wise";
    final String buttonText1 = " Whatever words, <br> but nothing wise, "
            + "<br> plus 1st. line, ";
    final String buttonText2 = " Whatever words, <br> but nothing wise, "
            + "<br> plus 1st. line, <br> plus 2nd. line,";
    private JButton btn1 = new JButton("Toggle");
    private JButton button = new JButton(buttonText);
    private JButton button1 = new JButton("Toggle");
    private JButton button2 = new JButton("Toggle");

    public HtmlAndJButton() {
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.setText("<html><font color=" + (button.isEnabled()
                        ? "blue" : "red") + ">" + buttonText + "</font></html>");
                button.setEnabled(!button.isEnabled());
                button1.setText("<html><font color=" + (button1.isEnabled()
                        ? "red" : "green") + ">" + buttonText1 + "</font></html>");
                button1.setEnabled(!button1.isEnabled());
                button2.setText("<html><font color=" + (button2.isEnabled()
                        ? "green" : "yellow") + ">" + buttonText2 + "</font></html>");
                button2.setEnabled(!button2.isEnabled());
            }
        });
        button.setText("<html><font color=red>" + buttonText + "</font></html>");
        button1.setText("<html><font color=green>" + buttonText1 + "</font></html>");
        button2.setText("<html><font color=yellow>" + buttonText2 + "</font></html>");
        JFrame f = new JFrame("ButtonTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2, 2));
        f.add(button);
        f.add(button1);
        f.add(button2);
        f.add(btn1);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                HtmlAndJButton t = new HtmlAndJButton();
            }
        });
    }
}
于 2013-05-01T13:39:11.093 に答える
0

あなたのJavaバージョンは何ですか?? あなたのサンプルコードを試した後、うまくいかないからです。Java 1.7 を使用しましたが、動作しません。

これを試して...

public class TestEntryPoint {

    private JButton btn1 = new JButton("Test");
    private JButton button1 = new JButton("Toggle");

    public TestEntryPoint() {

        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                button1.setEnabled(!button1.isEnabled());

            }
        });
        UIManager.put("Button.disabledText", Color.red);
        JFrame f = new JFrame("ButtonTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2, 2));
        f.add(button1);
        f.add(btn1);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestEntryPoint();
            }
        });
    }
}
于 2015-07-01T06:05:31.677 に答える