简体   繁体   English

简单的Java GUI程序

[英]Simple Java GUI Program

My JPanel and JTextField are for some reason not appearing. 由于某些原因,我的JPanel和JTextField没有出现。 The programs only task is to add a number to the counter every time the button is clicked. 程序唯一的任务是每次单击按钮时向计数器添加一个数字。 No compiling errors nor console issues. 没有编译错误也没有控制台问题。 Simply not showing up?? 根本不露面吗?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

public class Swing  
{
public static void main(String[] args) 
{
   SwingUtilities.invokeLater(new Runnable()
    {
        public void run() 
        {
            final JFrame mainFrame = new JFrame ("Counter (Program 1/2)"); 
            mainFrame.setVisible(true);
            mainFrame.setSize(400, 200);
            mainFrame.setLayout(new BorderLayout());
            mainFrame.setLocationRelativeTo(null); 
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


            JButton countButton = new JButton("Count up");
            mainFrame.add(countButton, BorderLayout.SOUTH);

            countButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    JTextField clicks = new JTextField(BorderLayout.CENTER);
                    JPanel firstPanel = new JPanel();
                    mainFrame.add(firstPanel);
                    mainFrame.add(clicks, BorderLayout.NORTH);      
                    int counter = 1;
                    counter++;
                    String textField = String.valueOf(counter);
                    clicks.setText(textField);

                }
            });     
        }
    });     
}

} }

Don't add the JTextField and JPanel inside the ActionListener. 不要添加的JTextField和JPanel中的ActionListener This makes no sense since you'll be re-adding new components each time the button is pressed. 这是没有意义的,因为每次按下按钮时都将重新添加新组件。 Instead add them on GUI creation, before calling setVisible(true) on the GUI. 而是在GUI上调用setVisible(true)之前,在GUI创建中添加它们。 Then update the text field's text in the ActionListener. 然后在ActionListener中更新文本字段的文本。

As a side recommendation: try to make your class more object oriented by giving it fields, a constructor, and getting most all of that code out of the static main method. 作为附带建议:尝试通过为类提供字段,构造函数,并从静态main方法中获取大部分代码,使类更加面向对象。

Your mistakes: 您的错误:

  1. Add clicks and firstpanel to the frame in run method, not in the actionPerformed run方法中而不是在actionPerformed向框架添加clicksfirstpanel
  2. BorderLayout.CENTER should be pass as a parameter to add method, not to the text field constructor. 应该将BorderLayout.CENTER作为add方法的参数传递,而不是传递给文本字段构造函数。
  3. Define count as static . 将count定义为static This is not the best way but in your situation it is the easiest way. 这不是最好的方法,但是在您的情况下,这是最简单的方法。
  4. Call mainFrame.setVisible(true); 调用mainFrame.setVisible(true); after you added all your components. 添加所有组件之后。

Here is the working code: 这是工作代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Swing {

    public static int counter = 1;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final JFrame mainFrame = new JFrame("Counter (Program 1/2)");

                mainFrame.setSize(400, 200);
                mainFrame.setLayout(new BorderLayout());
                mainFrame.setLocationRelativeTo(null);
                mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JButton countButton = new JButton("Count up");
                mainFrame.add(countButton, BorderLayout.SOUTH);

                final JTextField clicks = new JTextField(String.valueOf(counter));
                JPanel firstPanel = new JPanel();
                mainFrame.add(firstPanel, BorderLayout.CENTER);
                mainFrame.add(clicks, BorderLayout.NORTH);

                countButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        counter++;
                        String textField = String.valueOf(counter);
                        clicks.setText(textField);

                    }
                });

                mainFrame.setVisible(true);
            }
        });
    }
}

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

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