La creación de un marco básico con 3 botones de radio y un botón de envío

dale1221:
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);

Estoy tratando de hacer un marco que tiene 3 botones de radio y un botón de envío, pero no importa lo que hago cada vez que ejecuto es sólo un marco con un gran botón Enviar. Lo que está mal con mi código? Gracias por adelantado.

Gilbert Le Blanc:

En lugar de tratar de lugar y tamaño de todo usted mismo, el uso de Swing Layout Managers .

Aquí está la interfaz gráfica de usuario que se me ocurrió.

Rotación de la imagen GUI

Estos son los cambios que hice.

  1. He añadido una llamada a los SwingUtilities invokeLater método para asegurar que los componentes Swing son creados y ejecutados en el hilo de eventos de Despacho.

  2. Me anidado JPanels así que podía usar un BorderLayout y un GridLayout.

  3. Agrupé oscilación llamadas a métodos de componentes juntos y organizados por fila y columna. Esto hace que sea mucho mucho mucho más fácil encontrar y problemas de solución.

Aquí está el código.

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);
    }

}

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=371153&siteId=1
Recomendado
Clasificación