JButton 没有出现在 GUI 上

JButton does not appear on the GUI

当我编译 运行 我的代码时,除了没有出现 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);

这可能是一个错字或对坐标系的误解,每个新面板都有自己的专用系统,其中 (0,0) 是 at 组件的左上角。按钮位于 (140, 250),但是 bottomPanel 只有 360 x 50,所以它在可见范围之外..

尝试更改为

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

您的第一个也是主要的错误是:topPanel.setLayout(null);。虽然空布局和 setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用它们时 运行 就会遇到更严重的困难。它们不会在 GUI 调整大小时调整组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们会完全失败,当在所有平台或不同的屏幕分辨率上查看时它们看起来很糟糕来自原来的

解决方案很简单:了解布局管理器以及如何使用它们,然后使用它们。您可以在此处找到 Swing 教程的链接,包括布局管理器和其他 Swing 资源的链接:Swing Info


例如,

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

public class TempConverter extends JPanel {
   private static final int PREF_W = 400;
   private static final int GAP = 5;
   private JTextField celJTextField = new JTextField(10);
   private JTextField farJTextField = new JTextField(10);
   private JSlider sliderJSlider = new JSlider(0, 100, 0);
   private JButton closeButton = new JButton("Close");

   public TempConverter() {
      sliderJSlider.setMajorTickSpacing(10);
      sliderJSlider.setMinorTickSpacing(5);
      sliderJSlider.setPaintTicks(true);
      sliderJSlider.setPaintLabels(true);

      JPanel textFieldPanel = new JPanel(new GridBagLayout());
      textFieldPanel.add(new JLabel("Celcius:"), createGbc(0, 0));
      textFieldPanel.add(celJTextField, createGbc(1, 0));
      textFieldPanel.add(new JLabel("Faranheit:"), createGbc(0, 1));
      textFieldPanel.add(farJTextField, createGbc(1, 1));
      JPanel textFieldWrapperPanel = new JPanel(new GridBagLayout());
      textFieldWrapperPanel.add(textFieldPanel);

      JPanel conversionPanel = new JPanel(new BorderLayout());
      conversionPanel.setBorder(BorderFactory.createTitledBorder("Foo"));
      conversionPanel.setLayout(new BorderLayout());
      conversionPanel.add(sliderJSlider, BorderLayout.PAGE_START);
      conversionPanel.add(textFieldWrapperPanel, BorderLayout.CENTER);

      JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
      bottomPanel.add(closeButton);

      setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
      setLayout(new BorderLayout());
      add(conversionPanel, BorderLayout.CENTER);
      add(bottomPanel, BorderLayout.PAGE_END);
   }

   private GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridheight = 1;
      gbc.gridwidth = 1;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = new Insets(GAP, GAP, GAP, GAP);

      return gbc;
   }

   @Override
   public Dimension getPreferredSize() {
      Dimension superSize = super.getPreferredSize();
      ;
      if (isPreferredSizeSet()) {
         super.getPreferredSize();
      }
      int prefW = Math.max(PREF_W, superSize.width);
      return new Dimension(prefW, superSize.height);
   }

   private static void createAndShowGui() {
      TempConverter mainPanel = new TempConverter();

      JFrame frame = new JFrame("TempConverter");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

现在您可能会说这看起来更复杂,也许确实如此,但是当您想要添加另一个 JTextField 和 JLabel 来表示开尔文温标时会发生什么?对于您的 GUI,您将需要调整 GUI 的大小并重新计算可能会受到添加新组件影响的任何组件的位置。对于我的 GUI,您只需添加几行新代码,并且更改导致我的代码出现错误的可能性比您的更改小 much。例如请注意下面的更改只需要 3 行代码。其他一切保持不变:

public class TempConverter extends JPanel {
   private static final int PREF_W = 400;
   private static final int GAP = 5;
   private JTextField celJTextField = new JTextField(10);
   private JTextField farJTextField = new JTextField(10);
   private JTextField KelvinJTextField = new JTextField(10); // !!! Added
   private JSlider sliderJSlider = new JSlider(0, 100, 0);
   private JButton closeButton = new JButton("Close");

   public TempConverter() {
      sliderJSlider.setMajorTickSpacing(10);
      sliderJSlider.setMinorTickSpacing(5);
      sliderJSlider.setPaintTicks(true);
      sliderJSlider.setPaintLabels(true);

      JPanel textFieldPanel = new JPanel(new GridBagLayout());
      textFieldPanel.add(new JLabel("Celcius:"), createGbc(0, 0));
      textFieldPanel.add(celJTextField, createGbc(1, 0));
      textFieldPanel.add(new JLabel("Faranheit:"), createGbc(0, 1));
      textFieldPanel.add(farJTextField, createGbc(1, 1));

      // !!! added
      textFieldPanel.add(new JLabel("Kelvin:"), createGbc(0, 2));
      textFieldPanel.add(KelvinJTextField, createGbc(1, 2));