使用方法更改JComponents样式

时间:2017-02-06 16:36:09

标签: java swing

我有使用我的方法的问题" changeStyle"改变我的JButtons的外观。我做了一个方法,它将改变我的JButton的背景,边框和不透明度。我尝试使用另一种方法来缩短第一个代码,但它返回一个错误,"方法changeStyle()未定义为JButton类型。"

我试图缩短的代码:

public void buttonChange(){
        rock.setBackground(defaultColor);
        rock.setBorder(defaultBorder);
        rock.setOpaque(false);
        paper.setBackground(defaultColor);
        paper.setBorder(defaultBorder);
        paper.setOpaque(false);
        scissor.setBackground(defaultColor);
        scissor.setBorder(defaultBorder);
        scissor.setOpaque(false);

            switch(main.playerChoice){
            case 1:
                rock.setBackground(Color.cyan);
                rock.setBorder(BorderFactory.createLineBorder(Color.blue, 2, true));
                rock.setOpaque(true);
                break;
            case 2:
                paper.setBackground(Color.cyan);
                paper.setBorder(BorderFactory.createLineBorder(Color.blue, 2, true));
                paper.setOpaque(true);
                break;
            case 3:
                scissor.setBackground(Color.cyan);
                scissor.setBorder(BorderFactory.createLineBorder(Color.blue, 2, true));
                scissor.setOpaque(true);
                break;
            default:
                rock.setBackground(defaultColor);
                rock.setBorder(defaultBorder);
                rock.setOpaque(false);
                paper.setBackground(defaultColor);
                paper.setBorder(defaultBorder);
                paper.setOpaque(false);
                scissor.setBackground(defaultColor);
                scissor.setBorder(defaultBorder);
                scissor.setOpaque(false);
                break;
        }
    }

这是我为缩短buttonChange所做的功能:

package RPS;

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.border.Border;

public class changeStyle extends JButton{
    public changeStyle(Color color, Border border, Boolean bol){
        setBackground(color);
        setBorder(border);
        setOpaque(bol);
    }
}

整个代码:

package RPS;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ThreadLocalRandom;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import RPS.changeStyle;

public class main extends JFrame{

    private static String version = "V. 0.02";


    private JPanel contentPane;

    JPanel titlePane = new JPanel();

    JLabel title = new JLabel("Rock Paper Scissor " + version);

    JSplitPane split1 = new JSplitPane();

    JButton exit = new JButton("Exit");
    JButton play = new JButton("Play");

    JSplitPane split2 = new JSplitPane();
    JSplitPane split3 = new JSplitPane();
    JSplitPane split4 = new JSplitPane();

    JButton rock = new JButton("Rock");
    JButton paper = new JButton("Paper");
    JButton scissor = new JButton("Scissor");

    static int playerChoice;
    static int computerChoice;

    static int playerScore;
    static int computerScore;

    static String finalScore;

    JLabel plScore = new JLabel("Player Score: " + playerScore);
    JLabel compScore = new JLabel("Computer Score: " + computerScore);
    JButton reset = new JButton("Reset");

    Color defaultColor = new JButton().getBackground();
    Border defaultBorder = new JButton().getBorder();

    public static void main(String[] args) {

            try {
                main frame = new main();
                frame.setVisible(true);
                frame.setResizable(false);
            } catch (Exception e) {
                e.printStackTrace();
            }



        }


    public main() {

        setTitle("Rock Paper Scissor " + version);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setLayout(new BorderLayout());

        add(titlePane, BorderLayout.PAGE_START);
        titlePane.add(title);
        titlePane.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

        add(split2, BorderLayout.LINE_START);
        split2.setOrientation(JSplitPane.VERTICAL_SPLIT);
        split2.setEnabled(false);
        split2.setTopComponent(plScore);
        split2.setBottomComponent(rock);
        split2.setDividerLocation(this.getHeight()/4);
        split2.setDividerSize(0);

        rock.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                main.playerChoice = 1;
                //System.out.println(main.playerChoice);
                buttonChange();
            }

        });

        plScore.setFont(plScore.getFont().deriveFont(12F));
        plScore.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        add(split3, BorderLayout.CENTER);
        split3.setOrientation(JSplitPane.VERTICAL_SPLIT);
        split3.setEnabled(false);
        split3.setTopComponent(compScore);
        split3.setBottomComponent(paper);
        split3.setDividerLocation(this.getHeight()/4);
        split3.setDividerSize(0);

        paper.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                main.playerChoice = 2;
                //System.out.println(main.playerChoice);
                buttonChange();
            }
        });

        compScore.setFont(compScore.getFont().deriveFont(12F));
        compScore.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        add(split4, BorderLayout.LINE_END);
        split4.setOrientation(JSplitPane.VERTICAL_SPLIT);
        split4.setEnabled(false);
        split4.setTopComponent(reset);
        split4.setBottomComponent(scissor);
        split4.setDividerLocation(this.getHeight()/4);
        split4.setDividerSize(0);
        reset.setSize(25, 75);

        scissor.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                main.playerChoice = 3;
                //System.out.println(main.playerChoice);
                buttonChange();
            }
        });
        reset.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                finalScore = "";
                computerChoice = 0;
                playerChoice = 0;
                playerScore = 0;
                computerScore = 0;
                plScore.setText("Player Score: " + playerScore);
                plScore.setForeground(Color.black);
                compScore.setText("Computer Score: " + computerScore);
                compScore.setForeground(Color.black);
                buttonChange();
            }
        });

        //add(rock, BorderLayout.LINE_START);
        split2.setPreferredSize(new Dimension(this.getWidth()/3 -6,0));
        //add(paper, BorderLayout.CENTER);
        split3.setPreferredSize(new Dimension(this.getWidth()/3 -6,0));
        //add(scissor, BorderLayout.LINE_END);
        split4.setPreferredSize(new Dimension(this.getWidth()/3 -6,0));

        add(split1, BorderLayout.PAGE_END);
        split1.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        split1.setEnabled(false);
        split1.setDividerLocation(this.getWidth()/2);
        split1.setDividerSize(0);
        split1.setLeftComponent(play);
        split1.setRightComponent(exit);

        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });

        play.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                int rand = ThreadLocalRandom.current().nextInt(1, 4);
                main.computerChoice = rand;
                //System.out.println(main.computerChoice);
                plScore.setForeground(Color.black);
                compScore.setForeground(Color.black);

                if(main.playerChoice == main.computerChoice){
                    finalScore = "Tie!";
                    JOptionPane.showMessageDialog(null, finalScore, "Result", JOptionPane.INFORMATION_MESSAGE);
                }else if(main.playerChoice == 1){
                    switch(main.computerChoice){
                    case 2:
                        finalScore = "Computer Wins!";
                        break;
                    case 3:
                        finalScore = "Player Wins!";
                    }
                }else if(main.playerChoice == 2){
                    switch(main.computerChoice){
                    case 3:
                        finalScore = "Computer Wins!";
                        break;
                    case 1:
                        finalScore = "Player Wins!";
                    }
                }else if(main.playerChoice == 3){
                    switch(main.computerChoice){
                    case 2:
                        finalScore = "Computer Wins!";
                        break;
                    case 1:
                        finalScore = "Player Wins!";
                    }
                }else{
                    JOptionPane.showMessageDialog(contentPane, "You didn't choose!!!", "Error", JOptionPane.ERROR_MESSAGE);
                }
                while(finalScore == "Player Wins!"){
                    playerScore+=1;
                    plScore.setText("Player Score: " + playerScore);
                    //INSERT POP UP 
                    JOptionPane.showMessageDialog(contentPane, finalScore, "Result", JOptionPane.INFORMATION_MESSAGE);
                    plScore.setForeground(Color.green);
                    finalScore = "";
                }
                while(finalScore == "Computer Wins!"){
                    computerScore+=1;
                    compScore.setText("Computer Score: " + computerScore);
                    //INSTERT POP UP
                    JOptionPane.showMessageDialog(contentPane, finalScore, "Result", JOptionPane.INFORMATION_MESSAGE);
                    compScore.setForeground(Color.red);
                    finalScore = "";
                }

                main.playerChoice = 0;
            }
        });

    }

    public void buttonChange(){
        rock.setBackground(defaultColor);
        rock.setBorder(defaultBorder);
        rock.setOpaque(false);
        paper.setBackground(defaultColor);
        paper.setBorder(defaultBorder);
        paper.setOpaque(false);
        scissor.setBackground(defaultColor);
        scissor.setBorder(defaultBorder);
        scissor.setOpaque(false);

            switch(main.playerChoice){
            case 1:
                rock.setBackground(Color.cyan);
                rock.setBorder(BorderFactory.createLineBorder(Color.blue, 2, true));
                rock.setOpaque(true);
                break;
            case 2:
                paper.setBackground(Color.cyan);
                paper.setBorder(BorderFactory.createLineBorder(Color.blue, 2, true));
                paper.setOpaque(true);
                break;
            case 3:
                scissor.setBackground(Color.cyan);
                scissor.setBorder(BorderFactory.createLineBorder(Color.blue, 2, true));
                scissor.setOpaque(true);
                break;
            default:
                rock.setBackground(defaultColor);
                rock.setBorder(defaultBorder);
                rock.setOpaque(false);
                paper.setBackground(defaultColor);
                paper.setBorder(defaultBorder);
                paper.setOpaque(false);
                scissor.setBackground(defaultColor);
                scissor.setBorder(defaultBorder);
                scissor.setOpaque(false);
                break;
        }
    }

}

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

扩展类并添加功能以使父类不能使用该功能,只有新类的实例及其子级才能访问它,因此当您使用{{1}时},JButton rock = new JButton("Rock");将不具有rock的功能(它不是changeStyle的实例,那怎么能呢?)

我们可以进入关于多态和对象转换的语义,但是我们只是说你应该使用

changeStyle

现在,您可以调用changeStyle rock = new changeStyle(); 方法。注意,如果您希望在创建文本changeStyle时将文本String传递给它,则需要覆盖按钮的构造函数

Java命名约定还会建议类名应该以大写字符开头,因此changeStyle应该是ChangeStyle,虽然它看起来不是什么大问题,但它会使代码更容易为他人阅读,并为您阅读其他人的代码,这只是一个使用的好习惯