Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要将多个jradiobuttongroup和一个jcombobox连接到一个jbutton_Java_Eclipse - Fatal编程技术网

Java 我需要将多个jradiobuttongroup和一个jcombobox连接到一个jbutton

Java 我需要将多个jradiobuttongroup和一个jcombobox连接到一个jbutton,java,eclipse,Java,Eclipse,我需要做的是将两个JradioButtongGroup和一个combobox连接到一个jbutton,当我单击jbutton时,所有选项的总数都显示在一个文本字段中 ****(编辑)总的来说,我的意思是说你把选择的选项加起来(每个类别一个)。您将它们连接到calcular Import jbutton,并在ver dialogo jbutton中创建一个对话框窗口,显示您选择的微处理器和最终价格*** (重要注意)我正在使用eclipse,我无法使用您想要做的正是使用JTextField所做的…

我需要做的是将两个JradioButtongGroup和一个combobox连接到一个jbutton,当我单击jbutton时,所有选项的总数都显示在一个文本字段中

****(编辑)总的来说,我的意思是说你把选择的选项加起来(每个类别一个)。您将它们连接到calcular Import jbutton,并在ver dialogo jbutton中创建一个对话框窗口,显示您选择的微处理器和最终价格***


(重要注意)我正在使用eclipse,我无法使用您想要做的正是使用JTextField所做的……将所有相关Swing组件声明为类成员变量,以便可以在Ventapc类范围内的任何位置访问它们。完成后,您可以创建一个方法来验证所有必填字段是否都有选择,维护这些选择的总和,并将总价插入可用的JTextField(
textField
)。大概是这样的:

private boolean verifyEntriesAndSetTotal() {
    boolean res = true;
    precioTotal = 0;
    
    // Processor
    if (micro.getSelectedIndex() != -1) {
        switch (micro.getSelectedItem().toString().toLowerCase()) {
            case "intel":
                precioTotal += intel;
                break;
            case "athlon":
                precioTotal += athlon;
                break;
            case "turion":
                precioTotal += turion;
                break;
            default:
                return false;
        }
    }
    else {
        return false;
    }
    
    // Motherboard
    if (asusbutton.isSelected()) {
        precioTotal += asus;
    }
    else if (gigabutton.isSelected()) {
        precioTotal += giga;
    }
    else if (msibutton.isSelected()) {
        precioTotal += msi;
    }
    else {
        return false;
    }
    
    // Memory
    if (twoGB.isSelected()) {
        precioTotal += twogb;
    }
    else if (fourGB.isSelected()) {
        precioTotal += fourgb;
    }
    else if (eightGB.isSelected()) {
        precioTotal += eightgb;
    }
    else if (sixteenGB.isSelected()) {
        precioTotal += sixteengb;
    }
    else {
        return false;
    }
    
    //The OPTIONALS
    // Monitor Option
    if (chkMonitor.isSelected()) {
        precioTotal += monitor;
    }
    
    // Fixed Disk Option
    if (chkFixedDisk.isSelected()) {
        precioTotal += discofijo;
    }
    
    // Set the total price
    textField.setText(String.valueOf(precioTotal));
    return res;
}
if (!verifyEntriesAndSetTotal()) {
    // Not all required items are selected.
    JOptionPane.showMessageDialog(frmVentaPc, "Not all required selections have been made!", 
                                  "Invalid Selections", JOptionPane.WARNING_MESSAGE);
}
calcuimp
按钮的ActionPerformed事件中,您将看到如下内容:

private boolean verifyEntriesAndSetTotal() {
    boolean res = true;
    precioTotal = 0;
    
    // Processor
    if (micro.getSelectedIndex() != -1) {
        switch (micro.getSelectedItem().toString().toLowerCase()) {
            case "intel":
                precioTotal += intel;
                break;
            case "athlon":
                precioTotal += athlon;
                break;
            case "turion":
                precioTotal += turion;
                break;
            default:
                return false;
        }
    }
    else {
        return false;
    }
    
    // Motherboard
    if (asusbutton.isSelected()) {
        precioTotal += asus;
    }
    else if (gigabutton.isSelected()) {
        precioTotal += giga;
    }
    else if (msibutton.isSelected()) {
        precioTotal += msi;
    }
    else {
        return false;
    }
    
    // Memory
    if (twoGB.isSelected()) {
        precioTotal += twogb;
    }
    else if (fourGB.isSelected()) {
        precioTotal += fourgb;
    }
    else if (eightGB.isSelected()) {
        precioTotal += eightgb;
    }
    else if (sixteenGB.isSelected()) {
        precioTotal += sixteengb;
    }
    else {
        return false;
    }
    
    //The OPTIONALS
    // Monitor Option
    if (chkMonitor.isSelected()) {
        precioTotal += monitor;
    }
    
    // Fixed Disk Option
    if (chkFixedDisk.isSelected()) {
        precioTotal += discofijo;
    }
    
    // Set the total price
    textField.setText(String.valueOf(precioTotal));
    return res;
}
if (!verifyEntriesAndSetTotal()) {
    // Not all required items are selected.
    JOptionPane.showMessageDialog(frmVentaPc, "Not all required selections have been made!", 
                                  "Invalid Selections", JOptionPane.WARNING_MESSAGE);
}
上述代码用于下面提供的runnable中:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class Ventapc {

    private JFrame frmVentaPc;
    private JComboBox<String> micro;
    private JRadioButton asusbutton;
    private JRadioButton gigabutton;
    private JRadioButton msibutton;
    private JRadioButton twoGB;
    private JRadioButton fourGB;
    private JRadioButton eightGB;
    private JRadioButton sixteenGB;
    private JCheckBox chkMonitor;
    private JCheckBox chkFixedDisk;
    private JTextField textField;
    private final ButtonGroup PlacaMadre = new ButtonGroup();
    private final ButtonGroup Memoria = new ButtonGroup();
    
    private final String micros[] = {"Intel", "Athlon", "Turion"};
    // Prices
    private final int intel = 150;
    private final int athlon = 80;
    private final int turion = 120;
    private final int asus = 75;
    private final int giga = 320;
    private final int msi = 100;
    private final int twogb = 50;
    private final int fourgb = 80;
    private final int eightgb = 130;
    private final int sixteengb = 200;
    private final int monitor = 250;
    private final int discofijo = 80;
    private int precioTotal = 0;    // Total Price
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    Ventapc window = new Ventapc();
                    window.frmVentaPc.setVisible(true);
                    window.frmVentaPc.setLocationRelativeTo(null); // Display in Center Screen.
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Ventapc() {
        initialize();
    }

    private void initialize() {
        frmVentaPc = new JFrame();
        frmVentaPc.setTitle("PC Sale");
        frmVentaPc.setBounds(100, 100, 450, 310);
        frmVentaPc.setResizable(false);
        frmVentaPc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmVentaPc.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("<html><font color=red><b>*</b></font> Micro Type:</html>");
        lblNewLabel.setBounds(128, 15, 75, 22);
        frmVentaPc.getContentPane().add(lblNewLabel);
        
        JPanel placamadrepanel = new JPanel();
        placamadrepanel .setBorder(new LineBorder(new Color(0, 0, 0)));
        placamadrepanel .setBounds(120, 52, 280, 42);
        frmVentaPc.getContentPane().add(placamadrepanel );
        
        asusbutton = new JRadioButton("Asus");
        PlacaMadre.add(asusbutton);
        placamadrepanel .add(asusbutton);
        
        gigabutton = new JRadioButton("GigaByte");
        placamadrepanel .add(gigabutton);
        PlacaMadre.add(gigabutton);
        
        msibutton = new JRadioButton("MSI");
        placamadrepanel .add(msibutton);
        PlacaMadre.add(msibutton);
        
        JPanel memoriapanel = new JPanel();
        memoriapanel.setBorder(new LineBorder(new Color(0, 0, 0)));
        memoriapanel.setBounds(120, 111, 280, 42);
        frmVentaPc.getContentPane().add(memoriapanel);
        
        // NO ONE looks at a computer with Megabytes of memory ;)
        twoGB = new JRadioButton("2GB");
        memoriapanel.add(twoGB);
        Memoria.add(twoGB);
        
        fourGB = new JRadioButton("4GB");
        memoriapanel.add(fourGB);
        Memoria.add(fourGB);
        
        eightGB = new JRadioButton("8GB");
        memoriapanel.add(eightGB);
        Memoria.add(eightGB);
        
        sixteenGB = new JRadioButton("16GB");
        memoriapanel.add(sixteenGB);
        Memoria.add(sixteenGB);
        
        JLabel lblPlacaMadre = new JLabel("<html><font color=red><b>*</b></font> Motherboard:</html>");
        lblPlacaMadre.setBounds(10, 65, 90, 14);
        frmVentaPc.getContentPane().add(lblPlacaMadre);
        
        JLabel lblMemoria = new JLabel("<html><font color=red><b>*</b></font> Memory Size:</html>");
        lblMemoria.setBounds(10, 120, 90, 22);
        frmVentaPc.getContentPane().add(lblMemoria);
        
        chkMonitor = new JCheckBox("Monitor");
        chkMonitor.setBounds(128, 160, 97, 23);
        frmVentaPc.getContentPane().add(chkMonitor);
        
        chkFixedDisk = new JCheckBox("Fixed Disk: 1TB");
        chkFixedDisk.setBounds(231, 160, 120, 23);
        frmVentaPc.getContentPane().add(chkFixedDisk);
        
        textField = new JTextField();
        textField.setHorizontalAlignment(SwingConstants.CENTER);
        textField.setText("0,00");
        textField.setBounds(235, 190, 140, 30);
        frmVentaPc.getContentPane().add(textField);
        textField.setColumns(10);
        
        JButton calcuimp = new JButton("Calculate Amount");
        calcuimp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (!verifyEntriesAndSetTotal()) {
                    // Not all required items are selected.
                    JOptionPane.showMessageDialog(frmVentaPc, "Not all required selections have been made!", 
                                                  "Invalid Selections", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        calcuimp.setBounds(75, 194, 140, 23);
        frmVentaPc.getContentPane().add(calcuimp);

        JButton salir = new JButton("Leave");
        salir.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                 System.exit(0);
            }
        });
        salir.setBounds(235, 234, 140, 23);
        frmVentaPc.getContentPane().add(salir);
        
        micro = new JComboBox<>(micros);
        micro.setSelectedIndex(-1);
        micro.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                
            }
        });
        micro.setBounds(215, 15, 130, 22);
        frmVentaPc.getContentPane().add(micro);
        micro.getSelectedItem();
        
        JButton verdial = new JButton("See Dialog");
        verdial.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                JOptionPane.showInputDialog(frmVentaPc, "<html><center>The processor is: &nbsp;&nbsp;<font color=blue><b>" 
                        + micro.getSelectedItem().toString() + "</b></font></center><br></html>", 
                                            "Processor Type", JOptionPane.QUESTION_MESSAGE); 
            }
        });
        verdial.setBounds(75, 234, 140, 23);
        frmVentaPc.getContentPane().add(verdial);
    }
    
    private boolean verifyEntriesAndSetTotal() {
        boolean res = true;
        precioTotal = 0;
        
        // Processor
        if (micro.getSelectedIndex() != -1) {
            switch (micro.getSelectedItem().toString().toLowerCase()) {
                case "intel":
                    precioTotal += intel;
                    break;
                case "athlon":
                    precioTotal += athlon;
                    break;
                case "turion":
                    precioTotal += turion;
                    break;
                default:
                    return false;
            }
        }
        else {
            return false;
        }
        
        // Motherboard
        if (asusbutton.isSelected()) {
            precioTotal += asus;
        }
        else if (gigabutton.isSelected()) {
            precioTotal += giga;
        }
        else if (msibutton.isSelected()) {
            precioTotal += msi;
        }
        else {
            return false;
        }
        
        // Memory
        if (twoGB.isSelected()) {
            precioTotal += twogb;
        }
        else if (fourGB.isSelected()) {
            precioTotal += fourgb;
        }
        else if (eightGB.isSelected()) {
            precioTotal += eightgb;
        }
        else if (sixteenGB.isSelected()) {
            precioTotal += sixteengb;
        }
        else {
            return false;
        }
        
        //The OPTIONALS
        // Monitor Option
        if (chkMonitor.isSelected()) {
            precioTotal += monitor;
        }
        
        // Fixed Disk Option
        if (chkFixedDisk.isSelected()) {
            precioTotal += discofijo;
        }
        
        // Set the total price
        textField.setText(String.valueOf(precioTotal));
        return res;
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.LineBorder;
公共级Ventapc{
私有JFrame frmVentaPc;
私人JComboxMicro;
私人JRadioButton asusbutton;
私有JRadioButton gigabutton;
私有JRadioButton按钮;
私有JRadioButton twoGB;
私有JRadioButton-fourGB;
私人JRadioButton eightGB;
私人JRadioButton sixteenGB;
私人JCheckBox chkMonitor;
私有JCheckBox chkFixedDisk;
私有JTextField textField;
private final ButtonGroup PlacaMadre=new ButtonGroup();
私有最终按钮组内存=新按钮组();
私有最终字符串micros[]={“英特尔”、“Athlon”、“Turion”};
//价格
私有最终整数英特尔=150;
私人最终int athlon=80;
私人最终积分=120;
私人最终int asus=75;
私人最终整数千兆=320;
私人最终int msi=100;
私人最终int twogb=50;
私有最终int fourgb=80;
私人最终int eightgb=130;
私人最终整数十六B=200;
专用最终int监视器=250;
私人最终int discofijo=80;
private int preciottal=0;//总价
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
Ventapc窗口=新Ventapc();
window.frmVentaPc.setVisible(true);
window.frmVentaPc.setLocationRelativeTo(null);//显示在中央屏幕中。
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
公共场所{
初始化();
}
私有void初始化(){
frmVentaPc=新JFrame();
frmVentaPc.setTitle(“PC销售”);
frmVentaPc.立根(100100450310);
frmVentaPc.setresizeable(false);
frmVentaPc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmVentaPc.getContentPane().setLayout(null);
JLabel lblNewLabel=新JLabel(“*微型:”);
lblNewLabel.立根(128,15,75,22);
frmVentaPc.getContentPane().add(lblNewLabel);
JPanel placamadrepanel=新的JPanel();
placamadrepanel.setOrder(新线条边框(新颜色(0,0,0));
立根板(120,52,280,42);
frmVentaPc.getContentPane().add(placamadrepanel);
asusbutton=新的JRadioButton(“Asus”);
PlacaMadre.add(asusbutton);
placamadrepanel.add(asusbutton);
gigabutton=新的JRadioButton(“千兆字节”);
placamadrepanel.add(gigabutton);
PlacaMadre.add(千兆按钮);
msibutton=新的JRadioButton(“MSI”);
placamadrepanel.add(MSI按钮);
PlacaMadre.add(MSI按钮);
JPanel memoriapanel=新的JPanel();
memoriapanel.setOrder(新的线边框(新的颜色(0,0,0));
备忘录小组.立法院(120、111、280、42);
frmVentaPc.getContentPane().add(memoriapanel);
//没有人看一台内存为兆字节的计算机;)
twoGB=新的JRadioButton(“2GB”);
memoriapanel.add(两GB);
添加备忘录(两GB);
fourGB=新的JRadioButton(“4GB”);
memoriapanel.add(fourGB);
备忘录。添加(GB);
eightGB=新的JRadioButton(“8GB”);
memoriapanel.add(第八页);
备忘录。添加(第八条);
sixteenGB=新的JRadioButton(“16GB”);
备忘录小组。添加(第十六条b);
增加备忘录(第十六条b);
JLabel lblPlacaMadre=新的JLabel(“*主板:”);
lblPlacaMadre.立根(10,65,90,14);
frmVentaPc.getContentPane().add(lblPlacaMadre);
JLabel lblMemoria=新的JLabel(“*内存大小:”);
lblMemoria.挫折(10,120,90,22);
frmVentaPc.getContentPane().add(lblMemoria);
chkMonitor=新的JCheckBox(“监视器”);
chkMonitor.setBounds(128、160、97、23);
frmVentaPc.getContentPane().add(chkMonitor);
chkFixedDisk=新的JCheckBox(“固定磁盘:1TB”);
chkFixedDisk.setBounds(231160120,23);
frmVentaPc.getContentPane().add(chkFixedDisk);
textField=新的JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setText(“0,00”);
textField.setBounds(235、190、140、30);
frmVentaPc.getContentPane().add(textField);
textField.setColumns(10);
JButton calcuimp=新JButton(“计算金额”);
calcuimp.addActionListener(新ActionLis