简体   繁体   English

创建基本 java Swing GUI

[英]Creating a basic java Swing GUI

I want to create a plain GUI just to practice and get the hang of it, and the one I created compiles fine.我想创建一个简单的 GUI 来练习和掌握它,而我创建的那个可以很好地编译。 But there isn't any output that I can observe apart from it just saying 'build successful'.但是除了说“构建成功”之外,我没有任何 output 可以观察到。
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.我只创建了一个JButton按钮和一个JTextField文本字段,当您按下按钮时应该显示一个值,但它似乎没有这样做。

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);使用此解决方案,您必须使用f.show()显示框架,或者您可以使用f.setVisible(true); instead of .show()而不是.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您的程序的 output 如下所示,尽管您的 UI 设计不好在此处输入图像描述

The problem is that your not showing anything.问题是您没有显示任何内容。 To fix this you need to add f.setVisible(true);要解决此问题,您需要添加f.setVisible(true); to the end of setGUI .setGUI的末尾。 You should also add f.pack();您还应该添加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();
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM