java - 如何以其他形式显示 Java NetBeans 的结果?
问题描述
我希望“提交”按钮打开表单,其值设置为文本字段、选中的项目、选中的单选按钮位于 jLabels 和桌面窗格中的 jLabel 中,用于第二种形式的图像
/* * 要更改此许可证头,请在项目属性中选择许可证头。* 要更改此模板文件,请选择工具 | 模板
- 并在编辑器中打开模板。*/ 打包员工应用程序;导入java.io.File;导入 javax.swing.ImageIcon;导入 javax.swing.JFileChooser;
/** * * @author irecu */ public class Empmenu extends javax.swing.JFrame {
/** * Creates new form Empmenu */ public Empmenu() { initComponents(); } Submitted submit = new Submitted(); public class Empmennu extends Submitted{ } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jDesktopPane1 = new javax.swing.JDesktopPane(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox<>(); jScrollPane2 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList<>(); jCheckBox1 = new javax.swing.JCheckBox(); jTextField3 = new javax.swing.JTextField(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jRadioButton3 = new javax.swing.JRadioButton(); jRadioButton4 = new javax.swing.JRadioButton(); jScrollPane3 = new javax.swing.JScrollPane(); jTextArea2 = new javax.swing.JTextArea(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setAutoRequestFocus(false); setBounds(new java.awt.Rectangle(0, 0, 600, 600)); setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); setMaximumSize(new java.awt.Dimension(600, 600)); setResizable(false); setSize(new java.awt.Dimension(800, 800)); setType(java.awt.Window.Type.POPUP); jButton1.setText("Upload"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); jLabel1.setText("jLabel1"); jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT); jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255)); jDesktopPane1.setMaximumSize(new java.awt.Dimension(280, 280)); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel2.setMaximumSize(new java.awt.Dimension(280, 280)); jLabel2.setPreferredSize(new java.awt.Dimension(280, 280)); jDesktopPane1.setLayer(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER); javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1); jDesktopPane1.setLayout(jDesktopPane1Layout); jDesktopPane1Layout.setHorizontalGroup( jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 224,
javax.swing.GroupLayout.PREFERRED_SIZE) ); jDesktopPane1Layout.setVerticalGroup(jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE));
jLabel3.setText("Name"); jLabel4.setText("Occupation"); jLabel5.setText("Age Category:"); jLabel6.setText("Employment:"); jLabel7.setText("Filipino?:"); jLabel8.setText("TIN:"); jLabel9.setText("Gender:"); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Employed", "Self-Employed", "Un-Employed" })); jScrollPane2.setName(""); // NOI18N jList1.setModel(new javax.swing.AbstractListModel<String>() { String[] strings = { "Minor: Under 18", "Adult: 18 to 59", "Senior: 60 Above" }; public int getSize() { return strings.length; } public String getElementAt(int i) { return strings[i]; } }); jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jScrollPane2.setViewportView(jList1); jCheckBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCheckBox1ActionPerformed(evt); } }); jTextField3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField3ActionPerformed(evt); } }); jTextField3.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { jTextField3KeyTyped(evt); } }); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jRadioButton3.setText("Male"); jRadioButton4.setText("Female"); jRadioButton4.setToolTipText(""); jTextArea2.setColumns(20); jTextArea2.setRows(5); jScrollPane3.setViewportView(jTextArea2); jButton3.setText("Submit"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("Preview Details"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton2.setText("Clear"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
addGap(65, 65, 65) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton1) .addGap(18, 18, 18) .addComponent( jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(45, 45, 45) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment. TRAILING) .addGroup(layout.createParallelGroup(javax.
pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: JFileChooser chooser= new JFileChooser(); chooser.showOpenDialog(null); File f=chooser.getSelectedFile(); jLabel2.setIcon(new ImageIcon(f.toString())); filename=f.getAbsolutePath(); jLabel1.setText(filename); } private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(jCheckBox1.isSelected()){ jTextField3.setEnabled(true); } else{ jTextField3.setEnabled(false); } } private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jTextField3KeyTyped(java.awt.event.KeyEvent evt) { // TODO add your handling code here: char character = evt.getKeyChar(); if (((character < '0') || (character > '9')) && (character != '\b')) { evt.consume(); } } private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jTextArea2.append("Name: " + jTextField1.getText()+"\n" ); jTextArea2.append("Occupation: " + jTextField2.getText()+"\n"); if(jList1.isSelectionEmpty()){ jTextArea2.append("Age Category: null \n"); } else{ jTextArea2.append("Age Category: " + jList1.getSelectedValuesList().toString()+"\n"); } jTextArea2.append("Employment: " + jComboBox1.getSelectedItem().toString()+"\n"); if(jCheckBox1.isSelected()){ jTextArea2.append("Filipino: Yes"+ "\n"); jTextArea2.append("TIN: "+ jTextField3.getText() + "\n"); } else{ jTextArea2.append("Filipino: No" + "\n"); } if(jRadioButton3.isSelected()){ jTextArea2.append("Gender: " + jRadioButton3.getText() + "\n" + "\n"); } else{ jTextArea2.append("Gender: " + jRadioButton4.getText() + "\n" +"\n"); } jTextArea2.append("\n"); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jTextArea2.setText(""); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { submit.setVisible(true); // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Empmenu.class.getName()).log(java.util.logging.Level.SEVERE,
空,前);} catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Empmenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Empmenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Empmenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //
/* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Empmenu().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JCheckBox jCheckBox1; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JDesktopPane jDesktopPane1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JList<String> jList1; private javax.swing.JRadioButton jRadioButton3; private javax.swing.JRadioButton jRadioButton4; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTextArea jTextArea2; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; // End of variables declaration
字节[] 照片=空;字符串文件名=空;}
解决方案
有几种方法可以做这种事情。顺便说一句,一种简单的方法是创建另一个扩展名为SubmissionForm的 JFrame 类,然后为这个新类创建一个构造函数,它将接受所有需要的信息来显示,例如(不可运行):
// Your first form's class (just an example)
public class DataEntryForm extends JFrame {
public DataEntryForm() {
initializeForm();
}
// ... all your other class code ...
private void initializeForm() {
// Create an ActionListener
submitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitButtonActionPerformed(evt);
}
});
// ... all the other code required to create your form ...
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new DataEntryForm().setVisible(true);
}
});
}
// The Submit Button ActionPerformed event
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Gather the supplied Data...
String name = nameTextField.getText();
String occupation = occupationTextField.getText();
String ageCategory = ageCategoryList.getSelectedValue().toString();
String employment = employmentCombo.getSelectedItem().toString();
boolean filipino = filipinoCheckBox.isSelected();
String tin = tinTextField.getText();
String gender = genderRadioButton1.isSelected() ? "Male" :
genderRadioButton2.isSelected() ? "Female" : "Unknown";
// Display this gathered Data in the other form...
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new SubmissionForm(name, occupation, ageCategory, employment, filipino,
tin, gender).setVisible(true);
}
});
}
}
现在是SubmissionForm类。注意构造函数和其中的代码。另请注意,此类中没有main()方法:
public class SubmissionForm extends javax.swing.JFrame {
// Class Constructor...
public SubmissionForm(String name, String occupation, String ageCategory,
String employment, boolean isFilipino, String tin,
String gender) {
initializeForm();
// JLabel components on this form
lblName.setText(name);
lblOccupation.setText(occupation);
lblAgeCategory.setText(ageCategory);
lblEmployment.setText(employment);
lblPhilipino.setText(String.valueOf(isFilipino).toUpperCase());
lblTIN.setText(tin);
lblGender.setText(gender);
}
private void initializeForm() {
// Your code to initialize form components here...
}
}
您可以将所有收集到的数据放入 Array、ArrayList 或 List 接口中,然后将其传递给构造函数,然后在显示表单之前使用该数据集合。
上面的代码不是可运行的。它是在我脑海中提供的,因此可能包含一些小错误。
换句话说......创建一个类似于此处为您的第二种形式所做的构造函数。
推荐阅读
- java - Tomcat:未找到允许使用 OpenSSL 的本机库
- reactjs - 如何在 React useState 中访问元素的名称
- discord.js - Discord.js - 你如何获得频道的 ID?
- python - 来自 python 的 12 个月移动平均线
- python - 查找熊猫数据框中列之间的父子关系
- python - 如何将函数的字符串输出转换为输入或可运行的 shell 命令?
- javascript - 悬停效果仅在悬停在卡片上半部分时触发
- javascript - 想从关系树中删除关系,但我不知道我错在哪里
- android - 为什么flutter release apk会崩溃?
- r - cor.test 的奇怪问题——去掉结果中的 [[1]]、[[2]] 等