java - 如何在java中重置面板外观?

标签 java swing jpanel look-and-feel uimanager

Possible Duplicate:
Panel losing color

当我单击激活文件选择器的按钮并添加生成的文件时,面板颜色消失,因为使用 uimanager 将文件选择器显示为窗口选择器。

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;
import javax.swing.JFileChooser;
import javax.swing.plaf.FileChooserUI;

@SuppressWarnings("serial")
public class pan extends JPanel implements DropTargetListener {

    private DefaultListModel listModel = new DefaultListModel();
    private JButton addbutton;
    private JButton removebutton;
    private JButton selectbutton;
    private JButton lockbutton;
    private JButton unlockbutton;

    /**
     * Create the panel.
     */
    public pan() {
        setLayout(null);
        addbutton = new JButton("New button");
        addbutton.setBounds(10, 10, 90, 100);
        addbutton.addActionListener(new Action());
        add(addbutton);

        removebutton = new JButton("New button");
        removebutton.setBounds(110, 10, 90, 100);
        add(removebutton);

        selectbutton = new JButton("New button");
        selectbutton.setBounds(210, 10, 90, 100);
        add(selectbutton);

        lockbutton = new JButton("New button");
        lockbutton.setBounds(310, 10, 90, 100);
        add(lockbutton);

        unlockbutton = new JButton("New button");
        unlockbutton.setBounds(410, 10, 90, 100);
        add(unlockbutton);

        JLabel headerLabel = new JLabel("New label");
        headerLabel.setBorder(new BevelBorder(BevelBorder.RAISED,
            Color.LIGHT_GRAY, Color.GRAY, null, null));
        headerLabel.setUI(new ModifLabelUI());
        headerLabel.setBounds(10, 120, 635, 30);
        add(headerLabel);   
    }


    class Action implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==addbutton){
                JFileChooser filechooser=new JFileChooser();
                filechooser.setMultiSelectionEnabled(true);             
                try {
    UIManager.setLookAndFeel(UIManager
            .getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (InstantiationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (UnsupportedLookAndFeelException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

                filechooser.updateUI();
                filechooser.showOpenDialog(new pan());
                File files=filechooser.getSelectedFile();
                listModel.addElement(files);
        }       
    }
}

如果删除 UImanger,问题就消失了

最佳答案

The documentation ...您可能需要确保 UI 管理器首先设置为使用您想要使用的外观...

编辑具体示例@see This Post

public static void main(String[] args) {
    try {
            // Set System L&F
        UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
    } 
    catch (UnsupportedLookAndFeelException e) {
       // handle exception
    }
    catch (ClassNotFoundException e) {
       // handle exception
    }
    catch (InstantiationException e) {
       // handle exception
    }
    catch (IllegalAccessException e) {
       // handle exception
    }

    new SwingApplication(); //Create and show the GUI.
}

关于java - 如何在java中重置面板外观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13434443/

相关文章:

java - 使用 GridLayout 5x5 更新 JPanel 中的 JLabels 数组

java - 了解 Mockito 验证背后的语义

java - 将长整型标识符均匀分布到桶中

java - 使用 Vectors 字符串刷新 JTable

java - 如何解决这个错误,如果我使用一个类会导致其他类接口(interface)也被感染

java - GridBagLayout 约束不起作用?

java - 在 REST Assured 中,如何检查响应中是否存在某个字段?

java - XML 数字签名 Java

java - 删除(二分查找)并迭代同步列表

java - JPanel 和 JFrame 坐标困惑