【问题标题】:Unstable GUI in JavaJava中不稳定的GUI
【发布时间】:2012-12-12 04:15:17
【问题描述】:

我正在编写一个非常简单的 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 = "";
    }
}

【问题讨论】:

    标签: java swing user-interface controls


    【解决方案1】:

    这段代码有几个问题:

    • 您已经从 JFrame 继承,因此无需再创建另一个 JFrame
    • 您正在使用setVisible(true) 显示您的框架,然后向其中添加组件。这会使您的布局无效,您需要之后重新验证(或将 setVisible() 移动到您已添加组件的位置)
    • 您正在将组件直接添加到 JFrame,但您需要使用它的内容窗格。从Java 1.5 开始,JFrame.add() 方法会自动转发到内容窗格。在earlier versions 中,需要使用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);
    }
    

    【讨论】:

    • “您将组件直接添加到 JFrame,但您需要使用它的内容窗格。” 大约从 1.5 开始就没有了。
    • @AndrewThompson 很好,谢谢!我更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2017-05-11
    相关资源
    最近更新 更多