java - JButton 未出现在 GUI 上

标签 java swing layout jbutton layout-manager

当我编译并运行我的代码时,除了 JButton 没有出现之外,一切似乎都工作正常。我将其添加到框架上的 JPanel 中。我是新来的,所以我可能不知道在这里要注意什么。谢谢!

import java.awt.*;
import java.awt.event.*;
import java.text.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class TemperatureConverter extends JFrame{

    //declarations
    private JLabel celJLabel, farJLabel;

    private JTextField celJTextField, farJTextField;

    private JSlider sliderJSlider;

    private JButton closeButton;

    private TitledBorder border;

    private JPanel topPanel, bottomPanel;

    double celsiusDegrees, farenheitDegrees, sliderValue;

    DecimalFormat decimalFormat = new DecimalFormat("#.0");

    public TemperatureConverter()
    {
        createUserInterface();
    }

    public void createUserInterface()
    {
        //create the JFrame
        Container frame = getContentPane();
        frame.setBackground(Color.white);
        frame.setLayout(null);

        border = new TitledBorder("Convert between C & F");
        border.setTitleColor(Color.black);
        border.setTitleFont(new Font("Default", Font.ITALIC, 12));
        border.setTitleJustification(TitledBorder.LEFT);
        border.setTitlePosition(TitledBorder.TOP);

        topPanel = new JPanel();
        topPanel.setBounds(20,10,360,300);
        topPanel.setForeground(Color.black);
        topPanel.setBackground(Color.white);
        topPanel.setLayout(null);
        topPanel.setBorder(border);
        frame.add(topPanel);

        bottomPanel = new JPanel();
        bottomPanel.setBounds(20,310,360,50);
        bottomPanel.setForeground(Color.black);
        bottomPanel.setBackground(Color.white);
        bottomPanel.setLayout(null);
        frame.add(bottomPanel);

        celJLabel = new JLabel();
        celJLabel.setBounds(120, 200, 60, 20);
        celJLabel.setBackground(Color.white);
        celJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        celJLabel.setText("Celcius");
        celJLabel.setHorizontalAlignment(JLabel.LEFT);
        topPanel.add(celJLabel);

        farJLabel = new JLabel();
        farJLabel.setBounds(120, 220, 60, 20);
        farJLabel.setBackground(Color.white);
        farJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        farJLabel.setText("Faranheit");
        farJLabel.setHorizontalAlignment(JLabel.LEFT);
        topPanel.add(farJLabel);

        celJTextField = new JTextField();
        celJTextField.setBounds(195,200, 50, 15);
        celJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        celJTextField.setHorizontalAlignment(JTextField.CENTER);
        celJTextField.setForeground(Color.black);
        celJTextField.setBackground(Color.white);
        topPanel.add(celJTextField);

        farJTextField = new JTextField();
        farJTextField.setBounds(195,225, 50, 15);
        farJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        farJTextField.setHorizontalAlignment(JTextField.CENTER);
        farJTextField.setForeground(Color.black);
        farJTextField.setBackground(Color.white);
        topPanel.add(farJTextField);

        sliderJSlider = new JSlider(JSlider.HORIZONTAL, 0,100,0);
        sliderJSlider.setBounds(20, 20, 310, 120);
        sliderJSlider.setMajorTickSpacing(10);
        sliderJSlider.setMinorTickSpacing(5);
        sliderJSlider.setPaintTicks(true);
        sliderJSlider.setPaintLabels(true);
        sliderJSlider.setForeground(Color.black);
        sliderJSlider.setBackground(Color.white);
        topPanel.add(sliderJSlider);
        sliderJSlider.addChangeListener( 
                new ChangeListener()
                {
                    public void stateChanged(ChangeEvent event)
                    {
                        sliderStateChanged(event);
                    }       
                }

                );

        closeButton = new JButton();
        closeButton.setBounds(140, 250, 75, 20);
        closeButton.setFont(new Font("Default", Font.PLAIN,12));
        closeButton.setText("Close");
        closeButton.setForeground(Color.black);
        closeButton.setBackground(Color.white);
        bottomPanel.add(closeButton);
        closeButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    closeActionPerformed(event);
                }
            }
            );

        setTitle("Temperature Converter");
        setSize(400,400);
        setVisible(true);

    }   
    public static void main(String[] args)
    {
        TemperatureConverter application = new TemperatureConverter();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void sliderStateChanged(ChangeEvent event)
    {
        farenheitDegrees = sliderJSlider.getValue();
        calculateCelsiusTemperature();

    }
    public void calculateCelsiusTemperature()
    {
        celsiusDegrees = (farenheitDegrees - 32)*5.0/9.0;
        outputTemps();
    }
    public void outputTemps()
    {
        celJTextField.setText(decimalFormat.format(celsiusDegrees));
        farJTextField.setText(decimalFormat.format(farenheitDegrees));
    }
    public void closeActionPerformed(ActionEvent event)
    {
        TemperatureConverter.this.dispose();
    }

    }

最佳答案

我会遵循评论中的建议,使用 proper layout manager

实际的错误是底部面板内关闭按钮的位置。

closeButton.setBounds(140, 250, 75, 20);

这可能是一个拼写错误或对坐标系的误解,每个新面板都有自己的私有(private)系统,其中 (0,0) 是 at 组件的左上角。按钮位于 (140, 250),但 BottomPanel 只有 360 x 50,因此它超出了可见边界。

尝试更改为

closeButton.setBounds(0, 0, 75, 20);

关于java - JButton 未出现在 GUI 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28919947/

相关文章:

java - 循环通过 FileInputStream 时出现无限循环

java - 收缩字符串与使用原始字符串创建新字符串/编辑原始字符串时的程序效率

Java 计时器 - 使用 Platform.runLater 更新标签

java - JScrollPane 格式化以显示 Pascals 三角形

java - 如何将任务栏按钮上的图像更改为 Swing 应用程序上的自定义图像?

html - 由于内容过多,页脚错位

xml - Magento 布局 module_default

java - 如何在 JTextPane 中添加图片?

java - 在 Jbutton 启用之前要填充多个 Jtextfields

c# - 如何取消以编程方式添加的菜单的垂直空间?