这是我的程序假设的样子:
但我似乎无法将我的单选按钮和我的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();
}
}
答案 0 :(得分:2)
我在你的程序中看到了一些错误:
您正在扩展JFrame
并在同一程序中创建它的实例。使用其中一种(我建议使用后者),参见Using extends vs calling it inside of class。
您设置frame.setSize()
虽然这不是错误,但在程序中调用frame.pack()
总是更好。它将尊重所有组件在preferredSize
中显示的最小尺寸。如果您需要使用窗口覆盖getPreferredSize()
方法的确切大小。
您没有将您的程序放在Event Dispatch Thread (EDT)上,这可能导致将来出现线程问题,因为Swing不是线程安全的,可以通过以下方式解决:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//Call your constructor here
}
});
}
但我似乎无法将我的单选按钮和我的JLabel正确对齐
这是因为JPanel
的默认布局管理器为FlowLayout
,因此它会将您的组件放在一行中。
我想要达到你想要的GUI的想法是使用一个{0}行GridLayout
(它将根据需要添加多少行)和2列,并且你需要一个&#34;空白&#34;空格,您可以添加空JLabel
s。
我没有在我的代码上放置ActionListener
,因为这个问题是关于GUI设计而不是其中的逻辑。
我想我没有遗漏任何东西,这是以下代码创建的输出:
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);
}
}
答案 1 :(得分:-1)