2013-03-14 141 views
0

我想通過單擊按鈕(btnAdd)打開新的JFrame;我試圖創建一個行動偵查者,但我沒有運氣;代碼會運行,但點擊按鈕時不會發生任何事情。有問題的方法是以下代碼中的最後兩個。任何幫助深表感謝!從按鈕打開新的JFrame

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

} 
} 
+2

繼續操作之前,請參閱此經典作品。 stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/9554657#9554657 – 2013-03-14 16:24:33

回答

4

雖然你已經實現了actionPerformed方法按照ActionListener接口,你的類是不是該類型,你有沒有實施接口。一旦你實現這個接口,並與JButtonbtnAdd註冊,

btnAdd.addActionListener(this); 

的方法將被調用。

更緊湊的替代可能是使用匿名接口:

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

旁註:

  • 使用一個以上的JFrame在應用程序中創建大量的開銷用於管理可能需要在幀之間存在的更新。如果需要其他窗口,則首選方法是 使用模式JDialog。這更多地討論here
0

使用此:

btnAdd.addActionListener(this); 

@Override 

public void actionPerformed(ActionEvent e) 
{ 
    MainFrame frame = new MainFrame(); 
    frame.setVisible(true); 
} 
-1

類型此按鈕內

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
this.dispose(); 
ActionListener ActList = new ActionListener(); 
ActList.setVisible(true); 

}

+1

你如何期待一個ActionListener變得可見? – jhamon 2016-10-19 14:59:44

-1

看到我的最後一行

{ ActList.setVisible(true); }

+0

儘管此代碼片段可能會解決問題,但[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要用解釋性註釋來擠佔代碼,這會降低代碼和解釋的可讀性! – kayess 2016-12-12 11:19:24