1

我的案例

我有一个奇怪的BUG

在此处输入图像描述

我需要使用 1 个主类中的 2 个类,如下所示:

  • 驾驶两个班级的 1 个主要班级是YumYumYum

  • 其他 2 个仅作为用户界面的类如下:

    a)MyJSlider主要BUG创建者

    b)Volume第二个类,它只显示一个带有绿色进度条的黑色窗口

现在我已经在一个文件中准备了 SSCCE,demo.xml 也可以从这里获得。

问题

如果我注释掉该行private MyJSlider _______BUG__BUG__BUG_______ = new MyJSlider();它可以工作,但是当我使用它时,它会导致一切麻烦。我该如何解决?

import java.awt.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.plaf.synth.SynthLookAndFeel;

public class YumYumYum {
  //
  // Step 1: using 2 class from here
  //
  private MyJSlider _______BUG__BUG__BUG_______ = new MyJSlider();    
  private static JFrame f = new JFrame();    
  public YumYumYum() {
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setLayout(new FlowLayout());
    f.setPreferredSize(new Dimension(300, 200));         
    f.getContentPane().add(new JButton("Show me as Swing only - Then i am correct"));    
    f.pack();
    f.setVisible(true);    
  }
  public void test() {
    Volume a = new Volume();
    a.up(100);
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        YumYumYum s = new YumYumYum();
        s.test();
      }
    });
  }

  //
  // Step 2: Creates the problem
  //
  public class MyJSlider extends JSlider {
    public MyJSlider() {     
      try {
        SynthLookAndFeel laf = new SynthLookAndFeel();
        //laf.load(MyJSlider.class.getResourceAsStream("/ui/demo.xml"), MyJSlider.class);      
        laf.load(new URL("file:///var/tmp/demo.xml"));
        UIManager.setLookAndFeel(laf);
      } catch (Exception e) {
        e.printStackTrace();
      }       
      setOrientation(JSlider.VERTICAL);
      //setMinimum(-24);
      setMaximum(12);
      setOpaque(false);
    }
  }

  //
  // Step 3: Does not show its actual user interface when Step 2 is involved
  //
  public class Volume extends JWindow {
    private JPanel panelFirst;
    private JProgressBar a;
    public Volume() {    
      setLayout(new BorderLayout());
      setBounds(10, 40, 350, 60);
      panelFirst = new JPanel();        
      panelFirst.setBackground(Color.BLACK);    
      panelFirst.setLayout(new FlowLayout(FlowLayout.LEFT, 8,5));        
      a = createJP(100); 
      panelFirst.add(a);
      add(panelFirst);
      setVisible(true);
      setAlwaysOnTop(true);
    }

    public JProgressBar createJP(int input) {
      JProgressBar jp = new JProgressBar(JProgressBar.VERTICAL, 0, 100);
      jp.setPreferredSize(new Dimension(15, 50));
      jp.setValue(input);
      jp.setBackground(Color.RED);
      jp.setVisible(true);
      return jp;  
    }
    public void up(int input) {
      a.setForeground(Color.GREEN);
    }
  }

}

跟进:

  • 在 Java 6 和 Java 7 中测试在 Linux 中的结果相同
4

2 回答 2

0

我怀疑调用UIManager.setLookAndFeel是搞砸了你的用户界面(我在 Mac OSx 下测试过,没有任何问题......)

尝试在调用之前将调用setLookAndFeel放在方法中......maininvokeLater

public static void main(String[] args) {
    SynthLookAndFeel laf = new SynthLookAndFeel();
    //laf.load(MyJSlider.class.getResourceAsStream("/ui/demo.xml"), MyJSlider.class);      
    laf.load(new URL("file:///var/tmp/demo.xml"));
    UIManager.setLookAndFeel(laf);
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        YumYumYum s = new YumYumYum();
        s.test();
      }
    });
}

这可能会有所帮助

于 2012-07-19T21:27:12.977 回答
0

解决此问题的替代方法如下:

在此处输入图像描述

把它放在 Synth 不适合的地方。

  //
  // Step 3: Makeing this work
  //
  public class Volume extends JWindow {
    private JPanel panelFirst;
    private JProgressBar a;
    public Volume() {
      try {
                  // Set cross-platform Java L&F (also called "Metal")
              UIManager.setLookAndFeel(
                  UIManager.getCrossPlatformLookAndFeelClassName());
          } 
          catch (UnsupportedLookAndFeelException e) {
            // handle exception
          }
          catch (ClassNotFoundException e) {
            // handle exception
          }
          catch (InstantiationException e) {
            // handle exception
          }
          catch (IllegalAccessException e) {
            // handle exception
          }      
      setLayout(new BorderLayout());
      setBounds(40, 40, 350, 60);
      panelFirst = new JPanel();        
      panelFirst.setBackground(Color.BLACK);    
      panelFirst.setLayout(new FlowLayout(FlowLayout.LEFT, 8,5));        
      a = createJP(100); 
      panelFirst.add(a);
      add(panelFirst);
      setVisible(true);
      setAlwaysOnTop(true);
    }

    public JProgressBar createJP(int input) {
      JProgressBar jp = new JProgressBar(JProgressBar.VERTICAL, 0, 100);
      jp.setPreferredSize(new Dimension(15, 50));
      jp.setValue(input);
      jp.setBackground(Color.BLACK);
      jp.setVisible(true);
      return jp;  
    }
    public void up(int input) {
      a.setForeground(Color.GREEN);
    }
  }
于 2012-07-19T22:54:53.947 回答