Java 如何使GUI的内容以某种方式显示?(爪哇)

Java 如何使GUI的内容以某种方式显示?(爪哇),java,swing,user-interface,layout,layout-manager,Java,Swing,User Interface,Layout,Layout Manager,所以,我是编程新手,所以提前感谢你的帮助。我试着把我制作的这个以2为基数到10为基数/以10为基数到2为基数的计算器放到GUI中。就我的一生而言,我不知道如何很好地格式化它。我试着让它看起来像下面这样:顶部的两个单选按钮,输入文本字段,转换按钮,输出字段,以及清除按钮。关于我如何做到这一点有什么想法吗 import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.

所以,我是编程新手,所以提前感谢你的帮助。我试着把我制作的这个以2为基数到10为基数/以10为基数到2为基数的计算器放到GUI中。就我的一生而言,我不知道如何很好地格式化它。我试着让它看起来像下面这样:顶部的两个单选按钮,输入文本字段,转换按钮,输出字段,以及清除按钮。关于我如何做到这一点有什么想法吗

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.*;

@SuppressWarnings("serial")
public class GUI extends JFrame implements ActionListener
{

   private JTextField input;
   private JTextField output;
   private JRadioButton base2Button;
   private JRadioButton base10Button;
   private JButton convert;
   private JButton clear;
   private Container canvas = getContentPane();
   private Color GRAY;

       public GUI()

      {

     this.setTitle("Base 10-2 calc");
     this.setLayout(new FlowLayout(FlowLayout.LEFT));
     //this.setLayout(new GridLayout(2,2));
      base2Button = new JRadioButton( "Convert to base 2");   
      base10Button = new JRadioButton( "Convert to base 10"); 

      ButtonGroup radioGroup = new ButtonGroup();
      radioGroup.add(base2Button);
      radioGroup.add(base10Button);

      JPanel radioButtonsPanel = new JPanel();
      radioButtonsPanel.setLayout( new FlowLayout(FlowLayout.LEFT) );

      radioButtonsPanel.add(base2Button);
      radioButtonsPanel.add(base10Button);

      canvas.add(radioButtonsPanel);

      base2Button.setSelected( true );
      base10Button.setSelected( true );

      input = new JTextField(18);
      //input = new JFormattedTextField(20);
      canvas.add(input);
      output = new JTextField(18);
      //output = new JFormattedTextField(20);
      canvas.add(output);

      convert = new JButton("Convert!");
      convert.addActionListener(this);
      canvas.add(convert);

      clear = new JButton("Clear");
      clear.addActionListener(this);
      canvas.add(clear); 

      output.setBackground(GRAY);
      output.setEditable(false);

      this.setSize(300, 200);
      this.setVisible(true);
      this.setLocation(99, 101);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

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

    @Override
    public void actionPerformed(ActionEvent e) 
    {

        String s = e.getActionCommand();

        if(s.equals("Convert!"))
        {
            String numS = input.getText();
            int numI = Integer.parseInt(numS);

            if(base2Button.isSelected())
            {
              output.setText(Integer.toBinaryString(Integer.valueOf(numI)));
            }

            if(base10Button.isSelected())
            {
              output.setText("" + Integer.valueOf(numS,2));
            }
         }    

         if(s.equals("Clear"))
         {
             input.setText("");
             output.setText("");
         }


     }

}

对于简单布局,您可以使用一列GridLayout,然后使用一组子面板和FlowLayout,这些子面板根据单行中的可用空间对齐组件。如果您想要更多的控制,我建议您学习GridBagLayout管理器,它是GridLayout的一个更灵活的版本

public class ExampleGUI {

    public ExampleGUI() {
        init();
    }
    
    private void init() {
        JFrame frame = new JFrame();
        
        // Set the frame's layout to a GridLayout with one column
        frame.setLayout(new GridLayout(0, 1));
        frame.setPreferredSize(new Dimension(300, 300));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Child panels, each with FlowLayout(), which aligns the components
        // in a single row, until there's no more space
        JPanel radioButtonPanel = new JPanel(new FlowLayout());
        JRadioButton button1 = new JRadioButton("Option 1");
        JRadioButton button2 = new JRadioButton("Option 2");
        radioButtonPanel.add(button1);
        radioButtonPanel.add(button2);
        
        JPanel inputPanel = new JPanel(new FlowLayout());
        JLabel inputLabel = new JLabel("Input: ");
        JTextField textField1 = new JTextField(15);
        inputPanel.add(inputLabel);
        inputPanel.add(textField1);
        
        JPanel convertPanel = new JPanel(new FlowLayout());
        JButton convertButton = new JButton("Convert");
        convertPanel.add(convertButton);
        
        JPanel outputPanel = new JPanel(new FlowLayout());
        JLabel outputLabel = new JLabel("Output: ");
        JTextField textField2 = new JTextField(15);
        outputPanel.add(outputLabel);
        outputPanel.add(textField2);
        
        // Add the child panels to the frame, in order, which all get placed
        // in a single column
        frame.add(radioButtonPanel);
        frame.add(inputPanel);
        frame.add(convertPanel);
        frame.add(outputPanel);
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        ExampleGUI example = new ExampleGUI();
    }

}
最终结果是:


阅读布局管理器教程,然后使用布局管理器。您可以在此处找到布局管理器教程:,也可以在此处找到指向Swing教程和其他Swing资源的链接:。记住,您可以嵌套JPanel,每个都使用自己的布局,以便创建复杂且易于维护的GUI。