我在项目中创建了一个JFrame
,它从JComboBox
获取输入。我已将EventListener
添加到组合框中。 I.E.当我选择"其他"来自ment的项目,它应该创建一个JDialog
的实例。但它创造了两个实例。谁能告诉我为什么会这样?
package app;
import java.awt.BorderLayout;
public class Main extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JLabel lblCallType;
private JComboBox calltypecombo;
private JPanel panel;
private JLabel lblCallMode;
private JComboBox callmodecombo;
private JLabel lblSource;
private JComboBox sourcecombo;
private JLabel lblItemName;
private JTextField textField_4;
private JLabel lblItemMag;
private JTextField textField_5;
private JLabel lblItemDetails;
private JLabel lblItemModel;
private JTextField textField_6;
private JLabel lblItemSrno;
private JTextField textField_7;
private JLabel lblBookingDescription;
private JSeparator separator_1;
private JLabel lblCustomerDetails;
private JLabel lblCustomerName;
private JTextField textField_9;
private JLabel lblAddress;
private JLabel lblContactNo;
private JTextField textField_11;
private JLabel lblEmailId;
private JTextField textField_12;
private JTextArea textArea_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setTitle("F1 Services & Solutions");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300, 300, 1000, 600);
contentPane = new JPanel();
contentPane.setForeground(Color.RED);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblSrno = new JLabel("Sr.No ");
lblSrno.setBounds(10, 11, 49, 22);
contentPane.add(lblSrno);
textField = new JTextField();
textField.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField.setBounds(91, 12, 106, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblCallDate = new JLabel("Call Date");
lblCallDate.setBounds(10, 45, 71, 14);
contentPane.add(lblCallDate);
textField_1 = new JTextField();
textField_1.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_1.setBounds(91, 42, 106, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblCallTime = new JLabel("Call Time");
lblCallTime.setBounds(291, 45, 58, 14);
contentPane.add(lblCallTime);
textField_2 = new JTextField();
textField_2.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_2.setBounds(359, 40, 97, 22);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblRmaNo = new JLabel("RMA No.");
lblRmaNo.setBounds(738, 45, 75, 14);
contentPane.add(lblRmaNo);
textField_3 = new JTextField();
textField_3.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_3.setBounds(805, 41, 86, 20);
contentPane.add(textField_3);
textField_3.setColumns(10);
lblCallType = new JLabel("Call Type");
lblCallType.setBounds(10, 438, 70, 14);
contentPane.add(lblCallType);
String[] type={"Within Warranty","Out Of Warranty"};
calltypecombo = new JComboBox(type);
calltypecombo.setBounds(91, 435, 129, 20);
contentPane.add(calltypecombo);
lblCallMode = new JLabel("Call Mode");
lblCallMode.setBounds(302, 438, 58, 14);
contentPane.add(lblCallMode);
String[] mode={"On Site","Carry In"};
callmodecombo = new JComboBox(mode);
callmodecombo.setBounds(370, 435, 86, 20);
contentPane.add(callmodecombo);
lblSource = new JLabel("Source");
lblSource.setBounds(501, 45, 58, 14);
contentPane.add(lblSource);
String[] source={"F1 Smart","Asus","Other"};
sourcecombo = new JComboBox(source);
sourcecombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
String state=(String) sourcecombo.getSelectedItem();
if(state.equals("F1 Smart") || state.equals("Asus"))
{
calltypecombo.setEnabled(true);
}
else if(state.equals("Other"))
{
calltypecombo.setEnabled(false);
OtherCharges.main(null);
}
}
});
sourcecombo.setBounds(600, 42, 86, 20);
contentPane.add(sourcecombo);
lblItemName = new JLabel("Item Name");
lblItemName.setBounds(10, 156, 71, 14);
contentPane.add(lblItemName);
textField_4 = new JTextField();
textField_4.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_4.setBounds(91, 153, 106, 20);
contentPane.add(textField_4);
textField_4.setColumns(10);
lblItemMag = new JLabel("Item MAG");
lblItemMag.setBounds(10, 191, 71, 14);
contentPane.add(lblItemMag);
textField_5 = new JTextField();
textField_5.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_5.setBounds(91, 188, 106, 20);
contentPane.add(textField_5);
textField_5.setColumns(10);
JSeparator separator = new JSeparator();
separator.setBounds(10, 124, 964, 2);
contentPane.add(separator);
lblItemDetails = new JLabel("Item Details:");
lblItemDetails.setForeground(Color.RED);
lblItemDetails.setBounds(10, 128, 71, 14);
contentPane.add(lblItemDetails);
lblItemModel = new JLabel("Item Model");
lblItemModel.setBounds(10, 236, 71, 14);
contentPane.add(lblItemModel);
textField_6 = new JTextField();
textField_6.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_6.setBounds(91, 233, 106, 20);
contentPane.add(textField_6);
textField_6.setColumns(10);
lblItemSrno = new JLabel("Item Sr.No.");
lblItemSrno.setBounds(10, 277, 75, 14);
contentPane.add(lblItemSrno);
textField_7 = new JTextField();
textField_7.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_7.setBounds(91, 274, 106, 20);
contentPane.add(textField_7);
textField_7.setColumns(10);
lblBookingDescription = new JLabel("Booking Desc.");
lblBookingDescription.setBounds(10, 315, 93, 14);
contentPane.add(lblBookingDescription);
lblCustomerDetails = new JLabel("Customer Details :");
lblCustomerDetails.setForeground(Color.RED);
lblCustomerDetails.setBounds(501, 128, 129, 14);
contentPane.add(lblCustomerDetails);
lblCustomerName = new JLabel("Customer Name");
lblCustomerName.setBounds(501, 156, 93, 14);
contentPane.add(lblCustomerName);
textField_9 = new JTextField();
textField_9.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_9.setBounds(602, 156, 166, 20);
contentPane.add(textField_9);
textField_9.setColumns(10);
lblAddress = new JLabel("Address");
lblAddress.setBounds(501, 191, 67, 14);
contentPane.add(lblAddress);
lblContactNo = new JLabel("Contact No.");
lblContactNo.setBounds(501, 277, 71, 14);
contentPane.add(lblContactNo);
textField_11 = new JTextField();
textField_11.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_11.setBounds(602, 273, 166, 20);
contentPane.add(textField_11);
textField_11.setColumns(10);
lblEmailId = new JLabel("Email ID ");
lblEmailId.setBounds(501, 315, 75, 14);
contentPane.add(lblEmailId);
textField_12 = new JTextField();
textField_12.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 11));
textField_12.setBounds(602, 311, 166, 20);
contentPane.add(textField_12);
textField_12.setColumns(10);
JButton btnRegister = new JButton("Register");
btnRegister.setBounds(413, 493, 89, 23);
contentPane.add(btnRegister);
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 13));
textArea.setLineWrap(true);
textArea.setBounds(602, 186, 166, 74);
contentPane.add(textArea);
textArea_1 = new JTextArea();
textArea_1.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 13));
textArea_1.setLineWrap(true);
textArea_1.setBounds(91, 310, 206, 111);
contentPane.add(textArea_1);
}
}
package app;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class OtherCharges extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField from;
private JTextField to;
private JTextField distance;
private JTextField totalcost;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
OtherCharges dialog = new OtherCharges();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public OtherCharges() {
setVisible(true);
setBounds(100, 100, 1000,700);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBackground(Color.WHITE);
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel lblYouWillBe = new JLabel("You Will Be Charged As Per Taxes & LBT");
lblYouWillBe.setForeground(Color.BLACK);
lblYouWillBe.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 16));
lblYouWillBe.setBounds(342, 11, 324, 22);
contentPanel.add(lblYouWillBe);
JLabel lblFillYourDetails = new JLabel("Fill Your Details Below ");
lblFillYourDetails.setForeground(Color.RED);
lblFillYourDetails.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 16));
lblFillYourDetails.setBounds(409, 36, 164, 31);
contentPanel.add(lblFillYourDetails);
JLabel lblTravellingRequired = new JLabel("Travelling Required :");
lblTravellingRequired.setBounds(47, 111, 123, 14);
contentPanel.add(lblTravellingRequired);
String[] travelling={"Yes","No"};
final JComboBox travellingCombo = new JComboBox(travelling);
travellingCombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String Travellingchoice=(String) travellingCombo.getSelectedItem();
if(Travellingchoice.equals("Yes"))
{
from.setEditable(true);
to.setEditable(true);
distance.setEditable(true);
}
else if(Travellingchoice.equals("No"))
{
from.setEditable(false);
to.setEditable(false);
distance.setEditable(false);
}
}
});
travellingCombo.setBounds(174, 108, 66, 20);
contentPanel.add(travellingCombo);
JLabel lblFrom = new JLabel("From");
lblFrom.setBounds(298, 111, 34, 14);
contentPanel.add(lblFrom);
from = new JTextField();
from.setBounds(331, 108, 101, 20);
contentPanel.add(from);
from.setColumns(10);
JLabel lblTo = new JLabel("To");
lblTo.setBounds(472, 111, 34, 14);
contentPanel.add(lblTo);
to = new JTextField();
to.setBounds(499, 108, 101, 20);
contentPanel.add(to);
to.setColumns(10);
JLabel lblTotalEstimatedDistance = new JLabel("Total Estimated Distance");
lblTotalEstimatedDistance.setBounds(626, 111, 143, 14);
contentPanel.add(lblTotalEstimatedDistance);
distance = new JTextField();
distance.setBounds(779, 108, 86, 20);
contentPanel.add(distance);
distance.setColumns(10);
JLabel lblKilometers = new JLabel("Kilometers");
lblKilometers.setBounds(875, 111, 75, 14);
contentPanel.add(lblKilometers);
JLabel lblTotalEstimatedCost = new JLabel("Total Estimated Cost");
lblTotalEstimatedCost.setBounds(626, 166, 141, 14);
contentPanel.add(lblTotalEstimatedCost);
totalcost = new JTextField();
totalcost.setBounds(779, 163, 86, 20);
contentPanel.add(totalcost);
totalcost.setColumns(10);
JButton btnCalculateTravellingExpense = new JButton("Calculate Travelling Expense");
btnCalculateTravellingExpense.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int cal=Integer.parseInt(distance.getText());
double cost=cal*1.50;
String total=String.valueOf(cost);
totalcost.setText(total);
}
});
btnCalculateTravellingExpense.setBounds(329, 162, 201, 23);
contentPanel.add(btnCalculateTravellingExpense);
JLabel lblRupees = new JLabel("Rupees");
lblRupees.setBounds(875, 166, 46, 14);
contentPanel.add(lblRupees);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
}
}
}
答案 0 :(得分:3)
当您使用ItemListener
时,您需要验证下一个事件状态:
JComboBox<String> box = new JComboBox<>();
box.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
// do something
}
}
});
在tutorial中阅读更多内容。