【问题标题】:Adjusting the buttons from top to the bottom in GUI Java Swing在 GUI Java Swing 中从上到下调整按钮
【发布时间】:2020-04-02 22:08:20
【问题描述】:

下面的代码是将十六进制值转换为十进制和二进制的 GUI。问题是按钮打印在顶部,我还附上了一些图片以供参考,以了解我想要的结果。

class MyFrames implements ActionListener
{

    JFrame DancingFinchFrame;

    static JLabel NewLabel, DecimalValue, BinaryValue, FinchSpeed;
    static JTextField textField, DecimalText, BinaryText, SpeedText;
    static JButton Convert, Dance;
    static String Hexadecimal, Binary;
    static int Decimal, Speed;


    MyFrames()  

    { 
        // creates a frame
        DancingFinchFrame=new JFrame("Calculator");
        DancingFinchFrame.setTitle("Dancing Finch");
        DancingFinchFrame.setSize(320, 280);
        DancingFinchFrame.setLocationRelativeTo(null);
        DancingFinchFrame.setResizable(false);
        DancingFinchFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        NewLabel = new JLabel("Please enter a hexadecimal value");
        NewLabel.setVerticalAlignment(SwingConstants.TOP);
        NewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        NewLabel.setFont(new Font("Arial", Font.BOLD, 15));

        textField = new JTextField();
        textField.setHorizontalAlignment(SwingConstants.CENTER);
        textField.setColumns(20);

        Convert = new JButton("CONVERT");       
        Convert.setBounds(10, 400, 100, 40);
        
        
        DecimalValue = new JLabel("Decimal Value is ");
        DecimalValue.setVisible(false);
        DecimalValue.setFont(new Font("Arial", Font.BOLD, 15));
        DecimalValue.setVisible(false);

        DecimalText = new JTextField();
        DecimalText.setEditable(false);
        DecimalText.setVisible(false);
        DecimalValue.setLabelFor(DecimalText);
        DecimalText.setColumns(10);

        BinaryValue = new JLabel("Binary Value is ");
        BinaryValue.setVisible(false);
        BinaryValue.setFont(new Font("Arial", Font.BOLD, 15));
        BinaryValue.setToolTipText("");

        BinaryText = new JTextField();
        BinaryText.setEditable(false);
        BinaryText.setVisible(false);
        BinaryValue.setLabelFor(BinaryText);
        BinaryText.setColumns(10);

        FinchSpeed = new JLabel("Finch Speed is ");
        FinchSpeed.setVisible(false);
        FinchSpeed.setFont(new Font("Arial", Font.BOLD, 15));
        
        SpeedText = new JTextField();
        SpeedText.setVisible(false);
        SpeedText.setEditable(false);
        FinchSpeed.setLabelFor(SpeedText);
        SpeedText.setColumns(10);
        
        Dance = new JButton("DANCE");
        Dance.setVisible(false);**
        
    
        // creates the panel 
        JPanel mypanel = new JPanel();

        //adds action listeners and initialises the event handling for the buttons
        Convert.addActionListener(this); 
        Dance.addActionListener(this); 
        textField.addActionListener(this);

        // adds elements to the panel 
        mypanel.add(Convert); 
        mypanel.add(Dance); 
        mypanel.add(NewLabel); 
        mypanel.add(textField); 
        mypanel.add(DecimalValue); 
        mypanel.add(DecimalText); 
        mypanel.add(BinaryValue); 
        mypanel.add(BinaryText); 
        mypanel.add(FinchSpeed); 
        mypanel.add(SpeedText); 

        mypanel.setBackground(Color.white); 

        // adds panel to frame 
        DancingFinchFrame.add(mypanel); 
        DancingFinchFrame.setSize(320, 280); 
        DancingFinchFrame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {

        Hexadecimal = textField.getText();
        Decimal = DecimalConverter.HexaToDecimal(Hexadecimal);
        Binary = BinaryConverter.HexaToBinary(Hexadecimal);
        Speed = SpeedCalculator.Speed(Decimal);


        if(e.getSource()==Convert)

        {
            if (Decimal >= 144 && Decimal <= 255)

            {
                DecimalValue.setVisible(true);
                DecimalText.setVisible(true);
                BinaryValue.setVisible(true);
                BinaryText.setVisible(true);
                Dance.setVisible(true);
                FinchSpeed.setVisible(true);
                SpeedText.setVisible(true);
                DecimalText.setText(""+ Decimal);
                BinaryText.setText(""+ Binary);
                SpeedText.setText("" + Speed);
            }
            else
            {
                try
                {
                    JOptionPane.showMessageDialog(null, "Please enter a valid input!", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
                catch (Exception ne) 
                {
                    ne.printStackTrace();
                }
            }
        }


        else if (e.getSource()==Dance)  

        {
            int decimal = Integer.parseInt(SpeedText.getText());
            String binary = BinaryText.getText();

            FinchCommands Command = new FinchCommands();
            Command.Dance(decimal, binary);
        }
    }

    public JFrame getDancingFinchFrame() 

    {
        return DancingFinchFrame;
    }


}

问题是转换/跳舞按钮打印在顶部

这就是我想要的

提前谢谢你!!

【问题讨论】:

    标签: java swing user-interface layout-manager


    【解决方案1】:

    首先,你用第一个大写字母声明变量,这不是标准的,当你的程序增长或几年后做其他事情时可能很难处理。

    其次,您不使用任何布局。您使用 FlowLayout 是因为它似乎是默认设置。但是使用简单的 BorderLayout,您将能够为您的控件和字段定义区域。

    为了深入了解并了解如何使用它们,我为您提供了一个很好的链接: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

    【讨论】:

      【解决方案2】:

      这个布局:

      可以使用此代码制作:

      JPanel ui = new JPanel(new GridBagLayout());
      ui.setBorder(new EmptyBorder(10,40,10,40));
      
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = 2;
      gbc.insets = new Insets(5,5,5,5);
      
      ui.add(new JLabel("Please enter a hexadecimal value"), gbc);
      gbc.gridy = 1;
      ui.add(new JTextField("90", 10), gbc);
      gbc.gridy = 2;
      ui.add(new JButton("Convert"), gbc);
      gbc.gridy = 6;
      ui.add(new JButton("Dance"), gbc);
      
      gbc.gridwidth = 1;
      String[] name = {"Decimal Value", "Binary Value", "Finch Speed"};
      for (int ii=0; ii<3; ii++) {
          gbc.gridy = ii+3;
          gbc.gridx = 0;
          ui.add(new JLabel(name[ii]), gbc);
          gbc.gridx = 1;
          ui.add(new JTextField(6), gbc);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-03
        • 1970-01-01
        • 2016-09-16
        • 2018-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多