我想通过单击(比如说)菜单项来显示一个新窗口。新窗口将包含两个文本字段和一个取消按钮和一个确定按钮。当用户在文本字段中提供数据并按确定时,然后在父窗口中我应该收到用户提供的值。
我怎样才能做到这一点?请给我一个示例代码。谢谢
这是解决方案,您单击窗口菜单,然后单击新窗口,新窗口打开,带有两个文本字段,确定并取消按钮,我从第一个文本字段中获取文本值并将其显示在父窗口中。如果您有任何问题,请告诉我,我使用单例设计模式和 MVC。如果您对运行代码有任何疑问,请告诉我。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stackoverflow;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
/**
*
* @author Burak Firik
*/
public class ButtonListener implements MouseListener{
String value;
public ButtonListener(String str){
value=str;
}
@Override
public void mouseClicked(MouseEvent e) {
MainSingleton singleton=MainSingleton.getMainSingleton();
InputFrame inputFrame=singleton.getinputGUI();
MainFrame mainGUI=singleton.getGUI();
mainGUI.setLabelValue(inputFrame.getTextField1().getText());
mainGUI.repaint();
}
@Override
public void mousePressed(MouseEvent e) {
MainSingleton singleton=MainSingleton.getMainSingleton();
InputFrame inputFrame=singleton.getinputGUI();
inputFrame.setVisible(false);
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
并取消弹出窗口的按钮侦听器
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stackoverflow;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author Burak Firik
*/
public class CancelButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
MainSingleton singleton=MainSingleton.getMainSingleton();
InputFrame inputFrame=singleton.getinputGUI();
inputFrame.setVisible(false);
}
}
和 inputFrame 弹出的 jframe 将显示两个文本字段和两个按钮,用于确定和取消
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stackoverflow;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* @author Burak Firik
*/
public class InputFrame extends JFrame {
JTextField txt1;
JTextField txt2;
JButton btnOk;
JButton btnCancel;
public InputFrame(){
initGUI();
initHandlers();
}
void initGUI(){
this.setLayout(new BorderLayout());
this.setSize(300,300);
this.setLocation(200,200);
txt1=new JTextField(10);
txt2=new JTextField(10);
btnOk=new JButton("OK");
btnCancel=new JButton("Cancel");
JPanel northPanel=new JPanel();
northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
northPanel.add(txt1);
northPanel.add(txt2);
JPanel centerPanel=new JPanel();
centerPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
centerPanel.add(btnOk);
centerPanel.add(btnCancel);
add(northPanel,BorderLayout.NORTH );
add(centerPanel, BorderLayout.CENTER);
}
void initHandlers(){
ButtonListener btnListern=new ButtonListener(txt1.getText());
btnOk.addMouseListener(btnListern);
CancelButtonListener btnCancelListen=new CancelButtonListener();
btnCancel.addActionListener(btnCancelListen);
}
JFrame getInputFrame(){
return this;
}
JTextField getTextField1(){
return this.txt1;
}
JTextField getTextField2(){
return this.txt2;
}
public static void main(){
InputFrame frame=new InputFrame();
frame.setVisible(false);
}
}
newWindow ManuItem 所在的主机;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stackoverflow;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
/**
*
* @author Burak Firik
*/
public class MainFrame extends JFrame{
private JMenuBar menuBar;
private JMenu menuWindow;
private JMenuItem menunewWindow;
private JLabel labelInputValue;
public MainFrame(){
this.setVisible(true);
this.setSize(300,300);
initGUI();
initHandlers();
}
private void initGUI() {
// Create the menu bar
this.setLayout(new BorderLayout());
menuBar = new JMenuBar();
menuWindow = new JMenu( "Window" );
menuWindow.setMnemonic( 'N' );
menuBar.add( menuWindow );
menunewWindow = CreateMenuItem( menuWindow, ITEM_PLAIN,
"New Window", null, 'N', null );
add(menuBar, BorderLayout.NORTH);
labelInputValue=new JLabel("SELAM");
add(labelInputValue, BorderLayout.CENTER);
}
public JMenuItem CreateMenuItem( JMenu menu, int iType, String sText,
ImageIcon image, int acceleratorKey,
String sToolTip )
{
// Create the item
JMenuItem menuItem;
switch( iType )
{
case ITEM_RADIO:
menuItem = new JRadioButtonMenuItem();
break;
case ITEM_CHECK:
menuItem = new JCheckBoxMenuItem();
break;
default:
menuItem = new JMenuItem();
break;
}
// Add the item test
menuItem.setText( sText );
// Add the optional icon
if( image != null )
menuItem.setIcon( image );
// Add the accelerator key
if( acceleratorKey > 0 )
menuItem.setMnemonic( acceleratorKey );
// Add the optional tool tip text
if( sToolTip != null )
menuItem.setToolTipText( sToolTip );
// Add an action handler to this menu item
menu.add( menuItem );
return menuItem;
}
private final int ITEM_PLAIN = 0; // Item types
private final int ITEM_CHECK = 1;
private final int ITEM_RADIO = 2;
private void initHandlers() {
MenuListener menuListen=new MenuListener();
menunewWindow.addActionListener(menuListen);
}
public void setLabelValue(String str){
labelInputValue.setText(str);
this.repaint();
}
}
此类包含单例对象,对于懒惰的程序员来说非常快:))
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stackoverflow;
/**
*
* @author Burak Firik
*/
public class MainSingleton {
private static MainSingleton singleton=null;
private MainFrame gui;
private InputFrame inputGUI;
//private constructor for singleton design pattern
private MainSingleton(){}
public static MainSingleton getMainSingleton()
{
// ONLY CONSTRUCT THE SINGLETON THE FIRST TIME
if (singleton == null)
{
singleton = new MainSingleton();
}
// GET THE SINGLETON NO MATTER WHAT
return singleton;
}
public static void main(String[] args) {
MainSingleton mainFrame=MainSingleton.getMainSingleton();
mainFrame.init();
}
public void init(){
// INITALIZE THE GUI
gui = new MainFrame();
inputGUI=new InputFrame();
}
public void requestExit()
{
// WE MAY HAVE TO SAVE CURRENT WORK
boolean continueToExit = true;
// IF THE USER REALLY WANTS TO EXIT THE APP
if (continueToExit)
{
// EXIT THE APPLICATION
System.exit(0);
}
}
public MainFrame getGUI() { return gui; }
public InputFrame getinputGUI() { return inputGUI; }
}
这是菜单侦听器,当您单击新窗口时,将调用此侦听器。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stackoverflow;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
/**
*
* @author Burak Firik
*/
public class MenuListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
MainSingleton singleton=MainSingleton.getMainSingleton();
InputFrame inputFrame=singleton.getinputGUI();
inputFrame.setVisible(true);
}
}