Java中的不稳定GUI

use*_*275 1 java user-interface swing controls

我正在编写一个非常简单的GUI,它包含3个按钮,2个标签,2个文本字段和一个文本区域.奇怪的是,结果是不稳定的:当运行类时,GUI会出现随机数量的控件.我尝试了各种布局管理器,改变了控件之间的顺序 - 没有.

有人可以帮忙吗?

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 = "";
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*ter 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);
}
Run Code Online (Sandbox Code Playgroud)

  • *"您正在将组件直接添加到JFrame,但是您需要使用其内容窗格."*不是大约1.5. (2认同)