I want to create a plain GUI just to practice and get the hang of it, and the one I created compiles fine. But there isn't any output that I can observe apart from it just saying 'build successful'.
I've only created a JButton
button and a JTextField
text field that is supposed to display a value when you press the button but it doesn't seem to do that.
package swing;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author 1stef
*/
public class Swing implements ActionListener {
private Frame f;
private Button b;
private TextField w1;
private Panel p;
private MenuBar mb;
private Menu M;
private MenuItem miR;
public Swing(){
f=new Frame("TEst");
b= new Button("Display hello");
w1=new TextField();
p =new Panel();
mb =new MenuBar();
M=new Menu("Hello");
miR = new MenuItem("Option");
}
public void setGUI(){
p.setLayout(new GridLayout(1,2));
p.add(w1);
p.add(b);
mb.setHelpMenu(M);
f.add(p,BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("Display hello")){
w1.setText("This si how we wash our clothes");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Swing().setGUI();
}
use this solution, you have to show the frame using f.show()
or you can use f.setVisible(true);
instead of .show()
import java.awt.*;
import java.awt.event.*;
/**
*
* @author 1stef
*/
public class Swing implements ActionListener {
private Frame f;
private Button b;
private TextField w1;
private Panel p;
private MenuBar mb;
private Menu M;
private MenuItem miR;
public Swing(){
f=new Frame("TEst");
b= new Button("Display hello");
w1=new TextField();
p =new Panel();
mb =new MenuBar();
M=new Menu("Hello");
miR = new MenuItem("Option");
}
public void setGUI(){
f.setSize(400,400);
p.setLayout(new GridLayout(1,2));
p.add(w1);
p.add(b);
mb.setHelpMenu(M);
f.add(p,BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(this);
f.show();
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("Display hello")){
w1.setText("This si how we wash our clothes");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Swing().setGUI();
}
}
output of your program like below though your UI design is not good
The problem is that your not showing anything. To fix this you need to add f.setVisible(true);
to the end of setGUI
. You should also add f.pack();
to it.
import java.awt.*;
import java.awt.event.*;
/**
*
* @author 1stef
*/
public class Swing implements ActionListener {
private Frame f;
private Button b;
private TextField w1;
private Panel p;
private MenuBar mb;
private Menu M;
private MenuItem miR;
public Swing(){
f=new Frame("TEst");
b= new Button("Display hello");
w1=new TextField();
p =new Panel();
mb =new MenuBar();
M=new Menu("Hello");
miR = new MenuItem("Option");
}
public void setGUI(){
p.setLayout(new GridLayout(1,2));
p.add(w1);
p.add(b);
mb.setHelpMenu(M);
f.add(p,BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(this);
f.pack();
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("Display hello")){
w1.setText("This si how we wash our clothes");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Swing().setGUI();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.