有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java用3个单选按钮和一个提交按钮创建一个基本框架

public void rotateGUI() {
   JFrame rotateFrame = new JFrame("Image Rotation");
   JRadioButton rotateLeft = new JRadioButton("Rotate Left");
   JRadioButton rotateRight = new JRadioButton("Rotate Right"); 
   JRadioButton upsideDown = new JRadioButton("Rotate Upside Down");
   JButton submit = new JButton("Submit");
   ButtonGroup rotateButtons = new ButtonGroup();
   rotateLeft.setBounds(120,30,120,50);
   rotateRight.setBounds(120,30,120,50);
   upsideDown.setBounds(120,30,120,50);
   submit.setBounds(125,90,80,30);
   rotateFrame.add(rotateLeft);
   rotateFrame.add(rotateRight);
   rotateFrame.add(upsideDown);
   rotateFrame.add(submit);
   rotateButtons.add(rotateLeft);
   rotateButtons.add(rotateRight);
   rotateButtons.add(upsideDown);
   submit.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    if (rotateLeft.isSelected()) {    
          rotationAngle = 90; 
       } 
       else if (upsideDown.isSelected()) { 

              rotationAngle = 180; 
       } 
       else if (rotateRight.isSelected()){ 

              rotationAngle = 270;
       }
    }

        });
    rotateFrame.setBounds(200, 200, 400, 200);
    rotateFrame.setVisible(true);

我正在尝试制作一个有3个单选按钮和一个提交按钮的框架,但无论我何时运行它,它都只是一个有一个大提交按钮的框架。我的代码有什么问题?提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    使用Swing Layout Managers而不是尝试自己放置和调整所有内容的大小

    这是我设计的图形用户界面

    Image Rotation GUI

    这是我做的改动

    1. 我添加了对SwingUtilities invokeLater方法的调用,以确保在事件调度线程上创建和执行Swing组件

    2. 我嵌套了JPanel,以便使用BorderLayout和GridLayout

    3. 我将Swing组件方法调用分组在一起,并按行和列组织它们。这使得查找和修复问题变得更加容易

    这是密码

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.SwingUtilities;
    
    public class ExampleGUI {
    
        private int rotationAngle;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new ExampleGUI().rotateGUI();
                }
            });
        }
    
        public void rotateGUI() {
            JFrame rotateFrame = new JFrame("Image Rotation");
            rotateFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BorderLayout());
            mainPanel.setPreferredSize(new Dimension(300, 100));
    
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(0, 1));
    
            ButtonGroup rotateButtons = new ButtonGroup();
            JRadioButton rotateLeft = new JRadioButton("Rotate Left");
            rotateButtons.add(rotateLeft);
            JRadioButton rotateRight = new JRadioButton("Rotate Right");
            rotateButtons.add(rotateRight);
            JRadioButton upsideDown = new JRadioButton("Rotate Upside Down");
            rotateButtons.add(upsideDown);
    
            buttonPanel.add(rotateLeft);
            buttonPanel.add(rotateRight);
            buttonPanel.add(upsideDown);
    
            mainPanel.add(buttonPanel, BorderLayout.BEFORE_FIRST_LINE);
    
            JButton submit = new JButton("Submit");
            submit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (rotateLeft.isSelected()) {
                        rotationAngle = 90;
                    } else if (upsideDown.isSelected()) {
                        rotationAngle = 180;
                    } else if (rotateRight.isSelected()) {
                        rotationAngle = 270;
                    }
                }
            });
            mainPanel.add(submit, BorderLayout.AFTER_LAST_LINE);
    
            rotateFrame.add(mainPanel);
            rotateFrame.pack();
            rotateFrame.setLocationByPlatform(true);
            rotateFrame.setVisible(true);
        }
    
    }