Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GUI中单选按钮的对齐_Java_Swing_Radio Button_Layout Manager - Fatal编程技术网

Java GUI中单选按钮的对齐

Java GUI中单选按钮的对齐,java,swing,radio-button,layout-manager,Java,Swing,Radio Button,Layout Manager,我的程序应该是这样的: 但我似乎无法使我的单选按钮和JLabel正确对齐。如何对齐右侧的单选按钮和堆叠的单选按钮?另外,如何让我的JLabel和JTextField显示堆栈? 这是我的密码: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SeriesCalc extends JFrame { private final int WIDTH = 300; private final

我的程序应该是这样的:

但我似乎无法使我的单选按钮和JLabel正确对齐。如何对齐右侧的单选按钮和堆叠的单选按钮?另外,如何让我的JLabel和JTextField显示堆栈? 这是我的密码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SeriesCalc extends JFrame {

private final int WIDTH = 300;
private final int HEIGHT = 300;
private JFrame frame = new JFrame("Recursion");
private JPanel panel = new JPanel();
private JPanel labels = new JPanel();
private JPanel buttons = new JPanel();
private JPanel radioButtonsPanel = new JPanel();
private JLabel inputLabel = new JLabel("Enter i:");
private JLabel resultLabel = new JLabel("Result:");
private JTextField inputField = new JTextField(15);
private JTextField resultField = new JTextField(15);
private JButton compute = new JButton("Compute");
private JRadioButton iterative, recursive;

public SeriesCalc() {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    radioButtonsPanel.add(iterative = new JRadioButton("Iterative"));
    radioButtonsPanel.add(recursive = new JRadioButton("Recursive"));
    add(radioButtonsPanel);
    ButtonGroup radioButtons = new ButtonGroup();
    radioButtons.add(iterative);
    radioButtons.add(recursive);

    iterative.addActionListener(new Calculations());
    recursive.addActionListener(new Calculations());
    compute.addActionListener(new Calculations());

    resultField.setEditable(false);

    panel.setLayout(new GridLayout(4, 1));
    labels.add(inputLabel);
    labels.add(inputField);
    labels.add(resultLabel);
    labels.add(resultField);
    buttons.add(compute);
    panel.add(radioButtonsPanel);
    panel.add(labels);
    panel.add(buttons);
    frame.getContentPane().add(panel);
}

public void display() {
    frame.setSize(WIDTH, HEIGHT);
    frame.setVisible(true);
}

public class Calculations implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        Object calc = e.getSource();
        try {
            if (calc == compute) {
                if (iterative.isSelected()) {
                    double n = Double.parseDouble(inputField.getText());
                    double product = 1;
                    for (int i = 3; i < n; i++) {
                        product *= i;
                    }
                    resultField.setText(Double.toString(product));
                } else if (recursive.isSelected()) {
                    double i = Double.parseDouble(inputField.getText());
                    double y = 0;
                    if (i == 1) {
                resultField.setText(Double.toString(i / (2. * i +     1)));
                    } else {
             resultField.setText(Double.toString(i / (2. * i + 1)+ (i -1)));
                    }
                }
            }
        } catch (NumberFormatException nfe) {
            System.err.println(nfe.getMessage());
        }
    }
}

public static void main(String[] args) {
    SeriesCalc calculator = new SeriesCalc();
    calculator.display();
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类SerieCalc扩展JFrame{
私有最终整数宽度=300;
私人最终内部高度=300;
私有JFrame=新JFrame(“递归”);
private JPanel panel=new JPanel();
私有JPanel标签=新JPanel();
私有JPanel按钮=新JPanel();
专用JPanel radioButtonsPanel=新JPanel();
私有JLabel inputLabel=新JLabel(“输入i:”);
private JLabel resultLabel=新JLabel(“结果:”);
私有JTextField inputField=新JTextField(15);
私有JTextField resultField=新JTextField(15);
私有JButton compute=新JButton(“compute”);
私有JRadioButton迭代、递归;
公共服务CALC(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
radiobuttonPanel.add(迭代=新的JRadioButton(“迭代”);
radiobuttonPanel.add(recursive=newjradiobutton(“recursive”);
添加(radioButtonsPanel);
ButtonGroup单选按钮=新建ButtonGroup();
单选按钮。添加(迭代);
单选按钮。添加(递归);
iterative.addActionListener(新计算());
recursive.addActionListener(新计算());
compute.addActionListener(新计算());
resultField.setEditable(false);
panel.setLayout(新网格布局(4,1));
标签。添加(inputLabel);
标签。添加(输入字段);
标签。添加(结果标签);
labels.add(resultField);
按钮。添加(计算);
面板。添加(radioButtonsPanel);
面板。添加(标签);
面板。添加(按钮);
frame.getContentPane().add(面板);
}
公共空间显示(){
框架尺寸(宽度、高度);
frame.setVisible(true);
}
公共类计算实现ActionListener{
已执行的公共无效操作(操作事件e){
对象计算=e.getSource();
试一试{
如果(计算==计算){
if(iterative.isSelected()){
double n=double.parseDouble(inputField.getText());
双积=1;
对于(int i=3;i
我不知道您是否正在使用Eclipse或NetBeans之类的IDE。例如,在Eclipse中,您可以使用windowbuilder打开此类,并可以可视化框架的显示方式。显然,您可以在此视图(窗口生成器)中移动元素,并将所有对象放置在所需的位置和大小。在下一幅图中,如果您在Eclipse的设计模式下使用window builder打开框架,您可以看到框架的编辑视图。在此模式(设计)中,您可以将绝对布局放置在这些对象上,并且可以使用该布局将它们放置在您想要的位置。我建议您使用像eclipse这样的IDE进行Java开发,它非常有用,并且有很多工具。我希望我能帮助你: 在此处输入图像描述


我在您的程序中看到一些错误:

  • 您正在扩展
    JFrame
    ,并在同一程序中创建它的实例。使用其中一种(我建议使用后者),请参阅

  • 您正在设置
    frame.setSize()
    ,虽然这不是错误,但最好在程序中调用
    frame.pack()
    。它将遵循最小尺寸,其中所有组件都显示在其
    preferredSize
    s中。如果需要窗口的精确大小,请改为重写
    getPreferredSize()
    方法

  • 您没有将程序放在上,这可能会在将来导致线程问题,因为Swing不是线程安全的,这可以通过以下方法解决:

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Call your constructor here
            }
        });
    }
    

  • 但我似乎无法使我的单选按钮和JLabel正确对齐

    这是因为
    JPanel
    的默认布局管理器是
    FlowLayout
    ,因此它会将组件放在一行中

    我的想法是使用一个
    GridLayout
    ,它有0行(可以根据需要添加多行)和2列,在需要“空白”空间的地方,可以添加空的
    JLabel
    s

    我没有在代码上放置
    ActionListener
    ,因为这个问题是关于GUI设计的,而不是其中的逻辑

    我想我没有遗漏任何东西,这是下面代码创建的输出:


    这个项目不允许我在NetBeans中使用GUI,我将使用java.awtIF如果您要使用AWT(一个坏主意-它已经过时几十年了),那么为什么您要使用Swing而不是>@uheredero001使用空布局几乎总是一个坏主意;由于您可能不知道所有组件的默认插图,因此很难正确布局组件,并且如果您想要更改字体等,将导致噩梦。最好首先使用正确的布局管理器。我非常同意@FredK:绝对布局/空布局是一样的,请停止建议使用它。见和。此外,您的代码应该在答案内的代码格式文本中,而不是在
    import java.awt.GridLayout;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class SeriesCalc {
    
        private JFrame frame;
    
        private JRadioButton iterative;
        private JRadioButton recursive;
        private ButtonGroup group;
    
        private JLabel label;
        private JLabel resultLabel;
    
        private JTextField field;
        private JTextField resultField;
    
        private JButton computeButton;
    
        private JPanel pane;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new SeriesCalc().createAndShowGui();
                }
            });
        }
    
        public void createAndShowGui() {
            frame = new JFrame(getClass().getSimpleName());
    
            pane = new JPanel();
            pane.setLayout(new GridLayout(0, 2, 2, 5));
    
            iterative = new JRadioButton("Iterative");
            recursive = new JRadioButton("Recursive");
    
            group = new ButtonGroup();
    
            group.add(iterative);
            group.add(recursive);
    
            computeButton = new JButton("Compute");
    
            label = new JLabel("Enter \"i\": ");
            resultLabel = new JLabel("Result: ");
    
            field = new JTextField(5);
            resultField = new JTextField(5);
            resultField.setEnabled(false);
    
            pane.add(new JLabel(""));
            pane.add(iterative);
    
            pane.add(new JLabel(""));
            pane.add(recursive);
    
            pane.add(label);
            pane.add(field);
    
            pane.add(new JLabel(""));
            pane.add(computeButton);
    
            pane.add(resultLabel);
            pane.add(resultField);
    
            frame.add(pane);
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }