当前位置: 首页 > 知识库问答 >
问题:

JLabel和JButton

师承弼
2023-03-14

基本上我有两个账户,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 );
    }

}

共有2个答案

羊光辉
2023-03-14
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();

}
孙洋
2023-03-14

基本上,您需要在“用户输入”中保留对“视图”的引用。你已经有了。

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));

    }

在查看器类上,您需要添加一个方法来访问本地私有组件,例如,我要更改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 ...
}
 类似资料:
  • 我试图根据属性文件中定义的URL数量创建动态JButton和JLabel。 到目前为止,我尝试的是: 在我的Java程序中,我正在读取属性文件,并根据拆分字符串,现在我需要相应地生成按钮和标签。就像

  • 我正在做一个窗口,有一个大的文本区域和一个小的文本区域下。这是我到目前为止得到的: 这是我的代码: 我如何使较大的textarea部分透明,这样我就可以看到背景,我如何移动“比如说:”到较小的textarea前面?

  • 介绍 (Introduction) JLabel类可以显示文本,图像或两者。 通过在其显示区域中设置垂直和水平对齐来对齐标签的内容。 默认情况下,标签在其显示区域中垂直居中。 默认情况下,纯文本标签前沿对齐; 默认情况下,仅图像标签水平居中。 Class 声明 (Class Declaration) 以下是javax.swing.JLabel类的声明 - public class JLabel

  • 问题内容: 我想用setText()方法更新jlabel和jtextfield,但是它不起作用。但是,其余代码仍在工作。代码如下; 在这里,islemSureci是JLabel,而textFiled是JTextField。设置它们后,它不起作用。代码中的所有工作完成后,它们就会出现。请问原因是什么? 问题答案: 我认为您的意思是 “所有工作完成之后” 是从文件(您注释的行)中加载文本。我认为这段代

  • 我有一个9x9面板,它是每个面板都有一个JLabel,所以 我试过: 但是,我发现您很可能不能将“UndoableEditListener”添加到JLabels中。(对吗?) 我看到了一些可以将“UndoableEditListener”添加到JTextPane的示例,所以我想也许我可以创建一个JTextPane窗格[9][9],并将一个textpane添加到每个JLabel(添加到JPanel中

  • 主要内容:1 Java JLabel的介绍,2 Java JLabel的声明,3 Java JLabel的构造方法,4 Java JLabel的方法,5 Java JLabel的案例1,6 Java JLabel的案例21 Java JLabel的介绍 JLabel类的对象是用于将文本放置在容器中的组件。它用于显示一行只读文本。文本可以由应用程序更改,但用户无法直接编辑。它继承了JComponent类。 2 Java JLabel的声明 让我们看一下javax.swing.JLabel类的声明。