所以我有一个非常快速的问题。当用户按下更新按钮时,如何设置文本字段的可见性?我基本上希望将文本字段的可见性设置为false,当用户按下JButton时,它会将可见性设置为true。在JButton上,我会使用动作事件或更改事件吗?我已经尝试了两者,但可见度似乎没有变化。下面是我的代码,任何输入都会有所帮助。谢谢。
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.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.border.TitledBorder;
import javax.swing.JComponent;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.plaf.PanelUI;
public class FirmLawyerProfile extends JDialog {
private final JPanel contentPanel = new JPanel();
private final PrimaryLogin primaryLogin = new PrimaryLogin();
private JTextField jtfLawyerFirstName;
private JTextField jtfLawyerLastName;
private JTextField jtfLawyerHomeAddress;
private JTextField jtfLawyerPrimaryPhone;
private JTextField jtfLawyerMiddleName;
private JTextField jtfLawyerEmail;
private JTextField jtfLawyerCity;
private JTextField jtfLawyerZipCode;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
FirmLawyerProfile dialog = new FirmLawyerProfile();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public FirmLawyerProfile() {
setTitle("Firm Attorney Record");
setBounds(100, 100, 998, 679);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel.setBounds(0, 13, 200, 556);
contentPanel.add(panel);
panel.setLayout(null);
JLabel jlblFirmLawyerPicture = new JLabel("");
jlblFirmLawyerPicture.setIcon(new ImageIcon("C:\\Users\\Justyn Bell\\Desktop\\abaApplicationImages\\blankProfile.jpg"));
jlblFirmLawyerPicture.setBounds(41, 13, 115, 124);
panel.add(jlblFirmLawyerPicture);
JLabel jlblLawyerFirstName = new JLabel("[.getFirstName]");
jlblLawyerFirstName.setBounds(12, 161, 95, 16);
panel.add(jlblLawyerFirstName);
JLabel jlblLawyerLastName = new JLabel("[.getLastName]");
jlblLawyerLastName.setBounds(105, 161, 95, 16);
panel.add(jlblLawyerLastName);
JButton jbtnHome = new JButton("");
jbtnHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//this.FirmLawyerProfile.setVisible(false);
FirmLawyerProfile.this.primaryLogin.setVisible(true);
}
});
jbtnHome.setIcon(new ImageIcon("C:\\Users\\Justyn Bell\\Desktop\\abaApplicationImages\\homeButton.jpg"));
jbtnHome.setBounds(255, 497, 77, 72);
contentPanel.add(jbtnHome);
JButton jtbnRemove = new JButton("");
jtbnRemove.setIcon(new ImageIcon("C:\\Users\\Justyn Bell\\Desktop\\abaApplicationImages\\removeButton.png"));
jtbnRemove.setBounds(415, 497, 77, 72);
contentPanel.add(jtbnRemove);
final JButton jbtnUpdate = new JButton("");
jbtnUpdate.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
if(jbtnUpdate.isSelected()) {
jtfLawyerFirstName.setVisible(true);
}
}
});
jbtnUpdate.setIcon(new ImageIcon("C:\\Users\\\\Desktop\\abaApplicationImages\\UpdateInformation.png"));
jbtnUpdate.setBounds(569, 497, 77, 72);
contentPanel.add(jbtnUpdate);
JButton jbtnSave = new JButton("");
jbtnSave.setIcon(new ImageIcon("C:\\Users\\\\Desktop\\abaApplicationImages\\SaveButton.png"));
jbtnSave.setBounds(743, 497, 77, 72);
contentPanel.add(jbtnSave);
JPanel panel_1 = new JPanel();
panel_1.setLayout(null);
panel_1.setBorder(new TitledBorder("Attorney Information"));
panel_1.setBounds(212, 13, 734, 240);
contentPanel.add(panel_1);
JLabel jlblLawyerLegalSpeciality = new JLabel("Area of Legal Speciality");
jlblLawyerLegalSpeciality.setHorizontalAlignment(SwingConstants.LEFT);
jlblLawyerLegalSpeciality.setBounds(312, 23, 134, 23);
panel_1.add(jlblLawyerLegalSpeciality);
JLabel jlblLawyerMiddleName = new JLabel("Middle Name");
jlblLawyerMiddleName.setHorizontalAlignment(SwingConstants.LEFT);
jlblLawyerMiddleName.setBounds(342, 59, 80, 23);
panel_1.add(jlblLawyerMiddleName);
JLabel jlblEmail = new JLabel("Email");
jlblEmail.setHorizontalAlignment(SwingConstants.LEFT);
jlblEmail.setBounds(342, 95, 68, 23);
panel_1.add(jlblEmail);
JLabel jlblLawyerCity = new JLabel("City");
jlblLawyerCity.setBounds(342, 133, 80, 16);
panel_1.add(jlblLawyerCity);
JLabel jlblLawyerZipCode = new JLabel("Zip Code");
jlblLawyerZipCode.setHorizontalAlignment(SwingConstants.LEFT);
jlblLawyerZipCode.setBounds(342, 162, 93, 23);
panel_1.add(jlblLawyerZipCode);
JLabel jlblLawyerDegreeInstitution = new JLabel("Degree Institution");
jlblLawyerDegreeInstitution.setHorizontalAlignment(SwingConstants.LEFT);
jlblLawyerDegreeInstitution.setBounds(342, 198, 107, 23);
panel_1.add(jlblLawyerDegreeInstitution);
JLabel jlblLawyerPrefix = new JLabel("Prefix");
jlblLawyerPrefix.setHorizontalAlignment(SwingConstants.LEFT);
jlblLawyerPrefix.setBounds(12, 23, 54, 23);
panel_1.add(jlblLawyerPrefix);
JLabel label_7 = new JLabel("First Name");
label_7.setHorizontalAlignment(SwingConstants.LEFT);
label_7.setBounds(12, 59, 68, 23);
panel_1.add(label_7);
JLabel label_8 = new JLabel("Last Name");
label_8.setHorizontalAlignment(SwingConstants.LEFT);
label_8.setBounds(12, 95, 68, 23);
panel_1.add(label_8);
JLabel label_9 = new JLabel("Home Address");
label_9.setHorizontalAlignment(SwingConstants.LEFT);
label_9.setBounds(12, 130, 93, 23);
panel_1.add(label_9);
JLabel jlblLawyerStateOfResidence = new JLabel("State of Residence");
jlblLawyerStateOfResidence.setHorizontalAlignment(SwingConstants.LEFT);
jlblLawyerStateOfResidence.setBounds(12, 162, 107, 23);
panel_1.add(jlblLawyerStateOfResidence);
JLabel jlblLawyerPrimaryPhone = new JLabel("Primary Phone");
jlblLawyerPrimaryPhone.setHorizontalAlignment(SwingConstants.LEFT);
jlblLawyerPrimaryPhone.setBounds(12, 198, 107, 23);
panel_1.add(jlblLawyerPrimaryPhone);
JComboBox jcbLawyerSpeciality = new JComboBox(new Object[]{});
jcbLawyerSpeciality.setBounds(458, 24, 228, 22);
panel_1.add(jcbLawyerSpeciality);
JComboBox jcbLawyerDegreeInstitution = new JComboBox(new Object[]{});
jcbLawyerDegreeInstitution.setBounds(458, 198, 228, 22);
panel_1.add(jcbLawyerDegreeInstitution);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(530, 198, 21, 22);
panel_1.add(scrollPane);
jtfLawyerFirstName = new JTextField();
jtfLawyerFirstName.setColumns(10);
jtfLawyerFirstName.setBounds(151, 59, 154, 22);
panel_1.add(jtfLawyerFirstName);
//jtfLawyerFirstName.setVisible(false); SHOULD I SET THIS SOMEHOW?
jtfLawyerLastName = new JTextField();
jtfLawyerLastName.setColumns(10);
jtfLawyerLastName.setBounds(151, 95, 154, 22);
panel_1.add(jtfLawyerLastName);
jtfLawyerHomeAddress = new JTextField();
jtfLawyerHomeAddress.setColumns(10);
jtfLawyerHomeAddress.setBounds(151, 130, 154, 22);
panel_1.add(jtfLawyerHomeAddress);
JComboBox jcbLawyerStateOfResidence = new JComboBox(new Object[]{});
jcbLawyerStateOfResidence.setBounds(151, 162, 80, 22);
panel_1.add(jcbLawyerStateOfResidence);
JComboBox jcbLawyerPrefix = new JComboBox(new Object[]{});
jcbLawyerPrefix.setBounds(151, 23, 80, 22);
panel_1.add(jcbLawyerPrefix);
jtfLawyerPrimaryPhone = new JTextField();
jtfLawyerPrimaryPhone.setColumns(10);
jtfLawyerPrimaryPhone.setBounds(151, 198, 154, 22);
panel_1.add(jtfLawyerPrimaryPhone);
jtfLawyerMiddleName = new JTextField();
jtfLawyerMiddleName.setColumns(10);
jtfLawyerMiddleName.setBounds(458, 59, 154, 22);
panel_1.add(jtfLawyerMiddleName);
jtfLawyerEmail = new JTextField();
jtfLawyerEmail.setColumns(10);
jtfLawyerEmail.setBounds(458, 95, 154, 22);
panel_1.add(jtfLawyerEmail);
jtfLawyerCity = new JTextField();
jtfLawyerCity.setColumns(10);
jtfLawyerCity.setBounds(458, 130, 154, 22);
panel_1.add(jtfLawyerCity);
jtfLawyerZipCode = new JTextField();
jtfLawyerZipCode.setColumns(10);
jtfLawyerZipCode.setBounds(458, 162, 154, 22);
panel_1.add(jtfLawyerZipCode);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
}
}
}