簡體   English   中英

如何用JButton激活另一個gui類

[英]How can activate another gui class with JButton

我有兩個名為Menu和convert的GUI類。 我想點擊“打開”按鈕時運行轉換類。 它看起來很簡單,但我無法理解。

菜單類

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

轉換類

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

首先,你的類名應該以大寫字母開頭,所以不應該“轉換”它應該是“轉換”。

在Convert中創建一個公共方法:

  public JFrame getFrame() {
     return frmTitle;
  }

然后在你的按鈕的actionPerformed()方法中,只需:

   Convert w = new Convert();
   w.getFrame().setVisible(true);

首先,類名和構造函數應該以大寫字母開頭,就像你為class Menu做的那樣,但不是class convert
Java程序應該只有一個且只有一個主要方法用於所有類。 所以,從Convert類中刪除完成你的main方法,把new Convert(); 在Menu類的btnConvert的actionPerformed()方法中:

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

並添加frmTitle.setVisible(true); 到Convert類的initialize()方法的末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM