2013-05-27 128 views
0

我想做一个简单的计算器使用Java。下面是用于创建GUI的代码。JTextField不工作,因为它应该

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

public class Calculator extends JFrame implements ActionListener { 

    private JButton one, two, three, four, five, six, seven, eight, nine, zero, plus, minus, 
      multiply, divide, equalTo, point; 
    private JPanel panelForResult, panelForKeys; 
    private JLabel Result; 
    private JTextField result; 

    public Calculator() { 
    } 

    @Override 
    public void actionPerformed(ActionEvent ae) { 
    } 

    public static void main(String... args) { 


     Calculator calcFrame = new Calculator(); 
     calcFrame.setSize(330, 400); 
     calcFrame.setVisible(true); 
     calcFrame.setResizable(false); 
     calcFrame.createCalcGUI(); 

    } 

    private void createCalcGUI() { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 



     Container window = getContentPane(); 
     window.setBackground(Color.blue); 
     window.setSize(400, 400); 

     FlowLayout windowLayout = new FlowLayout(); 
     windowLayout.setHgap(50); 
     window.setLayout(windowLayout); 


     panelForKeys = new JPanel(); 
     panelForKeys.setBackground(Color.CYAN); 
     panelForKeys.setPreferredSize(new Dimension(200, 250)); 

     FlowLayout buttonLayout = new FlowLayout(); 
     // buttonLayout.setAlignOnBaseline(true); 
     panelForKeys.setLayout(buttonLayout); 

     panelForResult = new JPanel(); 
     panelForResult.setBackground(Color.CYAN); 
     panelForResult.setPreferredSize(new Dimension(200, 50)); 
     panelForResult.setLayout(new FlowLayout()); 


     Result = new JLabel("="); 
     result = new JTextField(); 

     one = new JButton("1"); 
     two = new JButton("2"); 
     three = new JButton("3"); 
     four = new JButton("4"); 
     five = new JButton("5"); 
     six = new JButton("6"); 
     seven = new JButton("7"); 
     eight = new JButton("8"); 
     nine = new JButton("9"); 
     zero = new JButton("0"); 
     plus = new JButton("+"); 
     minus = new JButton("-"); 
     multiply = new JButton("*"); 
     divide = new JButton("÷"); 
     equalTo = new JButton("="); 
     point = new JButton(". "); 




     one.addActionListener(this); 
     two.addActionListener(this); 
     three.addActionListener(this); 
     four.addActionListener(this); 
     five.addActionListener(this); 
     six.addActionListener(this); 
     seven.addActionListener(this); 
     eight.addActionListener(this); 
     nine.addActionListener(this); 
     zero.addActionListener(this); 
     plus.addActionListener(this); 
     minus.addActionListener(this); 
     divide.addActionListener(this); 
     multiply.addActionListener(this); 
     equalTo.addActionListener(this); 
     point.addActionListener(this); 

     panelForKeys.add(one); 
     panelForKeys.add(two); 
     panelForKeys.add(three); 
     panelForKeys.add(four); 
     panelForKeys.add(five); 
     panelForKeys.add(six); 
     panelForKeys.add(seven); 
     panelForKeys.add(eight); 
     panelForKeys.add(nine); 
     panelForKeys.add(zero); 
     panelForKeys.add(minus); 
     panelForKeys.add(plus); 
     panelForKeys.add(multiply); 
     panelForKeys.add(divide); 
     panelForKeys.add(equalTo); 
     panelForKeys.add(point); 


     window.add(panelForResult); 
     window.add(this.panelForKeys); 

     panelForResult.add(Result); 
     panelForResult.add(result); 


    } 
} 

每当我创建的JTextField的实例,并在panelForResult添加它的整个容器窗口得到蓝色。如果我评论JTextField然后它的作品。我只是Java的初学者,我可能知道可能的原因以及如何纠正它?

+1

请删除所有不必要的代码,并尽量做到:sscce.org – rethab

回答

1

这很可能是因为您在主线程上制作GUI而不是Event Dispatch Thread

只能在事件调度线程上访问Swing组件。这可以通过呼叫SwingUtilities.invokeLater来实现。

在你的情况,你的主要方法改成这样:

public static void main(String... args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      Calculator calcFrame = new Calculator(); 
      calcFrame.setSize(330, 400); 
      calcFrame.setVisible(true); 
      calcFrame.setResizable(false); 
      calcFrame.createCalcGUI(); 
     } 
    }); 
} 

编辑: 在Java中,有一些所谓的线程。通过使用多个线程,您可以同时运行多个代码段,这可以提高效率。

但是,并不是所有的代码可以同时安全地在不同的线程上运行。例如,Swing组件。 Swing组件只能在称为Event Dispatch Thread的特定线程上访问。

您可以阅读更多关于线程here

+0

@Leone星云它现在感谢,但我可以知道什么是事件分派线程详细地说, – user2398618

+0

@ user2398618我添加了一些关于线程和Event Dispatch Thread的信息。 –

3

您的代码有几个问题,很难提供确切的答案。这里有几件事情上下工夫:

  • JFrame.setVisible(true)应该是你最后一次通话(设置UI调用它之前,还是请确保调用pack()之后)
  • 不要强迫preferredSize(不曾经呼吁setPreferredSize)。这可能是你的问题的原因。始终使用适当的LayoutManager
  • 不要依靠FlowLayout来执行组件换行。
  • 做它用SwingUtilities.invokeLater()

这里是你的代码的工作示例(不知道通过设置列

  • 上调用EDT所有Swing相关的代码(数量提供一个尺寸暗示的JTextField虽然它符合布局你的想法):

    import java.awt.Color; 
    import java.awt.Container; 
    import java.awt.FlowLayout; 
    import java.awt.GridLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    
    import javax.swing.BoxLayout; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 
    import javax.swing.JPanel; 
    import javax.swing.JTextField; 
    import javax.swing.SwingUtilities; 
    
    public class Calculator extends JFrame implements ActionListener { 
    
        private JButton one, two, three, four, five, six, seven, eight, nine, zero, plus, minus, multiply, divide, equalTo, point; 
        private JPanel panelForResult, panelForKeys; 
        private JLabel Result; 
        private JTextField result; 
    
        public Calculator() { 
        } 
    
        @Override 
        public void actionPerformed(ActionEvent ae) { 
        } 
    
        public static void main(String... args) { 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override 
          public void run() { 
           Calculator calcFrame = new Calculator(); 
           calcFrame.createCalcGUI(); 
           calcFrame.setResizable(false); 
           calcFrame.pack(); 
           calcFrame.setVisible(true); 
          } 
         }); 
    
        } 
    
        private void createCalcGUI() { 
         setDefaultCloseOperation(EXIT_ON_CLOSE); 
    
         Container window = getContentPane(); 
         window.setBackground(Color.blue); 
    
         BoxLayout windowLayout = new BoxLayout(window, BoxLayout.PAGE_AXIS); 
         window.setLayout(windowLayout); 
    
         panelForKeys = new JPanel(new GridLayout(0, 3, 5, 5)); 
         panelForKeys.setBackground(Color.CYAN); 
    
         panelForResult = new JPanel(); 
         panelForResult.setBackground(Color.CYAN); 
         panelForResult.setLayout(new FlowLayout()); 
    
         Result = new JLabel("="); 
         result = new JTextField(12); 
    
         one = new JButton("1"); 
         two = new JButton("2"); 
         three = new JButton("3"); 
         four = new JButton("4"); 
         five = new JButton("5"); 
         six = new JButton("6"); 
         seven = new JButton("7"); 
         eight = new JButton("8"); 
         nine = new JButton("9"); 
         zero = new JButton("0"); 
         plus = new JButton("+"); 
         minus = new JButton("-"); 
         multiply = new JButton("*"); 
         divide = new JButton("÷"); 
         equalTo = new JButton("="); 
         point = new JButton(". "); 
    
         one.addActionListener(this); 
         two.addActionListener(this); 
         three.addActionListener(this); 
         four.addActionListener(this); 
         five.addActionListener(this); 
         six.addActionListener(this); 
         seven.addActionListener(this); 
         eight.addActionListener(this); 
         nine.addActionListener(this); 
         zero.addActionListener(this); 
         plus.addActionListener(this); 
         minus.addActionListener(this); 
         divide.addActionListener(this); 
         multiply.addActionListener(this); 
         equalTo.addActionListener(this); 
         point.addActionListener(this); 
    
         panelForKeys.add(one); 
         panelForKeys.add(two); 
         panelForKeys.add(three); 
         panelForKeys.add(four); 
         panelForKeys.add(five); 
         panelForKeys.add(six); 
         panelForKeys.add(seven); 
         panelForKeys.add(eight); 
         panelForKeys.add(nine); 
         panelForKeys.add(zero); 
         panelForKeys.add(minus); 
         panelForKeys.add(plus); 
         panelForKeys.add(multiply); 
         panelForKeys.add(divide); 
         panelForKeys.add(equalTo); 
         panelForKeys.add(point); 
    
         window.add(panelForResult); 
         window.add(this.panelForKeys); 
    
         panelForResult.add(Result); 
         panelForResult.add(result); 
    
        } 
    } 
    
  • +0

    谢谢!它现在有效! – user2398618

    相关问题