从按钮打开一个新的JFrame [英] Opening a new JFrame from a Button

查看:141
本文介绍了从按钮打开一个新的JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过点击一个按钮(btnAdd)打开一个新的JFrame;我试图创建一个ActionListener,但我有没有运气;单击该按钮时,code运行,但没有任何反应。问题的方法是在以下code中的最后两个。任何帮助深表AP preciated!

 包AdvancedWeatherApp;进口java.awt.BorderLayout中;
进口java.awt.Color中;
进口java.awt.Component中;
进口java.awt.Container中;
进口java.awt.Dimension中;
进口java.awt.FlowLayout中;
进口java.awt.Font中;
进口java.awt.Toolkit中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;进口javax.swing.BoxLayout中;
进口javax.swing.JFrame中;
进口javax.swing.JPanel中;
进口javax.swing.SwingConstants中;
进口javax.swing.event.ListSelectionListener;进口weatherforecast.FetchWeatherForecast;公共类大型机扩展JFrame的实现ListSelectionListener {初始化= FALSE私人布尔;
私人行为动作=新的操作();私人javax.swing.JScrollPane中jspFavouritesList =新javax.swing.JScrollPane中的();
私人javax.swing.DefaultListModel中的<串GT; ListModel的=新javax.swing.DefaultListModel中的<串GT;();
私人javax.swing.JList中的<串GT; favouritesList =新javax.swing.JList中的<串GT;(
        ListModel的);私人javax.swing.JLabel中lblAcknowledgement =新javax.swing.JLabel中的();
私人javax.swing.JLabel中lblTitle =新javax.swing.JLabel中的();私人javax.swing.JButton中btnAdd =新javax.swing.JButton中的();
私人javax.swing.JButton中btnRemove =新javax.swing.JButton中的();公共无效的initialize(){
    initializeGui();
    initializeEvents();
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}/ **
 *
 * /
私人无效initializeGui(){
    如果(初始化)
        返回;
    初始化= TRUE;
    this.setSize(500,400);    尺寸windowSize = this.getSize();
    屏幕尺寸尺寸= Toolkit.getDefaultToolkit()getScreenSize()。
    this.setLocation(screenSize.width / 2 - windowSize.width / 2,
            screenSize.height / 2 - windowSize.height / 2);
    容器窗格= this.getContentPane();
    pane.setLayout(新的BorderLayout());
    的setLayout(新的BorderLayout());
    的setTitle(喜爱的天气位置);    JPanel的jpSouth =新JPanel();
    jpSouth.setLayout(新的FlowLayout());    JPanel的jpNorth =新JPanel();
    jpNorth.setLayout(新的FlowLayout());    JPanel的jpCenter =新JPanel();
    jpCenter.setLayout(新的BoxLayout(jpCenter,BoxLayout.PAGE_AXIS));    JPanel的jpEast =新JPanel();
    JPanel的jpWest =新JPanel();    。的getContentPane()的setBackground(Color.WHITE);
    jpEast.setBackground(Color.WHITE);
    jpWest.setBackground(Color.WHITE);
    jpCenter.setBackground(Color.WHITE);    的getContentPane()加(jspFavouritesList)。
    jpCenter.add(jspFavouritesList);
    jspFavouritesList.setViewportView(favouritesList);
    favouritesList
            .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    favouritesList.addListSelectionListener(本);    jpCenter.add(btnAdd);
    jpCenter.add(btnRemove);
    jpCenter.setAlignmentY(CENTER_ALIGNMENT);
    btnAdd.setText(添加位置);
    btnAdd.setAlignmentX(Component.CENTER_ALIGNMENT);
    btnAdd.setFont(新字体(宋体,Font.PLAIN,18));    jpCenter.add(btnRemove);
    btnRemove.setText(删除位置);
    btnRemove.setAlignmentX(Component.CENTER_ALIGNMENT);
    btnRemove.setFont(新字体(宋体,Font.PLAIN,18));    的getContentPane()加(jpEast,BorderLayout.EAST)。
    的getContentPane()加(jpWest,BorderLayout.WEST)。    的getContentPane()加(jpSouth)。
    jpSouth.add(lblAcknowledgement);
    加(lblAcknowledgement,BorderLayout.SOUTH);
    lblAcknowledgement.setText(FetchWeatherForecast.getAcknowledgement());
    lblAcknowledgement.setHorizo​​ntalAlignment(SwingConstants.CENTER);
    lblAcknowledgement.setFont(新字体(宋体,Font.ITALIC,12));    的getContentPane()加(jpNorth)。
    jpNorth.add(lblTitle);
    加(lblTitle,BorderLayout.NORTH);
    lblTitle.setText(您最喜爱的位置);
    lblTitle.setHorizo​​ntalAlignment(SwingConstants.CENTER);
    lblTitle.setFont(新字体(宋体,Font.PLAIN,32));
    lblTitle.setForeground(Color.DARK_GRAY);    的getContentPane()加(jpCenter)。}私人无效initializeEvents(){
    // TODO:添加操作侦听器等
}公共类操作实现的ActionListener {
    公共无效的actionPerformed(ActionEvent的五){
        字符串命令= e.getActionCommand();
        命令=命令== NULL? :命令;
        // TODO:如果添加...如果...其他行动命令    }
}公共无效的Dispose(){
    // TODO:保存设置
    // super.dispose();
    System.exit(0);
}公共无效调用setVisible(布尔B){
    初始化();
    super.setVisible(二);
}公共静态无效的主要(字串[] args){    。新的大型机()调用setVisible(真);
}公共无效的actionPerformed(ActionEvent的EVT){    如果(evt.getSource()== btnAdd){
        showNewFrame();
        //打开搜索窗口        }    }私人无效showNewFrame(){
    JFrame的帧=新的JFrame(搜索窗口);
    frame.setSize(500120);
    frame.setLocationRelativeTo(NULL);
    frame.setVisible(真);}
}


解决方案

虽然你已经实现了的actionPerformed 方法按照<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html\">ActionListener接口,类不是该类型,你没有的实施的接口。一旦实现该接口并注册它的的JButton btnAdd

  btnAdd.addActionListener(本);

该方法将被调用。

一个更紧凑的替代方案可能是使用一个匿名接口:

  btnAdd.addActionListener(新的ActionListener(){
   @覆盖
   公共无效的actionPerformed(ActionEvent的五){
      //手柄按钮的ActionEvent&安培;显示对话框...
   }
});


旁注:


  • 在应用程序中使用多个的JFrame 为可能需要的帧之间存在管理更新大量的开销。在preferred的方法是
    使用的JDialog 如果需要其他窗口模式。这是中详细讨论<一个href=\"http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice\">here.

I want to open a new JFrame by clicking a button (btnAdd); I have tried to create an actionlistener but I am having no luck; the code runs but nothing happens when the button is clicked. The methods in question are the last two in the following code. Any help is much appreciated!

package AdvancedWeatherApp;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionListener;

import weatherforecast.FetchWeatherForecast;

public class MainFrame extends JFrame implements ListSelectionListener {

private boolean initialized = false;
private Actions actions = new Actions();

private javax.swing.JScrollPane jspFavouritesList = new javax.swing.JScrollPane();
private javax.swing.DefaultListModel<String> listModel = new javax.swing.DefaultListModel<String>();
private javax.swing.JList<String> favouritesList = new javax.swing.JList<String>(
        listModel);

private javax.swing.JLabel lblAcknowledgement = new javax.swing.JLabel();
private javax.swing.JLabel lblTitle = new javax.swing.JLabel();

private javax.swing.JButton btnAdd = new javax.swing.JButton();
private javax.swing.JButton btnRemove = new javax.swing.JButton();

public void initialize() {
    initializeGui();
    initializeEvents();
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

/**
 * 
 */
private void initializeGui() {
    if (initialized)
        return;
    initialized = true;
    this.setSize(500, 400);

    Dimension windowSize = this.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    this.setLocation(screenSize.width / 2 - windowSize.width / 2,
            screenSize.height / 2 - windowSize.height / 2);
    Container pane = this.getContentPane();
    pane.setLayout(new BorderLayout());
    setLayout(new BorderLayout());
    setTitle("Favourite Weather Locations");

    JPanel jpSouth = new JPanel();
    jpSouth.setLayout(new FlowLayout());

    JPanel jpNorth = new JPanel();
    jpNorth.setLayout(new FlowLayout());

    JPanel jpCenter = new JPanel();
    jpCenter.setLayout(new BoxLayout(jpCenter, BoxLayout.PAGE_AXIS));

    JPanel jpEast = new JPanel();
    JPanel jpWest = new JPanel();

    getContentPane().setBackground(Color.WHITE);
    jpEast.setBackground(Color.WHITE);
    jpWest.setBackground(Color.WHITE);
    jpCenter.setBackground(Color.WHITE);

    getContentPane().add(jspFavouritesList);
    jpCenter.add(jspFavouritesList);
    jspFavouritesList.setViewportView(favouritesList);
    favouritesList
            .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    favouritesList.addListSelectionListener(this);

    jpCenter.add(btnAdd);
    jpCenter.add(btnRemove);
    jpCenter.setAlignmentY(CENTER_ALIGNMENT);
    btnAdd.setText("Add Location");
    btnAdd.setAlignmentX(Component.CENTER_ALIGNMENT);
    btnAdd.setFont(new Font("Calibri", Font.PLAIN, 18));

    jpCenter.add(btnRemove);
    btnRemove.setText("Remove Location");
    btnRemove.setAlignmentX(Component.CENTER_ALIGNMENT);
    btnRemove.setFont(new Font("Calibri", Font.PLAIN, 18));

    getContentPane().add(jpEast, BorderLayout.EAST);
    getContentPane().add(jpWest, BorderLayout.WEST);

    getContentPane().add(jpSouth);
    jpSouth.add(lblAcknowledgement);
    add(lblAcknowledgement, BorderLayout.SOUTH);
    lblAcknowledgement.setText(FetchWeatherForecast.getAcknowledgement());
    lblAcknowledgement.setHorizontalAlignment(SwingConstants.CENTER);
    lblAcknowledgement.setFont(new Font("Tahoma", Font.ITALIC, 12));

    getContentPane().add(jpNorth);
    jpNorth.add(lblTitle);
    add(lblTitle, BorderLayout.NORTH);
    lblTitle.setText("Your Favourite Locations");
    lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
    lblTitle.setFont(new Font("Calibri", Font.PLAIN, 32));
    lblTitle.setForeground(Color.DARK_GRAY);

    getContentPane().add(jpCenter);

}

private void initializeEvents() {
    // TODO: Add action listeners, etc
}

public class Actions implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        command = command == null ? "" : command;
        // TODO: add if...if else... for action commands

    }
}

public void dispose() {
    // TODO: Save settings
    // super.dispose();
    System.exit(0);
}

public void setVisible(boolean b) {
    initialize();
    super.setVisible(b);
}

public static void main(String[] args) {

    new MainFrame().setVisible(true);
}

public void actionPerformed(ActionEvent evt){

    if (evt.getSource() == btnAdd) {
        showNewFrame();
        //OPEN THE SEARCH WINDOW    

        }

    }

private void showNewFrame() {
    JFrame frame = new JFrame("Search Window" );
    frame.setSize( 500,120 );
    frame.setLocationRelativeTo( null );
    frame.setVisible( true );   

}
}

解决方案

Although you have implemented the actionPerformed method as per the ActionListener interface, you class is not of that that type as you haven't implemented the interface. Once you implement that interface and register it with the JButton btnAdd,

btnAdd.addActionListener(this);

the method will be called.

A more compact alternative might be to use an anonymous interface:

btnAdd.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
      // handle button ActionEvent & display dialog...    
   }
});


Side notes:

  • Using more than one JFrame in an application creates a lot of overhead for managing updates that may need to exist between frames. The preferred approach is to use a modal JDialog if another window is required. This is discussed more here.

这篇关于从按钮打开一个新的JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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