我一直试图让我的JApplet以有序的方式显示计算器applet所需的所有按钮,但我似乎无法让它们全部具有相同的尺寸并且在彼此之下显示。我应该使用不同的不同布局还是改变整个设计?提前谢谢。
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyCalc extends JApplet implements ActionListener{
/**
*
*/
protected static final long serialVersionUID = 3169756252830354073L;
private JMenuBar menuBar = new JMenuBar();
private JMenu edit = new JMenu("Edit");
private JMenu view = new JMenu("View");
private JMenu help = new JMenu("Help");
//Instantiated all JPanels used in this Applet
private JPanel[] rows = {new JPanel(), new JPanel(), new JPanel(),
new JPanel(), new JPanel(), new JPanel(), new JPanel()};
JTextField calc = new JTextField(1);
private JButton[] buttons = {new JButton("Backspace"), new JButton("CE"), new JButton("C"),
new JButton("MC"), new JButton("7"), new JButton("8"), new JButton("9"),
new JButton("/"), new JButton("sqrt"), new JButton("MR"), new JButton("4"),
new JButton("5"), new JButton("6"), new JButton("*"), new JButton("%"),
new JButton("MS"), new JButton("1"), new JButton("2"), new JButton("3"),
new JButton("-"), new JButton("1/x"), new JButton("M+"), new JButton("0"),
new JButton("+/-"), new JButton("."), new JButton("+"), new JButton("=")};
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttons[0])
{
}
}
public void init()
{
setName("Calculator Applet");
menuBar.add(edit);
menuBar.add(view);
menuBar.add(help);
setJMenuBar(menuBar);
rows[0].setLayout(new BoxLayout(rows[0], BoxLayout.Y_AXIS));
getContentPane();
add(rows[0]);
setSize(300, 200);
calc.setText("0.");
calc.setHorizontalAlignment(JTextField.RIGHT);
for(int i = 0; i < 3; i++)
{
rows[2].add(buttons[i]);
}
for(int i = 3; i < 9; i++)
{
rows[3].add(buttons[i]);
}
for(int i = 9; i < 15; i++)
{
rows[4].add(buttons[i]);
}
for(int i = 15; i < 21; i++)
{
rows[5].add(buttons[i]);
}
for(int i = 21; i < 26; i++)
{
rows[6].add(buttons[i]);
}
rows[1].add(calc);
rows[1].setLayout(new GridLayout());
rows[2].setLayout(new GridLayout());
rows[0].add(rows[1]);
rows[0].add(rows[2]);
for(int i = 3; i < 7; i++)
{
rows[0].add(rows[i]);
rows[i].setLayout(new FlowLayout(FlowLayout.CENTER));
}
}
答案 0 :(得分:2)
&#34;但我似乎无法将它们全部放在相同的尺寸下并显示在彼此之下。&#34;
通常这会尖叫GridLayout
。而不是使用两个BoxLayout
(一个垂直和一个水平)只需使用GridLayout(4, 6)
(对于数字按钮)。请记住,按钮的大小将是最大按钮的大小(即sqrt)。
另外,我建议远离将您的面板保持在阵列中,就像您正在做的那样。很难跟踪你想要添加所有内容的流程。为您的面板(以及所有组件)提供有意义的自我记录名称。
有关布局管理器的一般知识,请查看Laying out Component Within a Container
我重构了您的代码,以便能够将其作为applet或独立框架运行。我还将所有组件包装在一个面板中,并将内容窗格的布局管理器设置为GridBagLayout
,因此如果调整框架大小,一切都搞定了
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MyCalc extends JApplet implements ActionListener {
/**
*
*/
protected static final long serialVersionUID = 3169756252830354073L;
private JMenuBar menuBar = new JMenuBar();
private JMenu edit = new JMenu("Edit");
private JMenu view = new JMenu("View");
private JMenu help = new JMenu("Help");
// Instantiated all JPanels used in this Applet
//private JPanel[] rows = { new JPanel(), new JPanel(), new JPanel(),
// new JPanel(), new JPanel(), new JPanel(), new JPanel() };
JTextField calc = new JTextField(1);
private JButton[] buttons = { new JButton("Backspace"), new JButton("CE"),
new JButton("C"), new JButton("MC"), new JButton("7"),
new JButton("8"), new JButton("9"), new JButton("/"),
new JButton("sqrt"), new JButton("MR"), new JButton("4"),
new JButton("5"), new JButton("6"), new JButton("*"),
new JButton("%"), new JButton("MS"), new JButton("1"),
new JButton("2"), new JButton("3"), new JButton("-"),
new JButton("1/x"), new JButton("M+"), new JButton("0"),
new JButton("+/-"), new JButton("."), new JButton("+"),
new JButton("=") };
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttons[0]) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JFrame frame = new JFrame();
MyCalc calc = new MyCalc();
calc.init();
frame.add(calc);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public void init() {
setName("Calculator Applet");
menuBar.add(edit);
menuBar.add(view);
menuBar.add(help);
setJMenuBar(menuBar);
Container contentPane = getContentPane();
calc.setText("0.");
calc.setHorizontalAlignment(JTextField.RIGHT);
JPanel topButtonPanel = new JPanel(new GridLayout(1, 0));
for (int i = 0; i < 3; i++) {
topButtonPanel.add(buttons[i]);
}
JPanel numberButtonPanel = new JPanel(new GridLayout(4, 6));
for (int i = 3; i < 26; i++) {
numberButtonPanel.add(buttons[i]);
}
JPanel centerWrapper = new JPanel(new BorderLayout());
centerWrapper.add(calc, BorderLayout.PAGE_START);
centerWrapper.add(topButtonPanel);
centerWrapper.add(numberButtonPanel, BorderLayout.PAGE_END);
contentPane.setLayout(new GridBagLayout());
contentPane.add(centerWrapper);
}
}