2014-12-31 43 views
0

我添加了一个带有eclipse的jbutton,但看不到它。我如何解决它?Java JButton它没有真正运行

this is screen shotenter image description here

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"); 

/*计算器要添加更多的细节* /}

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 

     } 
    } 
} 
+0

邮编看到这里Initial Threads .... –

+0

随时拨打'setVisible' last.after将所有组件。但这似乎VGA issue.close Eclipse和尝试运行'图形处理器nvidea' HTTP ://forums.lenovo.com/t5/image/serverpage/image-id/4817i32CB2A6D7D20EC06/image-size/original?v = mpbl-1&px = -1 –

+0

您可以将您的代码作为文本在这里发布... –

回答

0

我已经使用这个,它会正常工作。你可以请检查你的图形驱动程序或其他东西。

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); 
     } 

    } 

} 
+2

我使用高性能图形卡运行eclipse并运行正确。非常感谢你 –

0
  • 始终创建,并从事件的范围内调度线程
  • 只要有可能修改UI,叫setVisible最后,你已经建立了基本的UI之后。如果在窗口可见后添加组件,则需要调用revalidaterepaint

例如...

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); 
     } 
    }); 
} 

做你的ActionListener内同样的事情。

更多细节

+0

感谢有趣的,但我的问题是显卡。另外我不能运行你的代码。 ClassNotFoundException不是未定义的。我必须添加什么输入? –

+1

@HalilReis加入以下进口 'import java.awt.EventQueue; 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; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException;' –

+0

@MadProgrammer我跑程序通常默认的图形卡,你的代码运行完美。也谢谢你。 –