标签或文本更改时帧重置

时间:2018-04-17 05:43:37

标签: java

我目前在某些javaFrame应用程序中遇到了一些问题。这是一个简单的游戏,球在四处奔跑,有一个按钮可以启动和停止球。问题是每当我在球运行时更改标签或按钮的文本时,框架会重置(球重置在最初的位置)并且我不希望发生这种情况。我通过从侧面拖动来创建框架和面板(我没有编写代码来创建框架和面板)。我不知道这个网站的所有规则,我不知道是否允许,但我找不到任何方式向您显示这个,所以我在最后链接了2个短的YouTube视频,所以你可以看到它是什么(我为此道歉。)

我在框架中做了什么:

  1. 创建了2个面板。
  2. 在panel1(上面一个)上添加了一个按钮,并在其上添加了球形图像。
  3. 在面板2(下面一个)上添加了2个按钮,分别是Start / Stop和changeLabel。
  4. 我的NewJFrame类:

    package view;
    
    import controller.BallAction;
    
    public class NewJFrame extends javax.swing.JFrame {
    
        private BallAction ba;
        private boolean isBallRolling = false;
        private int labelAsciiValue = 65;
    
        public NewJFrame() {
            initComponents();
            ba = new BallAction(ball);
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            ball = new javax.swing.JButton();
            jPanel2 = new javax.swing.JPanel();
            jButton2 = new javax.swing.JButton();
            jButton1 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Ball Game");
    
            ball.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/top.png"))); // NOI18N
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(ball, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(515, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(ball, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(291, Short.MAX_VALUE))
            );
    
            jButton2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
            jButton2.setText("Start");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
            jButton1.setText("Change Label");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
    
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85)
                .addComponent(jButton1)
                .addGap(107, 107, 107)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(35, Short.MAX_VALUE))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
            );
            layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
        //Start-Stop Button
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            if(isBallRolling){
                ba.stop();
                jButton2.setText("Start");
                isBallRolling = false;
            }
            else{
                ba.start();
                jButton2.setText("Stop");
                isBallRolling = true;
            }
        }                                        
        //Change Label Button
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            jLabel1.setText(String.valueOf((char)(labelAsciiValue++)));
        }                                        
    
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton ball;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        // End of variables declaration                   
    }
    

    我的BallAction类:

    package controller;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.Timer;
    
    public class BallAction {
    
        private Timer time;
        private final ClockListener clock = new ClockListener();
        private JButton ball;
    
        public BallAction(JButton ball){
            this.ball=ball;
            time = new Timer(20,clock);
        }
    
        public void start(){
            time.start();
        }
    
        public void stop(){
            time.stop();
        }
    
        //If ball.x <= panel.x go right else ball.x = 0.
        public void update(){
            if(ball.getLocation().x<=ball.getParent().getSize().width-100)
                ball.setLocation(ball.getLocation().x+5,ball.getLocation().y);
            else{
                ball.setLocation(0,ball.getLocation().y);
            }
        }
    
        private class ClockListener implements ActionListener{
    
            @Override
            public void actionPerformed(ActionEvent e) {
                update();
            }   
        }
    }
    

    框架无需更改任何内容:https://www.youtube.com/watch?v=yC_8tyUmal0

    更改文字的框架:https://www.youtube.com/watch?v=zGvhCnRRTtw

    编辑:

    如果我自己创建框架,它可以正常工作。

    为什么这样请帮助我谢谢你。

    我自己的框架:

    package ballgame0;
    
    import controller.BallAction;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Action;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class BallGame0 {
    
        private static boolean isBallRunning = false;
        private static int labelAsciiValue = 65;
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("New Frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(1000,800);
            frame.setVisible(true);
            JPanel panel= new JPanel(null);
            frame.add(panel);
    
            JLabel changeLabel = new JLabel();
            changeLabel.setSize(100,50);
            changeLabel.setLocation(500,650);
            changeLabel.setFont(new Font("Tahoma", 0, 36));
    
            JButton buttonLabel = new JButton("Change Label");
            buttonLabel.setSize(150,40);
            buttonLabel.setLocation(300,650);
    
            JButton button = new JButton("asd");
            button.setSize(70,70);
    
            JButton button1 = new JButton("Start");
            button1.setSize(100,60);
            button1.setLocation(100,650);
    
            panel.add(changeLabel);
            panel.add(buttonLabel);
            panel.add(button);
            panel.add(button1);
            BallAction ba = new BallAction(button);
    
            ActionListener alis = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                changeLabel.setText(String.valueOf((char)labelAsciiValue++));
                }
            };
    
            buttonLabel.addActionListener(alis);
    
            ActionListener al = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(isBallRunning){
                        ba.stop();
                        button1.setText("Start");
                        isBallRunning = false;
                    }else{
                        ba.start();
                        button1.setText("Stop");
                        isBallRunning = true;
                    }
                }
            };
            button1.addActionListener(al);
        }
    }
    

0 个答案:

没有答案