如何使用ActionListener从按钮调用面板 [英] How to call a panel from a button with ActionListener

查看:48
本文介绍了如何使用ActionListener从按钮调用面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在制作一个简单的程序,该程序可以从一个面板跳转到另一个面板,并使用动作侦听器按钮进行跳转.我要使用哪种方法或操作从一个面板跳到另一个面板?

我尝试使用setVisible(true);在动作侦听器下,但是我只有一个空白屏幕.尝试使用setContentPane(differentPanel);但这是行不通的.

  ackage Com.conebind.Characters;进口Com.conebind.Tech.TechA16;导入Com.conebind.Overviews.OverviewA16;导入javax.swing.JFrame;导入javax.swing.JPanel;导入javax.swing.JLabel;导入javax.swing.*;导入java.awt.*;导入java.awt.event.ActionEvent;导入java.awt.event.ActionListener;公共类Char_A16扩展了JFrame {私有JButton combosButton16;私有JButton techButton16;私有JButton概述Button16;私人JLabel Image16;专用的JPanel面板16;私人JPanel panelOverviewA16;公共Char_A16(){overviewButton16.addActionListener(new ActionListener(){@Override公共无效actionPerformed(ActionEvent e){OverviewA16 overview16 =新的OverviewA16();overview16.setVisible(true);overview16.pack();overview16.setContentPane(new Char_A16().panelOverviewA16);}});techButton16.addActionListener(new ActionListener(){@Override公共无效actionPerformed(ActionEvent e){//去做}});}私人void createUIComponents(){Image16 = new JLabel(new ImageIcon("Android 16.png"));}公共静态void主函数(String [] args){JFrame frame = new JFrame("Android 16");frame.setContentPane(new Char_A16().panel16);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.pack();frame.setVisible(true);}} 

setContentPane(OverviewA16)不起作用,因为没有定义面板的对象.

解决方案

请检查

主窗体具有一种在其中显示的2种窗体之间切换的方法:

  public void showPanel(String id){最后的CardLayout cl =(CardLayout)cardPanel.getLayout();cl.show(cardPanel,id);} 

在主表单初始化期间,这两种表单都被添加到卡布局中:

  FormOne一个= new FormOne();one.setParentForm(this);cardPanel.add(one.getPanel(),FORM_ONE);FormTwo二=新FormTwo();two.setParentForm(this);cardPanel.add(two.getPanel(),FORM_TWO);最后的CardLayout cl =(CardLayout)cardPanel.getLayout();cl.show(cardPanel,FORM_ONE); 

使用 setParentForm()方法将对主父表单的引用传递给这两个表单,以便可以访问 FormOne FormTwo MainForm showPanel()方法.

在更基本的情况下,您可能会有一个按钮或其他控件来切换表格直接位于 MainForm 上,那么您可能不需要将主窗体引用传递给子窗体,但是根据您的应用程序逻辑,它仍然可能有用.

So I'm making a simple program that jumps from panel to panel and am using an actionlistener Button to make the jump. What kind of method or operation do I use to jump from panel to panel?

I tried to use setVisible(true); under the action listener, but I get just a blanks screen. Tried using setContentPane(differentPanel); but that doesn't work.

ackage Com.conebind.Characters;
import Com.conebind.Tech.TechA16;
import Com.conebind.Overviews.OverviewA16;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Char_A16 extends JFrame {

    private JButton combosButton16;
    private JButton techButton16;
    private JButton overviewButton16;
    private JLabel Image16;
    private JPanel panel16;
    private JPanel panelOverviewA16;
    public Char_A16() {
        overviewButton16.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                OverviewA16 overview16 = new OverviewA16();
             overview16.setVisible(true);
             overview16.pack();
             overview16.setContentPane(new Char_A16().panelOverviewA16);
            }
        });
        techButton16.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //Todo
            }
        });

        }

    private void createUIComponents(){

        Image16 = new JLabel(new ImageIcon("Android 16.png"));
    }
    public static void main (String[] args){
        JFrame frame = new JFrame("Android 16");
        frame.setContentPane(new Char_A16().panel16);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);}
}

The setContentPane(OverviewA16) doesn't work because there's not an object that defines the panel.

解决方案

Please check this demo project showing how to use CardLayout with IntelliJ IDEA GUI Designer.

The main form has a method that switches between 2 forms displayed inside it:

  public void showPanel(String id) {
    final CardLayout cl = (CardLayout) cardPanel.getLayout();
    cl.show(cardPanel, id);
  }

Both forms are added to the card layout during the main form initialization:

FormOne one = new FormOne();
one.setParentForm(this);
cardPanel.add(one.getPanel(), FORM_ONE);
FormTwo two = new FormTwo();
two.setParentForm(this);
cardPanel.add(two.getPanel(), FORM_TWO);
final CardLayout cl = (CardLayout) cardPanel.getLayout();
cl.show(cardPanel, FORM_ONE);

A reference to the main parent form is passed to these 2 forms using setParentForm() method so that FormOne and FormTwo classes can access the showPanel() method of the MainForm.

In a more basic case you may have a button or some other control that switches the forms located directly on the MainForm, then you may not need passing the main form reference to the subforms, but it can be still useful depending on your app logic.

这篇关于如何使用ActionListener从按钮调用面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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