如何在Java中更新LookAndFeel?

时间:2013-06-10 20:56:06

标签: java swing repaint look-and-feel

我编写了一个程序,首先在测试GUI上更新LookAndFeel。用户单击“选择”按钮后,LookAndFeel将传递给主GUI方法,该方法使用该选择创建新窗口。

工作正常。虽然,当用户循环浏览各种LookAndFeel时,测试GUI并不真正更新并显示更改。每次单击更改按钮时,我都会更新并显示当前的LookAndFeel(和repain();),所以我知道它正在改变。此外,当您点击选择时,新GUI将使用您当时所处的任何选项创建(即使测试GUI窗口未显示更改)。

我做错了什么?

    import javax.swing.*;           //UI.Manager <-- available
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.UIManager.*;
    import javax.imageio.ImageIO;
    import java.io.IOException;
    import java.net.URL;

    public class gui1 {
    static JFrame frame1, testFrame;
    static Container pane, testPane;
    static JButton btnConnect, btnDisconnect, changeButton, selectButton;
    static JLabel lblServer, lblUsername, lblPassword, lblPort, testLabel;
    static JTextField txtServer, txtUsername, txtPassword, txtPort;
    static Insets insets;
    static JTextArea area, testArea;
    public JScrollPane scroller, testScroller;
    static int  currentIndex;


URL frameImage;
Image img;

static boolean LookAndFeelSelected;

public static void main(String[] args){
    gui1 begin = new gui1();
}

public gui1() {

    //build our test gui & components
    testFrame = new JFrame("Select which LookAndFeel you prefer..");
    testFrame.setSize(375, 250);
    testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    testLabel = new JLabel("Text Example", JLabel.CENTER);
    testArea = new JTextArea(300,200);
    testPane = testFrame.getContentPane();
    changeButton = new JButton("Change");
    selectButton = new JButton("Select");
    testScroller = new JScrollPane(testArea);
    testScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    testScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    testPane.add(changeButton);
    testPane.add(selectButton);
    testFrame.getContentPane().add(BorderLayout.WEST, changeButton);
    testFrame.getContentPane().add(BorderLayout.EAST, selectButton);
    testFrame.getContentPane().add(BorderLayout.CENTER, testScroller);
    testFrame.getContentPane().add(BorderLayout.NORTH, testLabel);

    changeButton.addActionListener(new changeListener());
    selectButton.addActionListener(new selectListener());

    testFrame.setVisible(true);

    while(!LookAndFeelSelected) {
        try{
            Thread.sleep(500);
        }catch(InterruptedException ex) {ex.printStackTrace();}
    }

    System.out.println("Selected!");
    //start the real GUI with selected LookAndFeel
    testFrame.dispose();
    buildGui(UIManager.getLookAndFeel());
}
public void buildGui(LookAndFeel chosen){
    //The frame and panel
    frame1 = new JFrame("Sample GUI Application");
    frame1.setSize(800, 230);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pane = frame1.getContentPane();
    pane.setLayout(null);
    insets = pane.getInsets();

    //Set the image
    try{
        frameImage = new URL("http://i.imgur.com/jMaYO1f.jpg");
        img = ImageIO.read(frameImage);
    }catch(IOException ex){ex.printStackTrace();}
    frame1.setIconImage(img);

    //Construct our objects for gui
    btnConnect = new JButton("Connect");
    btnDisconnect = new JButton("Disconnect");
    lblServer = new JLabel("Remote Host: ");
    lblUsername = new JLabel("Username: ");
    lblPassword = new JLabel("Password: ");
    lblPort = new JLabel("Port #");
    txtServer = new JTextField(10);
    txtPassword = new JTextField(10);
    txtUsername = new JTextField(10);
    txtPort = new JTextField(5);
    area = new JTextArea(700, 125);
    area.setLineWrap(true);
    area.setEditable(false);
    scroller = new JScrollPane(area);
    scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    //Add components to the pane
    pane.add(lblServer);
    pane.add(lblServer);
    pane.add(lblPassword);
    pane.add(lblUsername);
    pane.add(lblPort);
    pane.add(txtServer);
    pane.add(txtPort);
    pane.add(txtPassword);
    pane.add(txtUsername);
    pane.add(btnConnect);
    pane.add(btnDisconnect);
    pane.add(scroller);  //includes area and scroller!


    //Arrange our components in the pane
    lblServer.setBounds(insets.left + 5, insets.top + 5, lblServer.getPreferredSize().width, lblServer.getPreferredSize().height);
    txtServer.setBounds(lblServer.getX() + lblServer.getWidth() + 5, insets.top + 5, txtServer.getPreferredSize().width, txtServer.getPreferredSize().height);
    lblUsername.setBounds(txtServer.getX() + txtServer.getWidth() + 5, insets.top + 5, lblUsername.getPreferredSize().width, lblUsername.getPreferredSize().height);
    txtUsername.setBounds(lblUsername.getX() + lblUsername.getWidth() + 5, insets.top + 5, txtUsername.getPreferredSize().width, txtUsername.getPreferredSize().height);
    lblPassword.setBounds(txtUsername.getX() + txtUsername.getWidth() + 5, insets.top + 5, lblPassword.getPreferredSize().width, lblPassword.getPreferredSize().height);
    txtPassword.setBounds(lblPassword.getX() + lblPassword.getWidth() + 5, insets.top + 5, txtPassword.getPreferredSize().width, txtPassword.getPreferredSize().height);
    lblPort.setBounds(txtPassword.getX() + txtPassword.getWidth() + 5, insets.top + 5, lblPort.getPreferredSize().width, lblPort.getPreferredSize().height);
    txtPort.setBounds(lblPort.getX() + lblPort.getWidth() + 5, insets.top + 5, txtPort.getPreferredSize().width, txtPort.getPreferredSize().height);
    btnConnect.setBounds(txtPort.getX() + txtPort.getWidth() + 5, insets.top + 5, btnConnect.getPreferredSize().width, btnConnect.getPreferredSize().height);
    btnDisconnect.setBounds(insets.left + 5, lblServer.getY() + lblServer.getHeight() + 15, btnDisconnect.getPreferredSize().width, btnDisconnect.getPreferredSize().height);
    scroller.setBounds(insets.left + 5, btnDisconnect.getX() + btnDisconnect.getHeight() + 33, 760, 125);

    //Add button listeners
    btnConnect.addActionListener(new connectListener());
    btnDisconnect.addActionListener(new disconnectListener());

    //Change the Look & Feel
    try {
        UIManager.setLookAndFeel(chosen);
    } catch (Exception ex) {ex.printStackTrace();}

    frame1.setVisible(true);
    frame1.setResizable(false);
}


public static class connectListener implements ActionListener {
    public void actionPerformed(ActionEvent event){
        String toField = String.format("ServerIP: %s \t Username: %s \t Password: %s \t Port: %s\n", txtServer.getText(), txtUsername.getText(),
                txtPassword.getText(), txtPort.getText());
        area.append(toField);
    }
}

public static class disconnectListener implements ActionListener {
    public void actionPerformed(ActionEvent event){
        area.append("Disconnected ..\n");
    }
}
// Here only for initial LookAndFeel selection!
public static class changeListener implements ActionListener {
    public void actionPerformed(ActionEvent event){
        LookAndFeelInfo[] lafArray = UIManager.getInstalledLookAndFeels();  //not actual LookAndFeel(just name), thus use LookAndFeelInfo

        try {
            if (currentIndex == lafArray.length - 1) {
                currentIndex = 0;
                System.out.println("You have seen all Look and Feels, starting over .. \n\n");
            }
            UIManager.setLookAndFeel(lafArray[currentIndex++].getClassName());
            testFrame.getContentPane().validate();
            testFrame.getContentPane().repaint();    //repaint the frame to update changes

        }catch(Exception ex) {ex.printStackTrace();}
        String LookAndFeelName = String.format("Changed to: %s",UIManager.getLookAndFeel().getClass());     //gets current and displays name
        System.out.println(LookAndFeelName);
    }
}

public static class selectListener implements ActionListener {
    public void actionPerformed(ActionEvent event){
        LookAndFeelSelected = true;
    }
}

}

1 个答案:

答案 0 :(得分:2)

这不是一个重新粉刷的问题,外观和感觉不仅仅是绘画颜色。查看本教程http://www.roseindia.net/java/example/java/swing/GettingAndSettingLAF.shtml

下面您可以在按钮点击更改时更改外观:

testFrame.getContentPane().validate();
testFrame.getContentPane().repaint();

SwingUtilities.updateComponentTreeUI(testFrame);