2016-05-17 32 views
1

我有两个名为Menu和Convert的GUI类。当我点击“打开”按钮时,我想运行convert类。它看起来很简单,但我无法弄清楚。如何用JButton激活另一个GUI类

Menu类

package com.ui; 

import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class Menu { 

private JFrame frame; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Menu window = new Menu(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public Menu() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JButton btnConvert = new JButton("open"); 
    btnConvert.setBounds(44, 52, 89, 23); 
    btnConvert.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      //??? 
     } 
    }); 
    frame.getContentPane().setLayout(null); 
    frame.getContentPane().add(btnConvert); 
} 
} 

Convert类

package com.ui; 

import java.awt.EventQueue; 

import javax.swing.ButtonGroup; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.border.TitledBorder; 
import java.awt.Color; 
import javax.swing.JComboBox; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 


public class convert { 

private JFrame frmTitle; 
private JTextField textField; 

private double value; 

private ButtonGroup group; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       convert window = new convert(); 
       window.frmTitle.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public convert() { 
    initialize(); 

} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 

    frmTitle = new JFrame(); 
    frmTitle.setTitle("TITLE"); 
    frmTitle.setBounds(100, 100, 450, 300); 
    frmTitle.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frmTitle.getContentPane().setLayout(null); 

    JLabel lblInputValue = new JLabel("Input value:"); 
    lblInputValue.setBounds(29, 22, 79, 14); 
    frmTitle.getContentPane().add(lblInputValue); 

    textField = new JTextField(); 
    textField.setBounds(22, 47, 86, 20); 
    frmTitle.getContentPane().add(textField); 
    textField.setColumns(10); 

    JPanel panel = new JPanel(); 
    panel.setBorder(new TitledBorder(null, "Convert", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED)); 
    panel.setBounds(29, 118, 264, 133); 
    frmTitle.getContentPane().add(panel); 
    panel.setLayout(null); 

    final JRadioButton rdbtnKelvin = new JRadioButton("Kelvin"); 
    rdbtnKelvin.setBounds(6, 30, 67, 23); 
    panel.add(rdbtnKelvin); 

    final JRadioButton rdbtnFahrenheit = new JRadioButton("Fahrenheit"); 
    rdbtnFahrenheit.setBounds(71, 30, 77, 23); 
    panel.add(rdbtnFahrenheit); 

    final JRadioButton rdbtnCelcius = new JRadioButton("Celcius"); 
    rdbtnCelcius.setBounds(174, 30, 67, 23); 
    panel.add(rdbtnCelcius); 

    group = new ButtonGroup(); 
    group.add(rdbtnCelcius); 
    group.add(rdbtnFahrenheit); 
    group.add(rdbtnKelvin); 

    final JComboBox comboBox = new JComboBox(); 
    comboBox.setModel(new DefaultComboBoxModel(new String[] {"Celcius", "Fahrenheit", "Kelvin"})); 
    comboBox.setBounds(177, 47, 116, 20); 
    frmTitle.getContentPane().add(comboBox); 

    JButton btnConvert = new JButton("CONVERT"); 
    btnConvert.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      value = Double.parseDouble(textField.getText().toString()); 


      if(comboBox.getSelectedItem().toString().equals("Celcius")){ 


       if(rdbtnCelcius.isSelected()==true){ 
        value = value; 

       } 
       else if (rdbtnFahrenheit.isSelected()==true){ 
        value= 1.8*value +32; 
       } 
       else{ 
        value =value+273; 

       }     
      } 
      else if (comboBox.getSelectedItem().toString().equals("Fahrenheit")){ 

       if(rdbtnFahrenheit.isSelected()==true){ 
        value = value; 

       } 
       else if (rdbtnCelcius.isSelected()==true){ 
        value= (value-32)*1.8; 
       } 
       else{ 
        value =(value-32)/1.8+273; 
       } 
      } 
      else{ 
       if(rdbtnCelcius.isSelected()==true){ 
        value = value-273; 
       } 
       else if (rdbtnFahrenheit.isSelected()==true){ 
        value= value -273*1.8+32; 
       } 
       else{ 
        value =value;      
       } 
      } 

      textField.setText(value +""); 
      textField.setEnabled(false); 
     } 
    }); 
    btnConvert.setBounds(303, 114, 89, 23); 
    frmTitle.getContentPane().add(btnConvert); 

    JButton btnClear = new JButton("CLEAR"); 
    btnClear.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      textField.setText(""); 
      textField.setEnabled(true); 
      comboBox.setSelectedIndex(0); 
      rdbtnKelvin.setSelected(true); 

     } 
    }); 
    btnClear.setBounds(303, 170, 89, 23); 
    frmTitle.getContentPane().add(btnClear); 
} 
} 

回答

0

首先,类名和构造应该以大写字母开头,像你这么做是为了class Menu,而不是class convert
对于所有类,Java程序应该只有一个且只有一个主要方法。因此,删除Convert类完成你的主要方法,把new Convert();在btnConvert的actionPerformed()方法中的菜单类:

btnConvert.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      new Convert(); 
     } 
}); 

,并添加frmTitle.setVisible(true);转换类的initialize()方法结束。

0

首先,你的类名应以大写字母开头,所以不是 “转换” 应该是“转换。

在转换中创建公共方法:

public JFrame getFrame() { 
    return frmTitle; 
    } 

然后在您的按钮的actionPerformed()方法,只是:

所有的
Convert w = new Convert(); 
    w.getFrame().setVisible(true);