我有计算器的按钮全部布局但我需要在左侧添加单选按钮,以便让用户选择他们想要转换的数字。选项包括hexa,octa,dec和binary。死亡之后,数字显示为生病了。感谢您的帮助和时间。
这是我到目前为止的代码
public class CalculatorBuild extends JFrame implements ActionListener {
public CalculatorBuild() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GridLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(6, 4));
frame.add(new JButton("A"));
frame.add(new JButton(""));
frame.add(new JButton(""));
frame.add(new JButton(""));
frame.add(new JButton(""));
frame.add(new JButton("B"));
frame.add(new JButton("<-"));
frame.add(new JButton("CE"));
frame.add(new JButton("c"));
frame.add(new JButton("%"));
frame.add(new JButton("C"));
frame.add(new JButton("7"));
frame.add(new JButton("8"));
frame.add(new JButton("9"));
frame.add(new JButton("+"));
frame.add(new JButton("D"));
frame.add(new JButton("4"));
frame.add(new JButton("5"));
frame.add(new JButton("6"));
frame.add(new JButton("-"));
frame.add(new JButton("E"));
frame.add(new JButton("1"));
frame.add(new JButton("2"));
frame.add(new JButton("3"));
frame.add(new JButton("*"));
frame.add(new JButton("F"));
frame.add(new JButton("."));
frame.add(new JButton("0"));
frame.add(new JButton("+/-"));
frame.add(new JButton("="));
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// listener code
}
}
答案 0 :(得分:1)
Here is Oracle's tutorial on radio buttons
就你的具体情况而言,这是你可以尝试的:
JRadioButton hexa = new JRadioButton("hexa");
hexa.setSelected(true);
// Make some more buttons here.
ButtonGroup bg = new ButtonGroup();
bg.add(hexa);
// Add the other buttons.
另外,我建议创建一个JPanel来添加所有按钮。然后将JPanel添加到JFrame。 There is an Oracle tutorial for this here也涵盖了内容窗格。