JScrollPane不使用本机OS卷轴(Swing)

时间:2014-10-16 21:19:40

标签: java swing jscrollpane native

我在Windows 8.1操作系统上创建了一个包含JScrollPane和其他一些组件的小JDialog。 问题是,scrollPane的滚动有点奇怪...不是原生的Windows 8.1滚动:

enter image description here

我尝试使用look and feel函数作为休闲符号:

    try {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) {
        // Handle exception
    }

并且根本没有帮助。

那么,我使用它是错误的,还是有其他方法可以使用本机操作系统滚动?

另外,如果有人能解释如何自定义卷轴(如箭头图片,或自定义轨道和旋钮),我将不胜感激。

编辑这是我提到的问题所提到的JDialog的代码:

package home;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.ScrollPaneLayout;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;

import net.miginfocom.swing.MigLayout;

import javax.swing.JLabel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Label;
import java.awt.Font;
import java.awt.List;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class ErrorDialog extends JDialog {
JDialog d = this;
Clip errorSoundClip = null;
/**
 * 
 */
private static final long serialVersionUID = -6060765607593894158L;

/**
 * Launch the application.
 */


/**
 * Create the dialog.
 */

public ErrorDialog(final JFrame parent,final String errors) {
    parent.setEnabled(false);
     try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            // Handle exception
        }
    addWindowFocusListener(new WindowFocusListener() {
        public void windowGainedFocus(WindowEvent e) {
        }
        public void windowLostFocus(WindowEvent e) {
            System.out.println("alert!!");
            d.requestFocus();
        }
    });
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    setAlwaysOnTop(true);
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowOpened(WindowEvent e) {

            AudioInputStream audioInputStream = null;
            try {
                audioInputStream = AudioSystem.getAudioInputStream(new File("src\\home\\sounds\\Sad Trombone Sound Effect - Wah Wah Wah FAIL Sound - Fail Horns.wav").getAbsoluteFile());
            } catch (UnsupportedAudioFileException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                errorSoundClip = AudioSystem.getClip();
            } catch (LineUnavailableException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                errorSoundClip.open(audioInputStream);
            } catch (LineUnavailableException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            errorSoundClip.start();

        }
        @Override
        public void windowClosed(WindowEvent e) {
            parent.setEnabled(true);
            errorSoundClip.stop();
        }
    });
    setBounds(new Rectangle(0, 0, 480, 220));
    JScrollPane scrollPane = new JScrollPane();

    getContentPane().add(scrollPane, BorderLayout.CENTER);
    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(450, 150));
    panel.setAutoscrolls(true);
    scrollPane.setViewportView(panel);
    panel.setLayout(new MigLayout("", "[100.00%,grow]", "[][grow][43.93%][]"));
    Label label = new Label("The operation could have not beed completed. Reasons for failling:");
    label.setForeground(Color.RED);
    label.setFont(new Font("Dialog", Font.PLAIN, 15));
    panel.add(label, "cell 0 0,alignx center");



    List list = new List();
    panel.add(list, "cell 0 2,grow");

    JButton btnNewButton = new JButton("Close");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            d.dispose();
        }
    });
    panel.add(btnNewButton, "cell 0 3,growx");
    list.add(errors);
}
}

0 个答案:

没有答案