Here's an example program in Java that uses Swing controls to create a simple graphical user interface (GUI):
import javax.swing.*;
public class SwingControlExample {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlExample(){
prepareGUI();
}
public static void main(String[] args){
SwingControlExample swingControlExample = new SwingControlExample();
swingControlExample.showTextFieldDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Swing Control Example");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showTextFieldDemo(){
headerLabel.setText("Control in action: TextField");
JLabel namelabel= new JLabel("User ID: ", JLabel.RIGHT);
JTextField userText = new JTextField(6);
JButton loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "User ID: " + userText.getText();
statusLabel.setText(data);
}
});
controlPanel.add(namelabel);
controlPanel.add(userText);
controlPanel.add(loginButton);
mainFrame.setVisible(true);
}
}
This program creates a GUI window with a header label, a control panel, and a status label. The control panel contains a text field and a button. When the button is clicked, the program displays the text entered in the text field in the status label. To run this program, save it as SwingControlExample.java and compile it with the command javac SwingControlExample.java. Then run it with the command java SwingControlExample.