Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll give all. The error was, that you set the String <code>string</code>, not the JLabel <code>label</code>.</p> <p>Such behaviour, of something not having a GUI effect, could also have been caused by a missing <code>invokeLater</code> in <code>actionPerformed</code>.</p> <p>You made classes for the used objects to set specific properties. That is a burocratic waste, hence I have rewritten it a bit.</p> <p>If you resize the window, you'll see that the GridLayout manager again layouts, so that the JButton appears at its original position. You can use absolute positionwing with <code>setLayout(null)</code> using individual <code>setLocation</code>s.</p> <pre><code>public class JavaGUI extends JPanel { private JPanel control; private JLabel keys; public JavaGUI() { setLayout(new GridLayout(0, 1)); keys = new JLabel("Original starting value."); keys.setHorizontalAlignment(SwingConstants.CENTER); add(keys); control = new JPanel(); control.setPreferredSize(new Dimension(200, 100)); control.setMinimumSize(new Dimension(200, 100)); control.setLayout(null); JButton button = new JButton(new AbstractAction("Update") { @Override public void actionPerformed(ActionEvent e) { System.out.println("Command: " + e.getActionCommand()); keys.setText(String.valueOf(System.nanoTime())); //keys.label.setText(keys.string); //remove comments cause button move JButton j = (JButton) e.getSource(); j.setLocation(j.getX() + 10, j.getY() + 10); } }); button.setBounds(10, 10, 90, 24); control.add(button); add(control); } public static void main(String[] args) { final JFrame f = new JFrame("JavaGUI"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new JavaGUI()); f.pack(); f.setLocationRelativeTo(null); EventQueue.invokeLater(new Runnable() { @Override public void run() { f.setVisible(true); } }); } } </code></pre> <p>P.S. I have seen considerably worse code. This is actually neat code with only the superfluous classes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload