簡體   English   中英

JLabel和JButton

[英]JLabel and JButton

基本上,我得到了2個評述,SSPUserInput和SSPViewer。 我想在SSPUserInput類中按下一個按鈕,並在SSPViewer中更改JLabel名稱。 基本上,這是一款剪刀石頭布游戲。 這是我第一次在這里發帖,如果做錯了事,我感到抱歉。

package p3;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SSPUserInput extends JPanel implements ActionListener {
    private JPanel panel = new JPanel();
    private JButton btnRock = new JButton ("Rock");
    private JButton btnScissor = new JButton ("Scissor");
    private JButton btnPaper = new JButton ("Paper");
    private JButton btnNewGame = new JButton ("New game");
    private JButton btnQuit = new JButton ("Quit");
    private Font plainSS14 = new Font("SansSerif", Font.PLAIN, 14);
    private SSPViewer lblHumanChoice;
    private String rock;
    private int scissor;
    private int paper;


public SSPUserInput(SSPViewer lblHumanChoice) {

        this.lblHumanChoice = lblHumanChoice;
    }

    public SSPUserInput(){
            setPreferredSize (new Dimension(400, 200));
            setLayout( null);

            btnRock.setBounds(20, 50, 100, 35);
            btnRock.setFont(plainSS14);
            btnRock.addActionListener(this);

            btnScissor.setBounds(155, 50, 100, 35);
            btnScissor.setFont(plainSS14);
            btnScissor.addActionListener(this);

            btnPaper.setBounds(290, 50, 100, 35);
            btnPaper.setFont(plainSS14);
            btnPaper.addActionListener(this);

            btnNewGame.setBounds(20, 100, 370, 35);
            btnNewGame.setFont(plainSS14);
            btnNewGame.addActionListener(this);

            btnQuit.setBounds(20, 150, 370, 35);
            btnQuit.setFont(plainSS14);
            btnQuit.addActionListener(this);

            add(btnRock);
            add(btnScissor);
            add(btnPaper);
            add(btnNewGame);
            add(btnQuit);



        }

    //   public String getRock() {
    //          return rock;
    //    }

        public void actionPerformed(ActionEvent e) {

            if( e.getSource() == btnRock ) {
                JOptionPane.showMessageDialog(null, "Hello world!");

            }
            else if( e.getSource() == btnScissor){


            }
            else if( e.getSource() == btnPaper){

            }
            else if( e.getSource() == btnNewGame){

            }
        }
    }


    package p3;

    import java.awt.*;
    import javax.swing.*;



    public class SSPViewer extends JPanel {

        private JLabel lblFirst = new JLabel("First to 3!");
        private JLabel lblHuman = new JLabel("Human");
        private JLabel lblComputer = new JLabel("Computer");
        private JLabel lblHumanChoice = new JLabel();
        private JLabel lblComputerChoice = new JLabel();
        private JLabel lblHumanPoints = new JLabel("0");
        private JLabel lblComputerPoints = new JLabel("0");

        private SSPUserInput rock;
        private SSPUserInput scissor;
        private SSPUserInput paper;

        public SSPViewer(SSPUserInput rock, SSPUserInput scissor, SSPUserInput paper) {

            this.rock = rock;
            this.scissor = scissor;
            this.paper = paper;
        }

        public SSPViewer() {

            setLayout(null);
            setPreferredSize(new Dimension(400, 200));

            lblFirst.setBounds(140, 10, 210, 24);
            lblFirst.setFont(new Font("Arial", 2, 24));

            lblHuman.setBounds(100, 75, 210, 17);
            lblHuman.setFont(new Font("Arial", 2, 17));

            lblComputer.setBounds(250, 75, 210, 17);
            lblComputer.setFont(new Font("Arial", 2, 17));

            lblHumanPoints.setBounds(120, 95, 210, 17);
            lblHumanPoints.setFont(new Font("Arial", 2, 17));

            lblComputerPoints.setBounds(280, 95, 210, 17);
            lblComputerPoints.setFont(new Font("Arial", 2, 17));

            lblHumanChoice.setBounds(100, 115, 210, 17);
            lblHumanChoice.setFont(new Font("Arial", 2, 17));
    //      lblHuman.setText(rock.getRock());

            lblComputerChoice.setBounds(260, 115, 210, 17);
            lblComputerChoice.setFont(new Font("Arial", 2, 17));

            add(lblFirst);
            add(lblHuman);
            add(lblComputer);
            add(lblHumanPoints);
            add(lblComputerPoints);
            add(lblHumanChoice);
            add(lblComputerChoice);

        }
        public JLabel getLblHumanChoice() {
            return lblHumanChoice;
        }

    }

package p3;

import javax.swing.*;

public class SSPApp {

    public static void main( String[] args ) {
        SSPPlayer player = new SSPPlayer();
        SSPViewer viewer = new SSPViewer();
        SSPController controller = new SSPController();
        SSPUserInput userInput = new SSPUserInput();
        JFrame frame1 = new JFrame( "SSPViewer" );
        frame1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame1.add( viewer );
        frame1.pack();
        frame1.setVisible( true );
        JFrame frame2 = new JFrame( "SSPUserInput" );
        frame2.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame2.add( userInput );
        frame2.pack();
        frame2.setVisible( true );
    }

}

基本上,您需要在'userInput'上保留對'view'的引用。 而且您已經擁有了它。

public ExampleFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new GridLayout(2, 1));
        setContentPane(contentPane);
        // create a view
        SSPViewer sspViewer = new SSPViewer();
        contentPane.add(sspViewer);
        // pass a reference to the userInput
        contentPane.add(new SSPUserInput(sspViewer));

    }

在查看器類上,您需要添加一種方法來訪問本地私有組件,例如,im將更改JLabel的文本:

public class SSPViewer extends JPanel {
      // code ...

      // setter. lblHuman is a reference here in that class
      // to the view class, so all public members are available
      public void setTextExample (String s){
            this.lblHuman.setText(s);
      }
}

然后在userInput類上,您可以“引用”另一個JPanel上的該屬性:

public void actionPerformed(ActionEvent e) {

        if (e.getSource() == btnRock) {
            JOptionPane.showMessageDialog(null, "Hello world!");
            // accessible now
            lblHumanChoice.setTextExample("changed from other panel");

        } else ...
}

注意:我編輯了答案。
我在這里添加了代碼。 這樣,當您按下ROCK按鈕(例如)時,它將在另一個窗口中顯示其名稱。 我使用了JFrame,並在SSPUserInput類中調用了SSPViewer類。 注意:我認為您想在SSPApp內調用其他一些類(這就是我編寫它的原因),但是即使您運行SSPUserInput,它仍然會彈出兩個窗口。現在它們就像這樣: 在此處輸入圖像描述 在此處輸入圖片說明 SSPUserInput類

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class SSPUserInput extends JFrame {

    private JPanel contentPane;

    JButton PaperButton;
    JButton ScissorButton;
    JButton rockButton;
    SSPViewer viewer = new SSPViewer();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SSPUserInput frame = new SSPUserInput();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SSPUserInput() {
        viewer.frame.setVisible(true); //this code is important
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
        gbl_panel.rowHeights = new int[]{0, 0, 93, 9, 19, 0, 0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        rockButton = new JButton("ROCK");
        GridBagConstraints gbc_rockButton = new GridBagConstraints();
        gbc_rockButton.fill = GridBagConstraints.HORIZONTAL;
        gbc_rockButton.insets = new Insets(0, 0, 5, 5);
        gbc_rockButton.gridx = 1;
        gbc_rockButton.gridy = 3;
        panel.add(rockButton, gbc_rockButton);

        ScissorButton = new JButton("Scissor");
        GridBagConstraints gbc_ScissorButton = new GridBagConstraints();
        gbc_ScissorButton.fill = GridBagConstraints.BOTH;
        gbc_ScissorButton.insets = new Insets(0, 0, 5, 5);
        gbc_ScissorButton.gridx = 3;
        gbc_ScissorButton.gridy = 3;
        panel.add(ScissorButton, gbc_ScissorButton);

        PaperButton = new JButton("PAPER");
        GridBagConstraints gbc_PaperButton = new GridBagConstraints();
        gbc_PaperButton.fill = GridBagConstraints.HORIZONTAL;
        gbc_PaperButton.insets = new Insets(0, 0, 5, 0);
        gbc_PaperButton.gridx = 5;
        gbc_PaperButton.gridy = 3;
        panel.add(PaperButton, gbc_PaperButton);

        JButton btnNewGame = new JButton("New Game");
        GridBagConstraints gbc_btnNewGame = new GridBagConstraints();
        gbc_btnNewGame.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnNewGame.insets = new Insets(0, 0, 5, 5);
        gbc_btnNewGame.gridx = 3;
        gbc_btnNewGame.gridy = 5;
        panel.add(btnNewGame, gbc_btnNewGame);

        JButton btnQuit = new JButton("Quit");
        GridBagConstraints gbc_btnQuit = new GridBagConstraints();
        gbc_btnQuit.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnQuit.insets = new Insets(0, 0, 0, 5);
        gbc_btnQuit.gridx = 3;
        gbc_btnQuit.gridy = 6;
        panel.add(btnQuit, gbc_btnQuit);
        MyListener2 listener = new MyListener2();
        rockButton.addActionListener(listener);
        ScissorButton.addActionListener(listener);
        PaperButton.addActionListener(listener);
    }

    private  class MyListener2 implements ActionListener {



        public void mousePressed(ActionEvent e) {
            //System.out.println(((JButton) e.getSource()).getText());
            //String name = ((JButton) e.getSource()).getText();
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println(((JButton) arg0.getSource()).getText());
            String name = ((JButton) arg0.getSource()).getText();
            viewer.humanWin.setText(name);

        }

        }
}

SSPViewer類

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;


public class SSPViewer {

    JFrame frame;

    JLabel humanWin;
    JLabel computerWin;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SSPViewer window = new SSPViewer();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public SSPViewer() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0, 4, 0, 0, 0, 0, 0, 0};
        gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        JLabel lblFirstTo = new JLabel("First to 3!");
        GridBagConstraints gbc_lblFirstTo = new GridBagConstraints();
        gbc_lblFirstTo.insets = new Insets(0, 0, 5, 5);
        gbc_lblFirstTo.gridx = 5;
        gbc_lblFirstTo.gridy = 2;
        panel.add(lblFirstTo, gbc_lblFirstTo);

        JLabel lblHuman = new JLabel("Human");
        GridBagConstraints gbc_lblHuman = new GridBagConstraints();
        gbc_lblHuman.gridwidth = 3;
        gbc_lblHuman.insets = new Insets(0, 0, 5, 5);
        gbc_lblHuman.gridx = 2;
        gbc_lblHuman.gridy = 4;
        panel.add(lblHuman, gbc_lblHuman);

        JLabel lblComputer = new JLabel("Computer");
        GridBagConstraints gbc_lblComputer = new GridBagConstraints();
        gbc_lblComputer.insets = new Insets(0, 0, 5, 0);
        gbc_lblComputer.gridx = 7;
        gbc_lblComputer.gridy = 4;
        panel.add(lblComputer, gbc_lblComputer);

        humanWin = new JLabel("");
        GridBagConstraints gbc_humanWin = new GridBagConstraints();
        gbc_humanWin.insets = new Insets(0, 0, 0, 5);
        gbc_humanWin.gridx = 3;
        gbc_humanWin.gridy = 6;
        panel.add(humanWin, gbc_humanWin);

        computerWin = new JLabel("");
        GridBagConstraints gbc_computerWin = new GridBagConstraints();
        gbc_computerWin.gridx = 7;
        gbc_computerWin.gridy = 6;
        panel.add(computerWin, gbc_computerWin);
    }

}

SSPApp類

public class SSPApp {
    SSPUserInput s = new SSPUserInput();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM