Create and show the label after starting the Java application

I have been working on the problem for 2 days and it will be a headache for me! I use swing to create a GUI for my application. I want to add a shortcut to my panel after running the code by clicking on the button, but I cannot. Please help me solve this problem. Most of this code is automatically generated by swing, and this is not the code I wrote.

package javaapplication1; import java.awt.Color; import javax.swing.SwingConstants; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.*; public class RandomWordGUI extends javax.swing.JFrame { /** Creates new form RandomWordGUI */ public RandomWordGUI() { initComponents(); } /** 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() { jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel1.setText("jLabel1"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(155, 155, 155) .addComponent(jButton1)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(172, 172, 172) .addComponent(jLabel1))) .addContainerGap(172, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGap(70, 70, 70) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1) .addGap(91, 91, 91)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(186, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JLabel jLabel2 = new JLabel(); jLabel2.setText("this is a label"); this.jPanel1.add(jLabel2); this.jPanel1.repaint(); this.jPanel1.revalidate(); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new RandomWordGUI().setVisible(true); } RandomWordGUI randWord=new RandomWordGUI(); }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; // End of variables declaration 

}

+6
source share
3 answers

You are trying to add to a panel using GroupLayout . Dynamically adding to this would be quite difficult (if possible), and I do not recommend doing this. I recommend adding the JPanel helper to the panel and adding any new components to this sub-panel.

EDIT:

To clear the solution used: the auxiliary JPanel was added using the form editor ( jPanel3 in the code below). The default form designer also uses GroupLayout for the added panel, which will lead to the same problem that should have been resolved in the first place, so the auxiliary panel layout manager has been changed to FlowLayout . The action code ends as simple as:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JLabel jLabel2 = new JLabel(); jLabel2.setText("this is a label"); this.jPanel3.add(jLabel2); this.jPanel3.revalidate(); } 
+2
source

I entered another panel in my Window-jPanel3- and added jLabel2 to it ... then added this code to the contents of the ActionEvent jPanel3.setLayout(new FlowLayout());

and everything became normal ... :)

Thanks to my dear friend @Kiheru for his help and guidance.

+1
source

Well, finally, my friend. Here is the code. We need

  • when we add the shortcut dynamically, we need to set the layout again.
  • so we need to copy the pase layout created by the IDE
  • enter manually in both horizontal and vertical groups

.addComponent (jLabel2)

.addGap ()

  • Your positioning is at the bottom of the screen, so I suggest you just change the position for better viewing.
  • Plz take care of spaces and positioning.
  • just paste the code into the actionPerformed remaing function the same

     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JLabel jLabel2 = new JLabel(); jLabel2.setText("this is a label"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(155, 155, 155) .addComponent(jButton1)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(172, 172, 172) .addComponent(jLabel2) .addGap(1,1,1) .addComponent(jLabel1))) .addContainerGap(172, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGap(70, 70, 70) .addComponent(jLabel1) ***.addGap(1,1,1) .addComponent(jLabel2)*** .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1) .addGap(91, 91, 91)) ); this.jPanel1.repaint(); this.jPanel1.revalidate(); 

    }

0
source

Source: https://habr.com/ru/post/951820/


All Articles