Java在同一JFrame中单击按钮时会摆动显示JPanel吗? [英] Java swings-displaying JPanel on button click in the same JFrame?

查看:80
本文介绍了Java在同一JFrame中单击按钮时会摆动显示JPanel吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,请解释一下在同一框架中显示JPanel和ON按钮的情况.

我已经显示了有关按钮单击的新JFrame,但未显示JPanel

这是我的代码,但是没有用!


Hi, everyone, please explain displaying JPanel, ON button click in the same Frame

i''ve displayed a new JFrame with respect to button click but not JPanel

here''s my code but it didn''t work!


import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class buttons extends JPanel{
    public static void main (String args []) {
    
        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EX…
        frame.setTitle("BULLS EYE");
        JPanel panel = new JPanel();
        JButton button1 = new JButton("click me");
        JButton button2 = new JButton("second button");
        
        
        panel.add(button1);
        panel.add(button2);
        
        frame.add(panel);
        
        button1.addActionListener( new Action1());
        button2.addActionListener(new Action2());
    }
    static class Action1 implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JFrame Frame1 = new JFrame();
            JPanel panel1 = new JPanel();
            JLabel label1 = new JLabel("OM NAMA SHIVAYA");
            Frame1.setVisible(true);
            Frame1.setSize(300,300);
            Frame1.setDefaultCloseOperation(JFrame.E…
            panel1.add(label1);
            Frame1.add(panel1);
        }
    
    }
        
    static class Action2 implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JPanel panel2 = new JPanel();
            
            JLabel label2 = new JLabel(" NARAYANAYA GANAPATHI ");
            GridBagConstraints c = new GridBagConstraints();
            c.gridx=0;
            c.gridy=0;
            panel2.add(label2,c);
            panel2.setSize(50,50);
            
        }
    }
    
}

推荐答案

此代码非常混乱.

-您要扩展JPanel,然后使用JFrame来构建您的GUI.
删除扩展JPanel"或使用扩展JFrame"并通过"this"进行引用.

This code is total chaos.

- you are extending JPanel and then using JFrame to build your GUI.
delete the "extends JPanel" or use "extends JFrame" and refer to it via "this".

public class MyFrame extends JFrame{

  public myFrame(){
    // create GUI
    this.setSize(500, 500);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EX…
    this.setTitle("BULLS EYE");

    JButton oButton = new JButton("Click me hard!");
    oButton.addActionListener(new ButtonAction());
    this.add(oButton);
  }

  public static void main (String args []) {
    // start action here by creating the JFrame
    MyFrame oFrame = new MyFrame();
    oFrame.setVisible(true);
  }

}



-您正在为Action创建静态类,因为您太懒了,无法将JFrame移出main方法.
改变它.



- you are creating static classes for the Actions because you are too lazy to move the JFrame out of the main method.
Change that.

public class MyFrame extends JFrame{
  // ...

  public ButtonAction implements ActionListener{
    public void actionPerformed(ActionEvent e) {

    }
  }
}



对于动作:
您可以通过oEvent.getSource()引用原始对象(JButton).您将需要强制转换给定的Object来重新获得JButton:



For the Action:
You can refer to the original object (the JButton) via oEvent.getSource(). You will need to cast that given Object that to get the JButton back:

public ButtonAction implements ActionListener{
  public void actionPerformed(ActionEvent oEvent) {
      if(oEvent.getSource instanceOf JButton){ // check if Action is triggered by JButton
        JButton oButton = (JButton)oEvent.getSource(); // get Button (if needed)
        
        MyFrame.this.doFunnyThings(); // call function of the JFrame
      }
    }
  }
}



您还可以为按钮定义一个ActionCommand,以标识该操作的触发器.
请为此在网络上进行一些搜索.



You can also define a ActionCommand for the button to identify the trigger of the action.
Please do a little search on the web for that.


您将在动作侦听器中创建对象的新实例.但是,一旦这些方法终止,这些对象就会超出范围并被销毁.您需要使对象成为主类的一部分.
You are creating new instances of objects within your action listeners. However, as soon as these methods terminate those objects go out of scope and are destroyed. You need to make the objects part of the main class.


这篇关于Java在同一JFrame中单击按钮时会摆动显示JPanel吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆