To display a string in the generation screen when a button is pressed

Asked 2 years ago, Updated 2 years ago, 33 views

When I tried to display a string in the screen generated by using swing, etc., when I pressed the button, an error occurred and I couldn't find a program to refer to the same content on the Internet, so I asked.
This program is designed to display a string generated by JLabel on the screen by pressing the button labeled "string".

import javax.swing.*;
import java.awt.*;      
import java.awt.event.*;       
/*
program:for making Button & String in window
*/
public class test {
    public static void main(String[]args) {
        JFrame f = new Edit();
        f.setVisible(true);
    }
}

class Edit extensions JFrame implements ActionListener {
    Edit(){
        super("testwindow");                
        setBounds (200, 200, 300, 300);     
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanelp = new JPanel();                     
        JButton button1 = new JButton("string");

        button1.addActionListener(this);
        button1.setActionCommand("button1");
        p.add (button1);                             

        Container contentPane=getContentPane();                          
        contentPane.add(p, BorderLayout.SOUTH);                            
    }
    public void actionPerformed (ActionEvent) {
        String cmd = e.getActionCommand();
        if(cmd.equals("button1"){
            JLabel label = new JLabel ("push Button");
            This.contentPane.add(label, BorderLayout.NORTH);
        }
    }
}

java

2022-09-30 18:13

1 Answers

I have attached a code that works with as few changes as possible.
The biggest point is

public void actionPerformed (ActionEvent) {
    String cmd = e.getActionCommand();
    if(cmd.equals("button1"){
        JLabel label = new JLabel ("push Button");
        This.contentPane.add(label, BorderLayout.NORTH);
    }
}

This is what happens when a button is clicked, but with the code above, I try to generate and add label components every time.
Instead, it is correct to add a label component that does not display any characters only once first, and then set (update) the text of the label when the button is pressed.

The second point is

contentPane.add(p, BorderLayout.SOUTH); 

As shown in , only of several types of layout managers can be positioned east, west, and north, east, west, and south with the argument (here BorderLayout.

).

Therefore,

as shown in the attached code.
Container contentPane=getContentPane();
    contentPane.setLayout(new BorderLayout());

You must configure the layout manager.

Swing tutorials from formal are systematically/comprehensively organized, but unfortunately only in English.
However, it comes with many moving code samples, so you may understand something just by copying and moving the code.

public class Test{
    public static void main(String[]args) {
        SwingUtilities.invokeLater(()->{
            JFrame f = new Edit();
            f.setVisible(true);
        });
    }
}

class Edit extensions JFrame implements ActionListener {

    JLabel label = new JLabel();

    Edit(){
        super("testwindow");
        setBounds (200, 200, 300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanelp = new JPanel();
        JButton button1 = new JButton("string");

        button1.addActionListener(this);
        button1.setActionCommand("button1");
        p.add (button1);

        Container contentPane=getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(p, BorderLayout.SOUTH);

        contentPane.add(label, BorderLayout.NORTH);
    }

    public void actionPerformed (ActionEvent) {
        String cmd = e.getActionCommand();
        if(cmd.equals("button1"){
            label.setText("push Button");
        }
    }
}


2022-09-30 18:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.