1

I am writing a very simple GUI, that contains 3 buttons, 2 labels, 2 text fields and one text area. Strangely, the result is unstable: when running the class the GUI appears with random number of the controls. I tried various layout managers, changing the order among the control - nothing.

Can someone help?

package finaltestrunner;

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

public class FinalTestGUI extends JFrame implements ActionListener
{   
public Boolean startState = false;

    JButton sofButton;
    JButton startStopButton;
    JButton exitButton;
        JTextField loopCounts;        
        JTextField trSnField;        
        JTextArea resultField = null;

    public FinalTestGUI() 
    {
// The constructor creates the panel and places the controls
    super();    // Jframe constructor

    JFrame trFrame = new JFrame();
    trFrame.setSize(1000, 100);
    trFrame.setVisible(true);
    trFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    trFrame.setTitle("Test runner");
    setFont(new Font("SansSerif", Font.PLAIN, 14));
//  trFrame.setLayout(new FlowLayout());

    JPanel trControlPanel = new JPanel();
    trControlPanel.setSize(1000, 100);
    trControlPanel.setLayout(new GridLayout(1,7));

        exitButton = new JButton("Exit");
    trControlPanel.add(exitButton);

    startStopButton = new JButton("Run ");
    trControlPanel.add(startStopButton);

    JLabel loopsLabel = new JLabel ("Loops count: ");
    trControlPanel.add(loopsLabel);

        loopCounts = new JTextField (5);
    trControlPanel.add(loopCounts);

    sofButton = new JButton("SoF");
    trControlPanel.add(sofButton);

    JLabel testLabel = new JLabel ("serial Number: ");
    trControlPanel.add(testLabel);

        trSnField = new JTextField (5);
    trControlPanel.add(trSnField);

    JTextArea trResultField = new JTextArea (80, 10);
    trFrame.add(trControlPanel);
//  cpl.add(trResultField);

    startStopButton.addActionListener(new ActionListener()
    {
            @Override
        public void actionPerformed (ActionEvent trStartStopButton)
        {
            startState = !startState;
            if (startState)
            {
                startStopButton.setText("Run ");
                startStopButton.setForeground(Color.red);
            }
            else
            {
                startStopButton.setText("Stop");
                startStopButton.setForeground(Color.green);
            }

        }
    });

    sofButton.addActionListener(new ActionListener()
    {
            @Override
        public void actionPerformed (ActionEvent trSofButton)
        {
                        loopCounts.setText("SOF\n");
        }
    });

    exitButton.addActionListener (new ActionListener()
    {
            @Override
        public void actionPerformed (ActionEvent trExitButton)
        {
                        System.exit(0);
        }
    });
    } // End of the constructor

    @Override
    public void actionPerformed (ActionEvent ae)  { }

    public void atpManager ()
    {
        String selectedAtp = "";
    }
}
4

1 回答 1

6

这段代码有几个问题:

  • 您已经从 JFrame 继承,因此您不需要创建另一个 JFrame
  • 您正在展示您的框架,setVisible(true)然后向其添加组件。这会使您的布局无效,您需要之后重新验证(或移动setVisible()到您已经添加组件的位置)
  • 您正在将组件直接添加到 JFrame,但您需要使用它的内容窗格。从Java 1.5开始,这些JFrame.add()方法会自动转发到内容窗格。在早期版本中,必须检索内容窗格JFrame.getContentPane()才能将子组件添加到内容窗格。

尝试这个:

public FinalTestGUI()     {
  // The constructor creates the panel and places the controls
  super();    // Jframe constructor

  setSize(1000, 100);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setTitle("Test runner");
  setFont(new Font("SansSerif", Font.PLAIN, 14));
  setLayout(new FlowLayout());

  JPanel trControlPanel = new JPanel();
  trControlPanel.setSize(1000, 100);
  trControlPanel.setLayout(new GridLayout(1,7));

  exitButton = new JButton("Exit");
  trControlPanel.add(exitButton);

  startStopButton = new JButton("Run ");
  trControlPanel.add(startStopButton);

  JLabel loopsLabel = new JLabel ("Loops count: ");
  trControlPanel.add(loopsLabel);

  loopCounts = new JTextField (5);
  trControlPanel.add(loopCounts);

  sofButton = new JButton("SoF");
  trControlPanel.add(sofButton);

  JLabel testLabel = new JLabel ("serial Number: ");
  trControlPanel.add(testLabel);

  trSnField = new JTextField (5);
  trControlPanel.add(trSnField);

  JTextArea trResultField = new JTextArea (80, 10);
  // getContentPane().add(trControlPanel); // pre 1.5
  add(trControlPanel);                     // 1.5 and greater

  setVisible(true);
}
于 2012-12-26T16:47:35.877 回答