简体   繁体   English

Java JButton并未真正运行

[英]Java JButton it is not running truly

I added a jbutton with eclipse but it is not seeing correctly. 我添加了一个带有eclipse的jbutton,但是它没有正确显示。 How do I fix it? 我如何解决它?

this is screen shot : 这是屏幕截图 在此处输入图片说明

public class GUITest // test class
{
    // block the warnings
    public static void main(String[] args) //main
    {
        System.out.println("start of main");
        /* stackoverflow want to add more details */
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        JPanel panel = new JPanel();
        frame.add(panel);
        JButton button = new JButton("Click here");
        panel.add(button);
        button.addActionListener(new Action());
        System.out.println("end of main");

/* stackoverflow want to add more details */ } / * stackoverflow要添加更多详细信息* /}

    public static class Action implements ActionListener //actionlistener
    {
        public void actionPerformed (ActionEvent e)
        { /* stackoverflow want to add more details */
            JFrame frame2 = new JFrame("Clicked");
            frame2.setVisible(true);
            frame2.setSize(200,200);
            JLabel label = new JLabel("You clicked me!");
            JPanel panel = new JPanel();
            frame2.add(panel);
            panel.add(label); // some more details

        }
    }   
}

I have used this and It will working fine. 我已经用过了,它将正常工作。 Can you please check your Graphics Driver or other things. 能否请您检查图形驱动程序或其他内容。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ashwin Parmar
 */
public class GUITest {
    public static void main(String[] args) {
        System.out.println("Start");
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        JPanel panel = new JPanel();
        frame.add(panel);
        JButton button = new JButton("Click here");
        panel.add(button);
        button.addActionListener(new Action());
        frame.setVisible(true);
        System.out.println("End");
    }

    public static class Action implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent ae) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            JFrame frame2 = new JFrame("Clicked");
            frame2.setSize(200,200);
            JLabel label = new JLabel("You have clicked me!");
            JPanel panel = new JPanel();
            frame2.add(panel);
            panel.add(label);
            frame2.setVisible(true);
        }

    }

}
  • Always create and modify the UI from within the context of the Event Dispatching Thread 始终在事件调度线程的上下文中创建和修改UI
  • Wherever possible, call setVisible last, after you have established the basic UI. 建立基本UI后,请尽可能最后调用setVisible You will be required to call revalidate and repaint if you add components after the window is made visible. 如果在窗口可见后添加组件,则需要调用revalidaterepaint

For example... 例如...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            frame.add(panel);
            JButton button = new JButton("Click here");
            panel.add(button);
            button.addActionListener(new Action());             
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

Do the same thing within your ActionListener . ActionListener执行相同的操作。

See Initial Threads for more details 有关更多详细信息,请参见初始线程

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

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