我需要将多个jradiobox按钮组和一个jcombobox连接到一个jbutton



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

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

(重要通知(我正在使用eclipse,不能使用<字符串和combobox,所以如果有人能帮我找到解决方案,我将不胜感激

package parciall;
import java.awt.Component;
public class Ventapc {
private JFrame frmVentaPc;
private final ButtonGroup PlacaMadre = new ButtonGroup();
private final ButtonGroup Memoria = new ButtonGroup();
private JTextField textField;
String micros[]={"Intel","Atlon","Turion"};
int tel=150;
int atlon=80;
int turion=120;
int asus=75;
int giga=320;
int msi=100;
int twomb=50;
int fourmb=80;
int eigthmb=130;
int monitor=250;
int discofijo=80;
int fp;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Ventapc window = new Ventapc();
window.frmVentaPc.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Ventapc() {
initialize();
}
private void initialize() {
frmVentaPc = new JFrame();
frmVentaPc.setTitle("Venta PC");
frmVentaPc.setBounds(100, 100, 450, 300);
frmVentaPc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmVentaPc.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Tipo de Micro:");
lblNewLabel.setBounds(128, 15, 67, 22);
frmVentaPc.getContentPane().add(lblNewLabel);

JPanel placamadrepanel = new JPanel();
placamadrepanel .setBorder(new LineBorder(new Color(0, 0, 0)));
placamadrepanel .setBounds(94, 52, 307, 42);
frmVentaPc.getContentPane().add(placamadrepanel );

JRadioButton asusbutton = new JRadioButton("Asus");
PlacaMadre.add(asusbutton);
placamadrepanel .add(asusbutton);

JRadioButton gigabutton= new JRadioButton("GigaByte");
placamadrepanel .add(gigabutton);
PlacaMadre.add(gigabutton);

JRadioButton 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(94, 111, 307, 42);
frmVentaPc.getContentPane().add(memoriapanel);

JRadioButton twomb = new JRadioButton("2MB");
memoriapanel.add(twomb);
Memoria.add(twomb);

JRadioButton fourmb = new JRadioButton("4MB");
memoriapanel.add(fourmb);
Memoria.add(fourmb);

JRadioButton eigthmb = new JRadioButton("8MB");
memoriapanel.add(eigthmb);
Memoria.add(eigthmb);

JLabel lblPlacaMadre = new JLabel("Placa Madre:");
lblPlacaMadre.setBounds(10, 80, 74, 14);
frmVentaPc.getContentPane().add(lblPlacaMadre);

JLabel lblMemoria = new JLabel("Memoria:");
lblMemoria.setBounds(10, 131, 74, 22);
frmVentaPc.getContentPane().add(lblMemoria);

JCheckBox monitor = new JCheckBox("Monitor");
monitor.setBounds(128, 160, 97, 23);
frmVentaPc.getContentPane().add(monitor);

JCheckBox discofjo = new JCheckBox("Disco Fijo 1TB");
discofjo.setBounds(227, 160, 97, 23);
frmVentaPc.getContentPane().add(discofjo);

textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setText("0,00");
textField.setBounds(237, 190, 87, 30);
frmVentaPc.getContentPane().add(textField);
textField.setColumns(10);

JButton calcuimp = new JButton("Calcular Importe");
calcuimp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText ( "error");
}
});
calcuimp.setBounds(94, 194, 115, 23);
frmVentaPc.getContentPane().add(calcuimp);

JButton verdial = new JButton("Ver Dialogo");
verdial.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
JOptionPane.showInputDialog(this,"el procesador es:"+micro,); 
}
});
verdial.setBounds(94, 228, 115, 23);
frmVentaPc.getContentPane().add(verdial);

JButton salir = new JButton("Salir");
salir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
salir.setBounds(235, 228, 115, 23);
frmVentaPc.getContentPane().add(salir);

JComboBox micro = new JComboBox();
micro.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
micro.setBounds(205, 15, 87, 22);
frmVentaPc.getContentPane().add(micro);

micro.getSelectedItem();
}
}

您想要做的正是您对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;
}

calcuimp按钮的ActionPerformed事件中,您会得到如下内容:

if (!verifyEntriesAndSetTotal()) {
// Not all required items are selected.
JOptionPane.showMessageDialog(frmVentaPc, "Not all required selections have been made!", 
"Invalid Selections", JOptionPane.WARNING_MESSAGE);
}

上面的代码用于下面提供的可运行程序:

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

最新更新