gpt4 book ai didi

java - Java GUI 中单选按钮的对齐方式

转载 作者:行者123 更新时间:2023-11-30 06:54:18 24 4
gpt4 key购买 nike

这是我的程序的样子:

Program Example

但我似乎无法让我的单选按钮和 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();
}
}

最佳答案

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

  1. 您正在扩展 JFrame 并在同一程序中创建它的实例。使用其中之一(我推荐后者),请参阅Using extends vs calling it inside of class .

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

  3. 您没有将程序放在 Event Dispatch Thread (EDT) 上,这可能会在将来导致线程问题,因为 Swing 不是线程安全的,这可以通过以下方式解决:

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

but I can't seem to get my radio buttons and my JLabel to be aligned properly

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

我的想法是使用一个带有 0 行(它将根据需要添加任意数量)和 2 列的 GridLayout 来实现您想要的 GUI,并且在您需要“空白”空间的地方可以添加空的JLabel

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

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

enter image description here

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

关于java - Java GUI 中单选按钮的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42121486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com